help on free()

This is a discussion on help on free() within the C Programming forums, part of the General Programming Boards category; For eg: when a list is created using linked list with head holding the first pointer to the structure and ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    11

    help on free()

    For eg: when a list is created using linked list with head holding the first pointer to the structure and followed by the next pointers to the structure.

    head->elemt1->elemt2->elemt3

    after creating the above list using link list and some operations done,

    if head is assigned NULL, in order to free or delete the memory space allocated,

    1.is this a right way of doing it.
    2.Will the program crash

    thank you

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Depends on how each node was allocated. But if malloc() (or friends) was used then,

    1. No
    2. No
    Last edited by zacs7; 11-03-2008 at 11:14 PM.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    11
    what if when calloc() was used to allocate

    thank you

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Same thing, no and no.

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    11
    One more query on allocating and deleting

    when i used malloc to allocate space for huge pointer

    Code:
    typedef struct FileHdr
    {
    		unsigned long  reg;
    		unsigned short width;
    		unsigned char  typ;
    		unsigned char  unit;
    		char huge       str[40];
    		unsigned short offset;
    		unsigned char  scale;
    		float	       sfactor;
    		struct FileHdr huge *next;
    }FileHdr;
    FileHdr huge	*Hdrhead=NULL,huge *Hdrcur=NULL;
    //
    //
    Hdrhead=(FileHdr huge	*)malloc(sizeof(FileHdr));
    //some operation
    free(Hdrhead);
    when i try to free huge pointer i get a warning stating suspicious pointer conversion
    is there any other best way to free it.

    thanks

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is probably because your pointer is huge, and free doesn't take a huge pointer - but that is only a guess - there could be other problems. My question would be why you are using a compiler that is at least 10 years old.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    11
    am developing a exe for dos based machine(Hand Held Unit) which retrieves data from micro-controller based device.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by supi View Post
    am developing a exe for dos based machine(Hand Held Unit) which retrieves data from micro-controller based device.
    Poor you. At least it's a better reason than some I've heard (such as "I haven't got any other" or "It's what my school uses").

    I suspect, but don't know for sure (and online docs are sparse) that you need a special "huge free".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    Quote Originally Posted by matsp View Post
    My question would be why you are using a compiler that is at least 10 years old.
    A question here. How do you understand how old is a compiler ?
    I suppose from warning, or something else ?

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Because in modern compilers, there are no "huge" pointers. There is only one type, normal pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > when i try to free huge pointer i get a warning stating suspicious pointer conversion
    And I'd say you'd get the same on the malloc as well, if the cast wasn't there.

    Check your documentation for fmalloc / hmalloc / xmalloc or whatever for a model-specific allocation routine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  2. Help needed with backtracking
    By sjalesho in forum C Programming
    Replies: 1
    Last Post: 11-09-2003, 05:28 PM
  3. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM
  4. SIGABRT upon free()
    By registering in forum C Programming
    Replies: 2
    Last Post: 07-19-2003, 07:52 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21