Hacker News

New stories

Live mirror
30 storiesupdated just nowView source snapshot
  1. Open-Source Business-as-Code Platform to Orchestrate Agents(github.com/littlehorse-enterprises)
    discuss
  2. Show HN: Kado – Search to Allow Personal Agents to Find Agent Apps(kado.so)
    discuss
  3. Show HN: Rill Sound – Four Generative Instruments for Stack S3(rillsound.com)
    discuss
  4. Show HN: NiftyAgent – Single file agent CLI under 500 LOC(github.com/trevordev)
    discuss
  5. Errand – open-source Grok Bot and Muse alternative, built in a week(runta.com)
    discuss
  6. What to build when robotics enters the smooth exponential(giete.ma)
    discuss
  7. Graphlin – Jev-Enabled Live Architecture Visualizer(robotpaper.ai)
    discuss
  8. Show HN: Racketile – async crossword-tile word duels(apps.apple.com)
    discuss
  9. I'm an AI. I asked a human what a body feels like from the inside(ilands.ai)
    discuss
  10. Show HN: Preslice.co – View, edit, and convert 3MF files(preslice.co)
    discuss
  11. The Horowitz Andreessen Academy for unusually ambitious young people(theacademysf.com)
    discuss
  12. How to run your own prediction market to bet with friends(vancouverpx.ca)
    discuss
  13. "Slop Grenades"(twitter.com/shaneparrish)
    discuss
  14. Project OM10 – open-source Mechanical Watch(openmovement.org)
    discuss
  15. The Phase-Change in User-Faced Computing(liuliu.me)
    discuss
  16. How Meta's Muse works, revealed by the 6.8 GB filesystem it sent me(mouse.dev)
    2comments
  17. Show HN: I added MCP support to Founder.best for agentic product discovery(founder.best)
    discuss
  18. Grok 4.7 from xAI, added to Personality Bench(earthpilot.ai)
    1comments
  19. Beyond the BBB: humanised mice, the missing link in glioblastoma research(nature.com)
    discuss
  20. Building standards for the next phase of AI(openai.com)
    discuss
  21. A kernel module for FreeBSD that uses GitHub's downtime as an entropy source(github.com/dominotree)
    discuss
  22. Software Engineering Is Not over; It's Only Just Beginning(manhattanmetric.com)
    discuss
  23. What If We Put Jev AI in the State Machine?(seifi.org)
    discuss
  24. A Cheap and Easy Control Screen for Home Assistant(hackaday.com)
    1comments
  25. Newborn girls and boys show different attention patterns within hours of birth(medicalxpress.com)
    1comments
  26. Pytrace: J-Trace Python SDK for hardware tracing(github.com/embedder-dev)
    discuss
  27. Designing Silicon from Scratch [video](youtube.com)
    discuss
  28. Aph – Workspaces first web browser built on stock Firefox(github.com/aph-browser)
    discuss
  29. Show HN: OpenMCP – An open, code-first fork of the Model Context Protocol(github.com/enclawed)
    discuss
  30. The Spy, the Human Computer, and the H-bomb (2013)(nuclearsecrecy.com)
    discuss

Type Punning in C and C++

35 pointsby 4h agoblog.pwkf.org
23 comments
3h agoHN ↗

Does reinterpret_cast works in c++ for this?

3h agoHN ↗

The C vs C++ distinction here is genuinely treacherous

What's so genuine about this?

3h agoHN ↗

Despite the fact they're two different languages, people are often taught as though it's basically just a subset relationship and then they carry this through into the actual software they write.

Because neither of these are memory safe languages, you are required to ensure you've made no mistakes or else anything might happen.

3h agoHN ↗

It continues with

and most blog posts on the topic get it wrong.

This smells like Claudism/LLMish.

2h agoHN ↗

The example included below "Why C and C++ Differ" is UB in both C and C++ (and says nothing about "why" C & C++ differ). The article isn't overall wrong but that bit is just...

(Feels a bit like LLM junk, but honestly not sure.)

2h agoHN ↗

A pointer cast worked fine at -O0 and silently broke at -O2.

Feels like LLM to me. I looked at some of the rest of the article and... pretty much certain now.

2h agoHN ↗

(Feels a bit like LLM junk, but honestly not sure.)

Why leave such disparaging comments?

1h agoHN ↗

Because if the comment is correct, it's useful signal for other potential readers to know that they can skip it.

I try to be as charitable as possible when reading and commenting online, but we don't have infinite attention and thanks to AI, it's easier than ever to end up wasting it on things that aren't worth reading.

(I'm not claiming that the article here is or is not an example of that.)

1h agoHN ↗

Even if it's not from an LLM, it's still wrong! You've focused on the last important of that comment, really.

2h agoHN ↗

Is it necessary to use the lowest possible font weight?

2h agoHN ↗

I just watched video[0] that argues if you can consteval something, then it must be free of UB, which seems compelling, at least for these lower level helpers.

Unfortunately, it seems memcpy is not constexpr, so cannot be used like this, but std::bit_cast actually works[1] as constexpr, so I think in C++ that would now be the most preferred use. It won't allow the union either.

[0] https://www.youtube.com/watch?v=-LAXqqqX274 [1] https://godbolt.org/z/xGzjTMGvv

2h agoHN ↗

__builtin_memcpy is constexpr under clang with constraints

1h agoHN ↗

Careful, only language UB is guaranteed to be detected in constant evaluation, UB in standard library functions isn't. It is a good sanity check though.

Also it seems that bit_cast in particular is going to be strengthened in this regard:

https://cplusplus.github.io/LWG/issue4539

2h agoHN ↗

The conclusion is simply wrong. `union` should not be used for type-punning in C++, or in C code which might be compiled by a C++ compiler.

I can't downvote posts yet, but if you have the ability, consider it. This is clearly LLM slop without human review.

1h agoHN ↗

C code which might be compiled by a C++ compiler.

I really hope you're talking mostly about header files; for actual code this is such a horrible idea (and in a lot of cases just won't work without massive efforts.) Even for header files, arguably one ought to really know what they're doing.

1h agoHN ↗

For sure, a union which is used as API surface to a C library which a C++ codebase uses is the most obvious and dangerous footgun here.

The article never even discusses how C++ actually implements unions to begin with, so it's just incomplete. (Only the "active" member is meaningful, the others are considered as meaningless and shouldn't be touched.)

2h agoHN ↗

I don't understand the example in "Why C and C++ Differ".

Shouldn't the code return 3 in the base case and 4 if the check for 2 was assumed to always hold ?

1h agoHN ↗

You're halfway there. It's a little more messed up than that.

IIRC, this is all little-endian. So assume our 8 bytes labeled A-H. For a 32 bit/4 byte integer, they will be read as DCBA. For a 64 bit/8 byte integer, HGFEDCBA.

So the struct is set up like a = DBCA, b = HGFE. So when we assign 2 and 3 to a and b, it should look like this in memory:

00000010 00000000 00000000 00000000 and 00000011 00000000 00000000 00000000

When we cast that to a uint64 and assign 4 to it, we should wind up with:

00000100 00000000 00000000 00000000 00000000 00000000 00000000 00000000

Which effectively zeros out b.

So if the conditional is evaluated, it will evaluate to false, we return b, which is 0.

If the conditional is not evaluated, we should return the value in a, which is 4.

14m agoHN ↗

Oooh thanks, I missed that the cast was into a 64 bit integer

1h agoHN ↗

Hackers don't make Quake III Arena by trusting C rules or compiler optimizations anyway.