简体   繁体   中英

Will a pointer declaration to a structure allocate memory for its members?

check the image of program on turbo c++

output of program

#include< stdio.h>

struct node  {
    int data;
};

int main() {

    struct node *ptr;

    ptr->data=3;
    printf("%d",ptr->data);

    return 0;
}

Output: 3

My question is that even though I have not declared an instance of structure node.

eg struct node n;

Also I have not assigned memory for ptr using malloc, it still allocates space for ptr->data. Why? Shouldn't it just assign memory for address ptr on stack.

And where this memory for data field is allocated, on stack or heap.---------

In your code, ptr->data=3; invokes undefined behavior , as ptr does not point to any valid memory location.

You need to make sure that ptr points to a valid memory location before making any attempt to de-reference it.

It appears to work properly in your case, that is also one of the most dangerous side effects of undefined behavior.

That said, ptr is having automatic storage. The memory it would point to, will depend on the allocation used. C standard does not have a notion of stack or heap - that is dependent on the implementation.

Also I have not assigned memory for ptr using malloc, it still allocates space for ptr->data.

Nothing's been allocated. Without an initializer, the value of ptr is indeterminate , and in this case just happens to point to memory that you can write to without immediately crashing your program. You managed to not overwrite anything "important", but that's a matter of luck more than anything else.

You should get in the habit of initializing pointers as you declare them, either as NULL or as the result of the & operator or as the result of a malloc / calloc / realloc call.

When you are creating the pointer struct node *ptr; it will be placed on your stack. The place where this variable is placed can be anywhere in your stack.

For example it is placed on address 0x100.

When the variable is placed on that location, the data that is there is not set to 0. So on address 0x100 was the data 0x200 for example, you will have a struct node *ptr pointing to the address 0x200 .

Then at that point you are doing the following ptr->data=3; As the code thinks that there is a struct node placed at your address 0x200, it will fill the variable data in there. So on the address 0x200 will the value 3 be written.

When you are in luck (well actually it isn't really luck, as it could cause lots of problems later on) the writing of the value 3 at the address 0x200 will not cause any problems, because the memory at address 0x200 wasn't used.

But keep in mind that your code only THINKS that the struct node was placed at that location, but actually there isn't one, so any other variable could be on that location. And when there is a different variable on that address. when you write the value 3 to that address you are you are overwriting the value of that variable. And that could create some very strange behavior in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM