malloc() allocs memory already allocated to something else - help

This is a discussion on malloc() allocs memory already allocated to something else - help within the C Programming forums, part of the General Programming Boards category; Hi all, I'm trying to readin a 2D array of long integers, of unknown row and col size to start ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    2

    malloc() allocs memory already allocated to something else - help

    Hi all,

    I'm trying to readin a 2D array of long integers, of unknown row and col size to start with.

    Here is the relevant part of the code
    Code:
    ...
    ...
    long int **datacols; 
    
    colstofindcount=6; 
    datacols=(long int **)malloc(colstofindcount*sizeof(long int *)); 
    if (datacols==NULL){ 
    printf("Unable to allocate memory to datacols\n"); 
    exit(1); 
    } 
    colslen=26416; 
    for(i=0;i<noofcolstofind;i++){ 
    *(datacols+i)=(long int *)malloc(colslen*sizeof(long int)); 
    if (*(datacols+i)==NULL){ 
    printf("Unable to allocate memory\n"); 
    exit(1); 
    } 
    } /* end of for loop allocating memory to all cols*/
    Here is the memory map of what the compiler allocates


    datacols= 0x94f5968

    I expect datacols to be allocated 24 bytes as sizeof(long int) on my machine is 4 bytes

    So I expect the memory from 0x94f5968 to 0x94f598c to be allocated, as is shown below.

    (datacols+0) 0x94f5968
    (datacols+1) 0x94f596c
    (datacols+2) 0x94f5970
    (datacols+3) 0x94f5974
    (datacols+4) 0x94f5978
    (datacols+5) 0x94f597c

    If we see the blocks of memory allocated in the for loop, I see

    *(datacols+0) 0x94f5978
    *(datacols+1) 0x950f638
    *(datacols+2) 0x95292f8
    *(datacols+3) 0x9542fb8
    *(datacols+4) 0x955cc78
    *(datacols+5) 0x9576938

    Please note that the first block allocated starts at 0x94f5978 which has already been allocated for the malloc call before the for loop. The compiler for some reason seem to reserve only 12 bytes instead of 24.

    Can some one explain / help. I'm wondering what I'm doing wrong.

    I'm running gcc4.0.1 on Fedora Core 4
    I'm able to duplicate the same error on gcc running in Cygwin.
    Thanks
    Senthil

    PS: Please note that colstofindcount and colslen are being calculated at runtime, and I have just put in the numbers for this example.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    The compiler for some reason seem to reserve only 12 bytes instead of 24.
    0x94f5968 - 0x94f598c = 0x24 = 36 (decimal)

    0x94f5968 + 24 (decimal) = 0x94F5980.
    Last edited by Dave_Sinkula; 12-01-2005 at 03:27 PM. Reason: Removed code snippet.
    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.*

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    I don't have the same problem:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      int i;
      int nrows = 6, ncols = 26416;
      long int **data;
    
      data = malloc(sizeof(long int *) * nrows);
      printf("data = %p\n\n", data);
    
      for(i = 0;i < nrows;++i)
      {
        *(data + i) = malloc(sizeof(long int) * ncols);
        printf("*(data + %d) = %p\n", i, *(data + i));
      }
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./multimem
    data = 0x804a050
    
    *(data + 0) = 0x804a070
    *(data + 1) = 0x8063d38
    *(data + 2) = 0x807da00
    *(data + 3) = 0x80976c8
    *(data + 4) = 0x80b1390
    *(data + 5) = 0x80cb058
    0x804a070 - 0x804a050 = 0x20 = 32 (decimal)

    That's using gcc-3.4.4
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    2
    Firstly, thx everyone. Dave, I did the additon in hex mode when posting. Sorry ! about that.

    The problem is with the different variables I have used in the first malloc and the following for loop

    Code:
    datacols=(long int **)malloc(colstofindcount*sizeof(long int *)); 
    for(i=0;i<noofcolstofind;i++){
    Those two variables, should be the same, but they are not and have different values. During runtime, colstofindcount is zero, malloc just returns a valid pointer, but zero bytes are allocated.

    I'm sorry about the line which says
    Code:
    colstofindcount=6;
    I just added that during the original post, to make the question clearer, but I was not paying enough attention.

    Thanks again for all the help and suggestions

    Senthil

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. [C++/WinAPI] Checking number of allocated memory
    By jagi in forum C++ Programming
    Replies: 2
    Last Post: 03-28-2005, 05:10 PM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. Memory handler
    By Dr. Bebop in forum C Programming
    Replies: 7
    Last Post: 09-15-2002, 04:14 PM

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