General statistics
List of Youtube channels
Youtube commenter search
Distinguished comments
About
Anony Mousse
Low Level
comments
Comments by "Anony Mousse" (@anon_y_mousse) on "a strange but powerful interview question" video.
@pwii Indeed. You have to turn off optimizations to get it to work, and if you do, you have no need for the volatile keyword anyway.
4
Actually, I thought about it some more and realized that you still have the same problem. Since their ordering is enforced it will always tell you the same direction. Let's say struct { int x; int y; }; x will always be at a lower address than y, no matter which direction the stack grows.
2
This is one of the problems with optimizations. It can alter the functionality of your code if you're not careful, and why I always test with both.
2
There's an easier non-recursive method. Use an inner block. `bool does_it_grow_up ( void ) { int x; { int y; if ( &y > &x ) return true; } return false; }` // but I can't wait until more compilers support C23 and we can all stop using `#include <stdbool.h>`.
2
Newer variables getting lower addresses is the stack growing down, not up. As for the parameter method of testing, the order in which they're passed to the function is actually unspecified, and whether they're even on the stack as well. On Linux they would be in registers and the compiler would have to generate code to copy them to the stack and it could do so in any order as well.
1
That feels like a bug, but it does the same for me despite p1 and q pointing to the same address.
1
@1234567qwerification Can you reference the section that specifies it?
1
@1234567qwerification I guess I'll have to find a copy of the C11 standard, as I don't seem to have it saved on this computer. Assuming that those references don't come from ChatGPT anyway, because none of those equate to this being valid behavior in the current standard.
1
@1234567qwerification Okay, and apparently the standard didn't change substantially between then and now, and still, none of those equate to this being anything other than a bug.
1
No matter which way the stack grows that will be true of an array. The only way that will work is to turn off optimizations with the compiler and declare two separate variables, either in subsequent function calls as he does, or using an inner scoped block.
1
I don't know about clang, but gcc used to have an alloca function and may still, but due to C99 adding VLA's hasn't been warranted in that long.
1
It's actually the same either way because it's just allocating space. However, C99 gave us VLA's so the size doesn't have to be known at compile time. There used to be a common function that several compilers had as an extension called alloca() which could allocate space on the stack in basically the same way as VLA's work, but since C99 it disappeared.
1