News for hackers. Stuff that matters.
Re:The Strict Aliasing Situation Is Pretty Bad by haberman on Tuesday March 15, 2016 @09:38AM (#11289483)
> Actually, casts to/from char * are always defined in C (chars are always assumed to alias).

Not true. The standard says you can access any object's value via the char type, but not the reverse. You can't cast a character array to any type and dereference it.

> The author was talking about "chunking" non-char units.

Sure, but you can call my copy_8_bytes() function like so legally:

    int64_t a, b = 0;
    copy_8_bytes(&a, &b);
So what I said applies to non-char types.
Re:The Strict Aliasing Situation Is Pretty Bad by TorKlingberg on Tuesday March 15, 2016 @10:48AM (#11289995)
> You can't cast a character array to any type and dereference it.

So making your own malloc backed by a static char array is undefined behavior?

Re:The Strict Aliasing Situation Is Pretty Bad by haberman on Tuesday March 15, 2016 @11:09AM (#11290182)
Yes, I believe it would be. That's a good point, now that you mention it -- I have code that does just that, and I hadn't realized it's probably undefined.

The "effective type" (this is a term defined in the standard) of the char array elements would be "char", whereas the memory returned from malloc() is considered to be an object that initially has no effective type. I don't know of any way to take the char array and "erase" its effective type so that it can be used generically, like the value returned from malloc().

This is a problem!

Re:The Strict Aliasing Situation Is Pretty Bad by jblow on Tuesday March 15, 2016 @05:10PM (#11293022)
This is one reason among many why people who write serious low-level code (e.g. game developers) think all the new aliasing rules are completely bonkers.

We implement our own allocators all the time. If you can't even do such a basic thing legally, then the rules are obvious nonsense.

Re:The Strict Aliasing Situation Is Pretty Bad by vc98mvco on Tuesday March 15, 2016 @11:38AM (#11290424)
Did you even make sure the memory was aligned?

Non-allocated memory (stack char) cannot be used like that. I'm sure you know alloc(), and malloc() is fast for small sizes.

Re:The Strict Aliasing Situation Is Pretty Bad by haberman on Tuesday March 15, 2016 @11:44AM (#11290467)
> Did you even make sure the memory was aligned?

Of course, what do you take me for? :)

> I'm sure you know alloc()

Do you mean alloca? It has a lot of problems, and is generally prohibited at Google.

> and malloc() is fast for small sizes.

Not nearly fast enough for my purposes.

Re:The Strict Aliasing Situation Is Pretty Bad by vc98mvco on Tuesday March 15, 2016 @12:08PM (#11290654)
> Just making sure. Fix that code will you! :)

> Yes, alloca. (Don't use it.)

> Just make a fast allocator that uses heap instead of the stack. You only need to malloc once and it can be used for any type since like you pointed out, it's effective type can be changed.

Re:The Strict Aliasing Situation Is Pretty Bad by TorKlingberg on Tuesday March 15, 2016 @12:26PM (#11290808)
Sometimes, especially in embedded systems, it is useful to have a bunch of statically allocated heaps. You can see them in a memory map, and the linker will tell you if they don't fit in memory.

There is also the case where you have some raw data from a file or network, that you want to re-interpret as a struct. That is always dangerous with endianness and struct padding, but it is a very common practice. You could always memcpy from a char array to a struct, but that can waste memory.

Re:The Strict Aliasing Situation Is Pretty Bad by mcguire on Tuesday March 15, 2016 @04:05PM (#11292562)
Ok, what's wrong with alloca? Other than blowing the stack, that is.
Re:The Strict Aliasing Situation Is Pretty Bad by haberman on Tuesday March 15, 2016 @06:00PM (#11293353)
See: https://google.github.io/styleguide/cppguide.html#Variable-L...

At least that's the rationale for why we don't use it at Google.

Re:The Strict Aliasing Situation Is Pretty Bad by mcguire on Thursday March 17, 2016 @11:56AM (#11305159)
"More importantly, they allocate a data-dependent amount of stack space that can trigger difficult-to-find memory overwriting bugs: "It ran fine on my machine, but dies mysteriously in production"."

Yep, blowing the stack will do that.