Leave these fields empty (spam trap):
Name
You can leave this blank to post anonymously, or you can create a Tripcode by using the format Name#Password
Comment
[i]Italic Text[/i]
[b]Bold Text[/b]
[spoiler]Spoiler Text[/spoiler]
>Highlight/Quote Text
[pre]Preformatted & Monospace Text[/pre]
[super]Superset Text[/super]
[sub]Subset Text[/sub]
1. Numbered lists become ordered lists
* Bulleted lists become unordered lists
File

Sandwich


Discord Now Fully Linked With 420chan IRC

C++ delete not working on (I believe) allocated pointer

Reply
- Wed, 27 Sep 2017 21:44:16 EST 5sC0bueS No.37196
File: 1506563056371.jpg -(111717B / 109.10KB, 1920x1080) Thumbnail displayed, click image for full size. C++ delete not working on (I believe) allocated pointer
Sup my people, I come here, humbly, once again to seek your guidance of C++.

I'm trying to implement my own linked list in a search algorithm and am running in to memory leaks. I define a node struct, create a 4 element array of pointers (because of the search algorithm), use new to fill those pointers, add those pointers to a linked list, and then create 4 new elements using the same array of pointers. HOWEVER, when I go to iterate through the list later to delete everything, I get an error saying:

malloc: *** error for object 0x7fff5fbfe4f0: pointer being freed was not allocated

I don't understand why I saying it was never allocated? Here is some of the code highlights and I'll also paste a link to the full code on pastebin:

//USING NEW TO (ALLOCATE?) NEW MEMORY (THIS IS IN A LOOP)
list_node* neighbors[4]; //The FOUR neighbors
neighbors[0] = new list_node;
*neighbors[0] = {nullptr, nullptr, q, (q->x)-1 , q->y , 9999, 9999, 9999};
neighbors[1] = new list_node;
*neighbors[1] = {nullptr, nullptr, q, (q->x)+1 , q->y , 9999, 9999, 9999};
neighbors[2] = new list_node;
*neighbors[2] = {nullptr, nullptr, q, q->x , (q->y)-1 , 9999, 9999, 9999};
neighbors[3] = new list_node;
*neighbors[3] = {nullptr, nullptr, q, q->x , (q->y)+1 , 9999, 9999, 9999};

//DEBUGGING
//Print out the closed and open_sets
//...the closed set
bool printing = true;
list_node* current2 = copen_set.first;
printf("\nCLOSE ");
while(printing){
if(current2==nullptr){
printing = false;
break;
}
printf(" (%d,%d) ", current2->x, current2->y );
list_node* temp = current; //Fo deletion
current = current->next;
delete temp; //Also fo deletion

So that code will print out the node in the list just fine, but gets hung up at the call to delete. Here's a link to pastebin:
https://pastebin.com/ghpn92HC

Thanks guys and gals, I know you always come through for me!
>>
Eugene Hillynore - Wed, 27 Sep 2017 22:29:49 EST gezKXAce No.37197 Reply
>>37196
At the start of your code

list_node temp = {nullptr, nullptr, nullptr, x1, y1, 0, 0, 0};

need to use new there
>>
Matilda Bandlechutch - Thu, 28 Sep 2017 19:36:34 EST 5sC0bueS No.37198 Reply
1506641794371.png -(807B / 807bytes, 48x48) Thumbnail displayed, click image for full size.
>>37197
Holy shit, that worked! Thank you so much.

I forgot to initialize the very first node with new and delete somehow knew. Does that function check the address and it's able to tell what's on the heap or something?
>>
Thomas Brindleworth - Thu, 28 Sep 2017 23:13:20 EST gezKXAce No.37199 Reply
>>37198
I believe calling delete on something not created with new is undefined, so it depends on your compiler.
>>
George Senningstone - Fri, 29 Sep 2017 13:08:19 EST P6PS9CBz No.37200 Reply
>>37198
At least on Windows with the debug CRT there's a mechanism built in to detect you calling delete or free on a pointer that did not come from the current process heap. You can easily do this in constant-time by simply checking the address against the lower and upper address bounds of your heap region.

Report Post
Reason
Note
Please be descriptive with report notes,
this helps staff resolve issues quicker.