Is there a limit on the number of malloc calls ?

This is a discussion on Is there a limit on the number of malloc calls ? within the Windows Programming forums, part of the Platform Specific Boards category; I'm having trouble with dynamic memory allocation. Using VC (toolkit 2003) on XP and programming C (not C++), I'm using ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Location
    Belgium
    Posts
    8

    Is there a limit on the number of malloc calls ?

    I'm having trouble with dynamic memory allocation. Using VC (toolkit 2003) on XP and programming C (not C++), I'm using a lot of malloc/free function calls.

    All went fine until today. At a certain point in the program, it crashes on the malloc() function. It doesn't really matter in what function malloc() is called or what specific pointer is being allocated... if I remove the malloc(), the program crashes on the next malloc(). And the same malloc() works fine in a previous call to the function containing the malloc/free functions...

    I thought I had reached a memory limit, but when I decrease one of the first mallocs (which is always succesful) from about 500000 Bytes to 50000 bytes, the program still crashes at the same later malloc() (which only claims for about 500 bytes).

    So I was wondering if there is a limit on the number of calls to malloc ? And how can this be avoided ?

    What should I do ??

    Thanks !

  2. #2
    Registered User joed's Avatar
    Join Date
    Mar 2004
    Posts
    59
    try ensuring the pointers are appropriately dead or active before using malloc or free:

    Code:
    int *p;
    
    if(!p)
        malloc...
    
    if(p)
        free...
    also, in some cases you may check p in the same way before writing to it:

    Code:
    if(p)
        write_stuff...
    else
        error...

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    > but when I decrease one of the first mallocs (which is always succesful) from about 500000 Bytes to 50000 bytes,
    Just how much memory are you allocating?
    I mean, a few hundred rounds of 5MB a time will soon eat up all the memory in your machine.

    Do you get any specific error message when it crashes - like trying to access address 0 perhaps?
    Do you check that malloc was successful in all cases?

    The most common cause for malloc failures is mis-use of the memory from some previous malloc, most commonly referred to as a buffer overflow. If memory has been corrupted, then malloc is usually the first thing to fail as a result.
    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.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Location
    Belgium
    Posts
    8
    I have to admit that I don't really understand the exception dump information, but the first three lines are:

    Exception Information
    Code: 0xc0000005 Flags: 0x00000000
    Record: 0x0000000000000000 Address: 0x0000000077f481bd

    The amount of memory I try to allocate throughout the program is large, but not huge - the example of 500Kb only occurs in one place.

    But I will check all malloc/free code in the program now, perhaps there is some dirty stuff in it - although I was always coding these things quite clean. It's a huge codebase, so I'll need hours to do this checking...

    So far I assume there are no compiler settings that could have caused or that can prevent this problem (?).

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Shortest path algorithm with a maximum number of edges limit
    By blackslither in forum C Programming
    Replies: 4
    Last Post: 12-28-2008, 03:49 PM
  3. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  4. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  5. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 01:53 PM

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