Memory Allocation While using malloc....

In my win32 console application using the Microsoft 'C' Compiler, I am using malloc to allocate memory in various places. It is working very fine, but sometimes, malloc failed to allocate memory of given size. The allocated Pointer variable has no memory and I am getting the "Unhandled Exception". The control flow stops at the _heap.c(system file).

Could you please tell me in what are all the cases Malloc will get fail?
How could we rectify this issue as a permanent?

Also in some cases, while I am deallocating the dynamically allocated variable using the free(), getting assertion failed Error. Initially the Pointer variable is declared as NULL.
Could you please tell me why it is happening and how to resolve this issue.

Earliest replies are appreciated.
Thanks in advance.

Regards,
Shahul.

Comments

  • : In my win32 console application using the Microsoft 'C' Compiler, I am using malloc to allocate memory in various places. It is working very fine, but sometimes, malloc failed to allocate memory of given size. The allocated Pointer variable has no memory and I am getting the "Unhandled Exception". The control flow stops at the _heap.c(system file).
    :
    : Could you please tell me in what are all the cases Malloc will get fail?
    : How could we rectify this issue as a permanent?
    :
    : Also in some cases, while I am deallocating the dynamically allocated variable using the free(), getting assertion failed Error. Initially the Pointer variable is declared as NULL.
    : Could you please tell me why it is happening and how to resolve this issue.
    :
    : Earliest replies are appreciated.
    : Thanks in advance.
    :
    : Regards,
    : Shahul.
    :
    :
    [blue]malloc() can fail if:

    1. There is no more memory left in the system to allocate - all used up. If your code does not allocate megabytes of memory, look for loops where the small pieces of memory allocated and make sure there are no leaks - meaning, you free all blocks you allocate.

    2. You pass the incorrect pointer back to free() - this corrupts the heap and malloc() no longer functions properly. That means you cannot move (modify) the pointer which is returned by malloc(). Example:

    [code]
    char* p = malloc (1200);

    p += 100; // Move address forward 100 symbols
    ...
    free (p); // WRONG! Heap may be corrupted
    [/code]

    3. Your code writes data beyond the allocated room. Example:

    [code]
    char* p = malloc (6); // Get 6 bytes

    strcpy (p, "My code has bugs!");

    // This ^^^ will copy into the block more than 6 bytes and this may
    // corrupt the heap internal structure.
    [/code]

    Not every heap is prone to these kind of problems. Also, some languages, like JAVA or C# will not have these problems, because memory is allocated by the language automatically for every object as needed.
    [/blue]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion