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

Programming, for all ages and all languages.
Post Reply
HugeCode
Member
Member
Posts: 112
Joined: Mon Dec 17, 2012 9:12 am

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

Post 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)?
Last edited by HugeCode on Sun May 12, 2013 6:03 am, edited 1 time in total.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

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

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
HugeCode
Member
Member
Posts: 112
Joined: Mon Dec 17, 2012 9:12 am

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

Post 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.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

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

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
HugeCode
Member
Member
Posts: 112
Joined: Mon Dec 17, 2012 9:12 am

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

Post by HugeCode »

... or I found out that the code compiled as 32bit can't carry more than 4 bytes from the method.
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

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

Post 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.
Learn to read.
HugeCode
Member
Member
Posts: 112
Joined: Mon Dec 17, 2012 9:12 am

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

Post 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.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

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

Post 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.
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

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

Post 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!
Learn to read.
HugeCode
Member
Member
Posts: 112
Joined: Mon Dec 17, 2012 9:12 am

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

Post by HugeCode »

For my compiler configuration it doesn't.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

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

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

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

Post by dozniak »

HugeCode wrote:For my compiler configuration it doesn't.
Proof wanted.
Learn to read.
HugeCode
Member
Member
Posts: 112
Joined: Mon Dec 17, 2012 9:12 am

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

Post by HugeCode »

Currently it works, it had to be caused by my pointer mess.
Post Reply