Thread: A memory allocation error

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    74

    A memory allocation error

    Hi,

    I am experiencing a memory allocation error.

    I have two systems, the program compiles ok on both these systems.
    But, on one of the systems I receive a malloc error. Here is the text of the error:

    Code:
    threads: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
    Aborted
    Now, I write some pieces of code from my program.

    I am creating a thread. This thread is created in the following function.

    Code:
    // necessary structures, and variable declarations
    #define MAX_THREADS 2
    static rtl_pthread_t threadIds[MAX_THREADS];
    void* thread_function_1(void* s);
    
    signed char serversInit(unsigned short num_servers, unsigned long ec_period_reg, void* svs[]){
        int ret;
    ret = rtl_pthread_create( &threadIds[0], NULL, thread_function_1, svs[0]);
    if (ret != 0) {
     printf( "Error creating thread %d\n", (int)threadIds[0] );
    return -1;
    }
    
    return 0;
    }
    
    
    void* thread_function_1(void* sv){
        printf("\n says hello \n");
    }
    The function "serversInit" is called from another file that passes the arguments to this function. Please notice the fourth argument to the thread creation function. This argument is actually passed on to the thread function i.e. "thread_function_1". I think, there might be problem regarding how I initialize that structure and then pass it on to the "serversInit" function. I write the way I am doing it,

    Code:
    SERVER* svs[2];     // where SERVER is a structure
    
    svs[0] = (SERVER*)malloc(sizeof(SERVER));
    if(svs[0] == NULL){
            printf("\n Error Allocating Memory\n");
            return;
        }
    
    svs[1] = (SERVER*)malloc(sizeof(SERVER));
    if(svs[1] == NULL){
            printf("\n Error Allocating Memory\n");
            return;
        }
    
    // calling the "serversInit" function.
     serversInit(num_servers, 1000, svs);
    Please can you provide some help or inputs in this regard.

    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    On a wider scale, your allocated memory looks like this
    Code:
    +----+--------+-------+----+--------+-------+----+--------+-------+
    | CB | malloc | slack | CB | malloc | slack | CB | malloc | slack |
    +----+--------+-------+----+--------+-------+----+--------+-------+
          ^                     ^                     ^
    The control blocks (CB) are used to manage the memory pool as a whole, and typically contain links to other memory blocks, and the size of the current block. This is mainly to facilitate implementing free()
    For reasons such as buffer overrun, and memory pool fragmentation, there may be some slack space at the end of the block allocated, before there is another control block.

    The error message you have indicates that you've smashed a control block of some sort, and that's fatal.

    I would suggest you run valgrind on your program to attempt to locate the code which is doing the trashing.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Allocation Error - /w Valgrind Output
    By Ggregagnew in forum C Programming
    Replies: 5
    Last Post: 12-02-2010, 04:02 PM
  2. memory allocation error
    By prakash0104 in forum C Programming
    Replies: 1
    Last Post: 12-10-2008, 11:23 AM
  3. Memory allocation error
    By cunnus88 in forum C++ Programming
    Replies: 5
    Last Post: 01-25-2008, 04:24 PM
  4. HELP!!! Memory allocation error!
    By killiansman in forum C Programming
    Replies: 6
    Last Post: 06-08-2007, 11:47 AM
  5. Help on coding, possible memory allocation error.
    By NiHL in forum C Programming
    Replies: 1
    Last Post: 11-08-2004, 09:14 AM