We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
nesting a structure inside a structure is just and plainly a bad idea
Structures containing pointers to other structures are common and idiomatic in C... essentially every graph data structure uses such pointers to represent edges. A classic linked list, for example:
structnode{intdata;structnode*next;}
the second outer structure will be allocated... overwriting the first inner structure
Malloc will never return a memory block that aliases any block it previously returned until that block has been free()'d. In the situation you describe, malloc would simply allocate room for the second outer structure after the first inner structure.
the allocation of memory might differ after a new compilation
Allocations using malloc are resolved at run-time, not compile-time. The only memory allocations resolved at compile-time are variables with automatic and static storage classes. Y-You aren't allocating the whole data structure on the stack, are you?
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Structuring the Document
You are viewing a single comment's thread. Return to all comments →
Lolwut?
Structures containing pointers to other structures are common and idiomatic in C... essentially every graph data structure uses such pointers to represent edges. A classic linked list, for example:
Malloc will never return a memory block that aliases any block it previously returned until that block has been free()'d. In the situation you describe, malloc would simply allocate room for the second outer structure after the first inner structure.
Allocations using malloc are resolved at run-time, not compile-time. The only memory allocations resolved at compile-time are variables with automatic and static storage classes. Y-You aren't allocating the whole data structure on the stack, are you?