Domanda

I have the following code in which I wrote two functions. Both are meant to produce the same output. But the function g() which has loop produces a different output from what I had expected as shown below.

#include <stdio.h>

struct S { int i; };

void f(void)
{
    struct S *p;

    int i = 0;

    p = &((struct S) {i});
    printf("%p\n", p);

    i++;

    p = &((struct S) {i});
    printf("%p\n", p);
}

void g(void)
{
    struct S *p;

    for (int i = 0; i < 2; i++)
    {
        p = &((struct S) {i});
        printf("%p\n", p);
    }
}

int main()
{   
    printf("Calling function f()\n");
    f();

    printf("\nCalling function g()\n");
    g();
}

Output:

Calling function f()
0023ff20
0023ff24

Calling function g()
0023ff24
0023ff24

How come the address of p is same in case of g() when it was called?

È stato utile?

Soluzione

Well, I don't know exactly what are you trying to accomplish, but what happens here is:

  • the (struct S){i} notation in C99 will create new data structure on stack
  • this data structure is automaticaly destroyed at the end of the scope in which it was created

So in f() function you actually make TWO distinct structures in the scope of the whole function (even if you assign their addresses to the same pointer) - hence two different addresses.

void f(void)
{
    struct S *p;

    int i = 0;

    p = &((struct S) {i}); // <- first data structure, let's call it A
    printf("%p\n", p);     // <- address of the structure A printed

    i++;

    p = &((struct S) {i}); // <- second data structure, let's call it B
    printf("%p\n", p);     // <- address of the structure B printed
}                          // <- both A and B destroyed

But in g() function the p is created and destroyed in the inner block of the for block, and so it happens that p is allocated over and over again in the same position on stack, giving always the same address.

void g(void)
{
    struct S *p;

    for (int i = 0; i < 2; i++)
    {
        p = &((struct S) {i}); // <- data structure A created
        printf("%p\n", p);     // <- data structure A's address printed
    }                          // <- data structure A destroyed
}

Altri suggerimenti

You should check assembly code to be sure but I guess that, since p is assigned locally in a loop scope with the address of an automatic allocated variable (which is allocated on the stack), it just reuses the same space on the stack.

In f() it doesn't do it because both structs coexist in the same scope while in g() compiler is sure that at the end of the first iteration you won't be able to use &((struct S) {0}).

Just tried for curiosity with -O2 on gcc4.2 to see if anything changes:

Calling function f()
0x7fff5fbff388
0x7fff5fbff380

Calling function g()
0x7fff5fbff390
0x7fff5fbff390
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top