last day (14 days later) » 

10:57 AM
1
A: creating new struct in cycle

RobertWatch out the struct ed is only valid within the for-loop i.e. it's visibility so if you're lucky you get no crash; I would suggest using: for(i = 0; i < m; i++) { cin >> t1 >> t2; e* ed = new e(); ed->next = 0x00; ed->choosen = false; ed->target = &b[t2]; add(&b[t1], ...

 
you are doing the same thing as he is; the difference is the is using a initialization list and you are explicitly assigning values
 
@Pandrei Difference between e ed; and e* ed = new e();??
 
sorry missed that; but you do add a memory leak -please edit your answer to fix it.
 
@Pandrei: The answer says "don't forget to delete the elements", which will fix the leak. They shouldn't be deleted at this point since they've been passed to add. (Although a note about smart pointers might be nice.)
 
@Pandrei where's there a memory leak. Since we do not know where he needs the structs (most likely somewhere outside) we may not delete them (thats why I wrote that he does not forget to delete them later on)
 
10:57 AM
@MikeSeymour the local variable is allocated on the stack, so it should not matter unless the compiler is doing some optimizations; each time you enter the for loop a different object of type ed should be allocated on the stack
 
@Pandrei: I've no idea what you mean. The e object can't be a local variable since add stores a pointer to it to use later - after a local variable will have been destroyed. A solution, as this answer describes, is to dynamically allocate the object, so that it will last as long as it's needed.
 
@Robert: each time you call new() you allocate new memory on the heap and you use the same pointer to keep track of it; so if you don't call delete before calling new you have a memory leak. If you call delete outside of the loop you will free only the last allocated object
 
@Pandrei I think the add() function inserts the struct into a container (list, map, whatever) so he stores the pointers somewhere. So when he dont need the elements anymore, he can go through his container, remove and delete them.
 
@MikeSeymour: actually that's not true; the function add does take a pointer, but it does not store it, it copies the content of the pointer to some other location; and this happens in the same for iteration. The code should work fine without needing the variable to be allocated on the heap
 
@Pandrei: How on earth do you know that without seeing what add does? Clearly it doesn't do that, otherwise there would be no problem and the question wouldn't be asked. The question certainly wouldn't say "after all there are just pointers to the same struct e" if there were no pointers being stored.
 
10:57 AM
@Robert: we're just assuming what add does; if you ask me I wouldn't store the pointers as it's more error prone.
 
@Pandrei Why should he pass the address to the struct add(...,&ed) to then internally copy the content be derefernce the pointer again with *ed that would not realy make sense (in my opinion), so I think it is most likely that he stores the pointer. In addition, as the OP states out: "So after all there are just pointers to the same struct e"
 
@MikeSeymour: I was just trying to make a point; we can't really say that the correct solution is unless we know what add is doing. regardless of what it does this piece of code should work fine with the variable allocated on the stack. Though the code might break somewhere else
@Robert because if you don't pass a pointer to a struct as argument to a function but pass it by value, you copy the content of the struct on the stack - that's why you ALWAYS pass struct variables to functions by pointer.
 
@Pandrei: It's quite clear from the description what add does, and why it's a problem if ed is a local variable. If you have a better solution than dynamic allocation then please write your own answer rather than posting confusing comments to someone else's.
 
@MikeSeymour now it becomes clear, because he posted the code to add; when we started debating it was not there.
 
@Pandrei after reading discussion I've post it. So far I don't need to use the delete() in for cycle because it is stored in another struct? Is it correct?
 
10:57 AM
@user1889548 correct
 
@Pandrei To your statement: "because if you don't pass a pointer to a struct as argument to a function but pass it by value, you copy the content of the struct on the stack - that's why you ALWAYS pass struct variables to functions by pointer." If I really only need the content, then I would use a (probably const) reference
 
@Robert: I'm not sure where you are going with this :) the compiler implements pointers and reference variables the same way -> using pointers
 
However, in my opinion it is actually "cleaner" passing a reference when only the data would be needed within the add function
@Pandrei However, in my opinion it is actually "cleaner" passing a reference when only the data would be needed within the add function
 

last day (14 days later) »