Page 1 of 1

[SOLVED] (&ret)->position inequal to ret.position?

Posted: Sat Apr 27, 2013 5:42 am
by HugeCode
Hi. I have simple question: is this code

Code: Select all

(&ret)->position
inequal to this

Code: Select all

ret.position
counting with 'ret' as variable allocated on stack (in function body)?

Re: (&ret)->position inequal to ret.position?

Posted: Sat Apr 27, 2013 6:24 am
by Brendan
Hi,
HugeCode wrote:Hi. I have simple question: is this code

Code: Select all

(&ret)->position
inequal to this

Code: Select all

ret.position
counting with 'ret' as variable allocated on stack (in function body)?
Looks equivalent to me.


Cheers,

Brendan

Re: (&ret)->position inequal to ret.position?

Posted: Sat Apr 27, 2013 9:13 am
by HugeCode
I don't know why but when I have variable declared in function (main(), it's toplevel function), it shows me crazy values. If I have it as static, it's OK.

Re: (&ret)->position inequal to ret.position?

Posted: Sat Apr 27, 2013 10:01 am
by Combuster
In other words:
- You asked the wrong question
- You're making the ancient mistake of not initializing your local variables.
- You're silencing or otherwise not listening to your compiler warnings.

Re: (&ret)->position inequal to ret.position?

Posted: Sat Apr 27, 2013 10:25 am
by HugeCode
... or I found out that the code compiled as 32bit can't carry more than 4 bytes from the method.

Re: (&ret)->position inequal to ret.position?

Posted: Sat Apr 27, 2013 12:28 pm
by dozniak
HugeCode wrote:... or I found out that the code compiled as 32bit can't carry more than 4 bytes from the method.
What are you talking about.

Combuster precisely told you what your problem is.
Your comment about "when static it's ok" shows you have no idea how static variables are allocated.

Go enable all warnings and read them carefully.

Re: (&ret)->position inequal to ret.position?

Posted: Sun Apr 28, 2013 1:17 am
by HugeCode
I know that static variables are allocated statically, so in compilation time as data. auto variables are allocated on stack. My structure has 12bytes, but I was testing only first 4 so I thought returning is OK.

Re: (&ret)->position inequal to ret.position?

Posted: Sun Apr 28, 2013 1:30 am
by bluemoon
HugeCode wrote:I know...
You think you know it, but you dont.
The specification does not specific where to put auto variable, it can be on register, stack, on cloud, or eliminated if it does not affect program logic.

Re: (&ret)->position inequal to ret.position?

Posted: Sun Apr 28, 2013 4:13 am
by dozniak
HugeCode wrote:... or I found out that the code compiled as 32bit can't carry more than 4 bytes from the method.
Are you saying that this does not work? While it clearly does.

Code: Select all

#include <assert.h>
#include <stdint.h>
#include <stdio.h>

struct a12bytestruct {
    uint32_t a, b, c;
};

struct a12bytestruct get() {
    struct a12bytestruct ret = {0xabba,0xdead,0xbabe};
    return ret;
}

int main() {
    struct a12bytestruct some = get();
    assert(some.a == 0xabba);
    assert(some.b == 0xdead);
    assert(some.c == 0xbabe);
    printf("All OK!\n");
}

Code: Select all

$ clang a12byteret.c -m32 -o a12byteret
$ file a12byteret
a12byteret: Mach-O executable i386
$ ./a12byteret
All OK!

Re: (&ret)->position inequal to ret.position?

Posted: Sun Apr 28, 2013 9:07 am
by HugeCode
For my compiler configuration it doesn't.

Re: (&ret)->position inequal to ret.position?

Posted: Sun Apr 28, 2013 9:11 am
by Combuster
I think that you got yourself a compiler with the PEBKAC bug enabled. It needs manual fixing.


Hint: go read forum rules 3 and 4.

Re: (&ret)->position inequal to ret.position?

Posted: Sun Apr 28, 2013 1:36 pm
by dozniak
HugeCode wrote:For my compiler configuration it doesn't.
Proof wanted.

Re: (&ret)->position inequal to ret.position?

Posted: Mon Apr 29, 2013 10:00 am
by HugeCode
Currently it works, it had to be caused by my pointer mess.