Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

I swear I'm used to C you guys

Name: Anonymous 2013-05-29 16:37

So, in my code, I have the following function to remove nodes from a linked list.

colBox* remove(colBox* head, colBox* target)
{
    // If the target is the head of the linked list, free the head and return the rest of the list (whatever 'head' was pointing to)
    if(head == target)
    {
        target = target->next;
        free(head);
        return target;
    }
   
    // If the target is immediately following the head of the linked list, set head's "next" to whatever came after that target and free 'target'
    if(head->next == target)
    {
        head->next = head->next->next;
        free(target);
        return head;
    }

    // Otherwise, there's enough room in the linked list to search through it properly, find the target, and remove it
    colBox* searcher = head;
    while(searcher->next != target)
    {
        searcher = searcher->next;
    }
    searcher->next = target->next;
    free(target);
    return head;
}

However, I'm always getting "Access violation reading location 0xfeeefeee" exception errors during the lines that say 'free'. If I comment those lines out, the code appears to work exactly as I intended it, but I know that there's memory not being properly freed. The code I have at the end of the program in 'main', to free whatever remains of the linked list, doesn't cause any errors. Can anyone tell me why errors are happening here? I'm pretty sure there aren't any rules about 'free' lines needing to be in 'main'.

Name: L. A. Calculus !!wKyoNUUHDOmjW7I 2013-05-29 16:42

YEA, LIKE DER ISN'T A FUNCTION IN DA STANDARD C LIBRARY DATS ALREADY CALLED 'remove'. TRY REEDIN DA STANDARD YA FUCKIN RETOID.

Name: Anonymous 2013-05-29 17:22

>>2

The function gets overloaded, so it doesn't matter. I already tried simply renaming the function and that changed nothing.

Name: Anonymous 2013-05-29 17:41

Why do you have three branches for doing one thing?

Name: Anonymous 2013-05-29 18:03

Name: Anonymous 2013-05-29 18:51

>>4

Good point.

There's one situation when dealing with linked lists where you need three branches, but I couldn't remember which one it was. I guess it might have been something to do with BSTs instead that needed three branches? Regardless,

colBox* remove(colBox* head, colBox* target)
{
    // If the target is the head of the linked list, free the head and return the rest of the list (whatever 'head' was pointing to)
    if(head == target)
    {
        target = target->next;
        //free(head);
        return target;
    }

    // Otherwise, there's enough room in the linked list to search through it properly, find the target, and remove it
    colBox* searcher = head;
    while(searcher->next != target)
    {
        searcher = searcher->next;
    }
    searcher->next = target->next;
    //free(target);
    return head;
}

As soon as I uncomment the 'free' lines, I get exceptions whenever the code reaches those 'free' lines.

>>5

That seems wholly unrelated.

Name: Anonymous 2013-05-29 19:54

>>6
So you got it down to two. Now cut it down to one, which is all that's really needed. As for your crash, you're probably trying to free statically or stack-allocated memory or something equally retarded. The problem is the input, not the function.

Name: Anonymous 2013-05-29 20:12

>>7

>Now cut it down to one, which is all that's really needed.

I don't see much of a reason to, honestly.

>As for your crash, you're probably trying to free statically or stack-allocated memory or something equally retarded.

That's not it, because I free that same memory later in the program without issue.

// Free the linked list
searcher = head;
while(searcher != NULL)
{
    searcher = searcher->next;
    free(head);
    head = searcher;
}

This is the same linked list of the same structs. Any members of the linked list that I didn't use the 'remove' function on would get freed by this bit of code, and this bit of code has never given me errors at all. This bit of code is at the very end of 'main'.

Aside from one 'free' statement being in a function that gets passed the head pointer and the other 'free' statement being at the end of main, I can't find much of a difference between the two, let alone enough of a difference that one would cause an exception error and the other wouldn't.

Name: Anonymous 2013-05-29 20:19

>>8
Please use the code tags and learn to quote like someone who doesn't come from the imagereddits or fuck off back to /g/.

Name: Anonymous 2013-05-29 20:25

>>9

No.

Name: Anonymous 2013-05-29 20:35

>>10
LLLLLLLLEEEEEELLLLLLL I'M SUCH A /G/ REBEL
INSTALL
>LE /G/ENTOO
XDDDDDDDDDDD
>LLLLLLLLLLLLEEEEEEEEELLLLLLLLLLLLLL

Name: Anonymous 2013-05-29 20:53

>>10
See >>9? See why helping retards when they come here is against the rules of /prog/? Next time please don't break the rules, so that the retards will learn to not come here with their homework questions that are answered by reading the standard.

Name: Anonymous 2013-05-29 22:12

top lel

Name: L. A. Calculus !!wKyoNUUHDOmjW7I 2013-05-29 23:42

>>3
The function gets overloaded
YAINT KNO C AND YAINT RED DA STANDARD, YA FUCKIN RETOID. DATS Y UR PROGRAM'S SHIT N DAT'S Y UR A SHITTY PROGRAMMER.

Name: Anonymous 2013-05-30 0:45

>>8
I don't see much of a reason to, honestly.
Forever destined to be a shit-tier programmer.
That's not it, because I free that same memory later in the program without issue.
Obviously there's some fucking difference.

Name: Anonymous 2013-05-30 3:52

Imagine target is not in the linked list. Well it seems OP didn't really think about much of anything.

Name: Anonymous 2013-05-30 4:38

>>6
As soon as I uncomment the 'free' lines, I get exceptions whenever the code reaches those 'free' lines.
This generally means you're writing over the bounds of your allocated blocks somewhere

Name: Anonymous 2013-05-30 23:06

>>8,15,17
Freeing uninitialized pointer, freeing null pointer, double freeing any pointer.

Don't change these.
Name: Email:
Entire Thread Thread List