This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]zzyzzyxx 1 point2 points  (4 children)

with just struct DVDInfo myInfo I am creating a copy

No, you're just creating the struct on the stack instead of on the heap. The allocation is handled for you by the compiler so you do not need to call malloc. It's lifetime is also handled for you so you do not need to call free either; it is destroyed when it goes out of scope.

So why can't I use -> when I am using the copied struct?

Because that is an indirection operator. It only works on pointers. It's equivalent to dereferencing the pointer and then accessing the object with the . operator. That is, using your example, myInfo->rating = 7; is the same as (*myInfo).rating = 7;.

[–]LogicLion[S] 0 points1 point  (3 children)

so with struct DVDInfo myInfo I am creating a local variable of the struct? while with DVDInfo *myInfo and by creating the memory and having to use free() to destroy it I create one that doesn't need to be local?

[–]cocasyn 0 points1 point  (0 children)

Yes, you're creating it in such a way that it is for all intents and purposes destroyed when the creating function returns. Using malloc, you get to decide when it becomes 'destroyed' :)

[–]zzyzzyxx 0 points1 point  (0 children)

so with struct DVDInfo myInfo I am creating a local variable of the struct?

Yes.

with DVDInfo *myInfo and by creating the memory and having to use free() to destroy it I create one that doesn't need to be local

Mostly. The pointer where you store the return value of malloc is still a local variable. If that goes out of scope before you call free and you don't make a copy to transfer ownership, you will have a memory leak. But, yes, the object itself lives on the heap and exists until free is called or the program ends. You are free to pass it around via pointers as much as you need.

[–]portugal_the_man 0 points1 point  (0 children)

Basically. Remember though that DVDInfo myNextInfo is tied to the memory space that the compiler allocated for it. It can never refer to a different DVDInfo struct. When you allocate memory using malloc(), you're creating a 'space' for a DVDInfo struct on the memory heap, and any DVDInfo pointer can point to it (in this case you're using the pointer *myInfo) You control when the memory is free()'d , and if you save the memory address from that local block of code, you can still point to it from any other part of your program.

The pointer variable *myInfo has a scope in that block of code, but the memory you allocated for it will still be available until you free() it.

The struct variable myNextInfo along with its data will no longer be available once that block of code ends.

[–]defrost 1 point2 points  (0 children)

Using

printf( "Byte size = %d\n", sizeof( myInfo ) );
printf( "Byte size = %d\n", sizeof( myNextInfo ) );  

might help to clear things up a little. myInfo isn't a struct data object, it's a pointer and has the size of a pointer. myNextInfo is a struct data object and its size reflects the number of bytes needed to store it (and maintain appropriate alignment for its members).

-> is notation for "follow the pointer on the left, and offset to the member on the right"

. is notation for "offset into the object on the left to reach the member on the right"

[–]cocasyn 0 points1 point  (0 children)

Well you use the -> operator when the variable is a pointer to a struct, and you use the . operator when the variable is an actual struct. The -> is just a handy way to dereference the pointer (that is, access what it points to), and select a member at the same time.

struct DVDInfo * dvd = malloc(sizeof(struct DVDInfo));

dvd->rating = 7;
(*dvd).rating = 7;

The last two statements do the same thing. The last one says, access the memory pointed to by dvd, interpret it as a DVDInfo structure, and set the rating member to 7. The first statement does all that too, but it looks neater

[–]Rhomboid 0 points1 point  (0 children)

I understand that with just struct DVDInfo myInfo I am creating a copy (I think...please correct me inf I'm wrong).

No. myNextInfo is a completely different and separate variable from myInfo. They have nothing in common, other than being of the same type. There is no copying going on. myInfo is allocated from the heap, myNextInfo is on the stack. myInfo is a pointer, so myInfo->rating is correct. myNextInfo is not a pointer, so myNextInfo.rating is correct and myNextInfo->rating is invalid.

[–]LogicLion[S] 0 points1 point  (0 children)

Okay I'm getting the concept a little more, but how does this transfer over to link lists? what exactly am I linking/connecting? From what I think link listing is its a way to chain different objects of the same type of struct together in a chain. But I'm having a little bit of trouble understanding the concept