Thread: Help free structs and pointer.

  1. #16
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    hm you kind of lost me there.
    So I have this list LIST_ELEM which holds a bunch of structs

    pHead <-strct1<-strct2

    and when I remove it i want it to look like

    pHead<-struct2

    or in the first case

    pHead<-strct1

    remove returns

    pHead


    but i'm not getting anything like that, I'm getting my original list when print it out, or find the size

  2. #17
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    so if I call a remove on my list which holds 1 item, shouldn't the size of the list after the remove return 0?
    Last edited by TaiL; 10-04-2008 at 05:56 PM.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So run the following code and play "spot the parallel":
    Code:
    void func1(int bob) {
        bob = 6;
    }
    
    void func2(int *bob) {
        bob = NULL;
    }
    
    int main(void) {
        int i = 11;
        int *iptr = &i;
        func1(i);
        printf("&#37;d\n", i);
        func2(iptr);
        if (iptr == NULL) printf("Variable successfully removed.\n");
        printf("%p\n", iptr);
        return 0;
    }

  4. #19
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    11 and an address is returned.

  5. #20
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    so i basically can't set something to null and say it was removed.

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Even though you pass in a pointer to the function, that means you can only change the value of what is pointed to, not the value of the pointer itself (that's just a copy). So if you want to change the value of the pointer, which it seems like you do, you'll have to add an extra *.

  7. #22
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    So if:

    LIST_ELEM *ptr = pHead;

    and I want to free the actual item at *ptr

    i would have to use

    free(**ptr)? doesn't that return an invalid type error?

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No you would have to use
    Code:
    void removePtr(LIST_ELEM **pHead, int loc)
    and adjust accordingly.

  9. #24
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    Man that would make it more difficult for me.

    so taking in a *pHead and then setting equal to say

    Code:
     LIST_ELEM **ptr = pHead

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You could do that but you're back where you started -- pHead wouldn't change just because you changed ptr.

    I'm starting to worry a little bit -- all the list functions require a elem** to work (creating, adding, deleting, ...)

    EDIT: And for that matter, it's not as though the change is hard: do a search-and-destroy turning pHead to *pHead everywhere in the function and you're done!

  11. #26
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    LOL you're beginning to worry me now. I've been using (LIST_ELEM *pHead) for my add, print, and find methods.

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, okay, print just takes one star. Find could take one * if you're not trying to change the list. But if you add something to the front of the list and try to change pHead = just_created_element, that won't work.

  13. #28
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    so my program is a fluke since I use one * for add, and sizeOf returns the right size after adding in an element.

  14. #29
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you in a special case where you are always adding at the end or something?

  15. #30
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    always adding to the front

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using free with a casted pointer..
    By smoking81 in forum C Programming
    Replies: 3
    Last Post: 10-01-2008, 04:50 PM
  2. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 2
    Last Post: 01-08-2008, 02:05 PM
  3. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 4
    Last Post: 01-02-2008, 11:20 AM
  4. Replies: 41
    Last Post: 07-04-2004, 03:23 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM