Thread: memory allocation/deallocation

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    memory allocation/deallocation

    Hi,

    I'm trying to figure the error message that I get when trying to deallocate an array of pointers I'm allocating.
    Here's the allocation and free'ing lines:
    Code:
    CMyEdit *txtboxf[50];
    txtboxf[i] = new CMyEdit();
    
    free(txtboxf);
    in run-time I get an error that Bebud assertion failed! in File:dbgheap.c

    The same problem occures with this:
    Code:
    char line[80];
    
    free(line);
    I've read tutorials and I think I'm doing what they are telling me. How do I free arrays properly?
    Everything is relative...

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by earth_angel
    The same problem occures with this:
    Code:
    char line[80];
    
    free(line);
    I've read tutorials and I think I'm doing what they are telling me. How do I free arrays properly?
    You don't free arrays. You only free memory that was allocated using malloc/realloc/calloc.
    Last edited by Dave_Sinkula; 08-23-2005 at 12:17 PM. Reason: Added link.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Even if it's an array of pointers?
    Everything is relative...

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    A pointer is not an array. In your examples, line is an array; txtboxf is an array of pointers (txtboxf[i] would be one such pointer, for example). You can free the memory allocated to each of the pointers. But you can't free an array.

    And don't mix new and free.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    >> And don't mix new and free.
    I checked for that.

    Thanks.

    AS
    Everything is relative...

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What happened to using vectors?

  7. #7
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    just to check. If I have something like this:
    Code:
    Params = (char **)calloc(NumParam, sizeof(char *));
    
       for(i=0; i<NumParam; ++i)
    	   Params[i] = (char*)calloc(NameLengthP , sizeof(char));
    I would free(Params[i]) first and then free(params), right?
    Everything is relative...

  8. #8
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    >> What happened to using vectors?

    Nothing, why?
    Everything is relative...

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I remember earlier posts of yours that used vectors. I was wondering why you weren't using a vector in this case. You wouldn't have to worry about new/delete or free (or malloc or calloc).

    You seem to be mixing C and C++ style. You should probably pick one and stick with it unless you have a specific reason not to.

  10. #10
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I use vector for lists of unknown size. These guys I know exactly how big they are dynamically.

    C and C++, I started writign in C, but then I needed a GUI. So I had to use the code as event handlers for VC++ controls. I didn't want to re-write it all in C++. There was too much. I do try to keep it in line as use as little C as possible now, and I usually don't.
    Everything is relative...

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Ok. Fair enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  4. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  5. What's the best memory (RAM) type?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 12-15-2001, 12:37 AM