mf_
mf_

Reputation: 625

why cant i manage ~382MB of memory when i have it available?

OBJECTIVE: manage a unsigned long tomBOLA[5][10000000];

$top gives me:

top - 14:05:35 up  4:06,  4 users,  load average: 0.46, 0.48, 0.44
Tasks: 182 total,   1 running, 180 sleeping,   1 stopped,   0 zombie
Cpu(s): 14.4%us,  2.4%sy,  0.0%ni, 82.5%id,  0.6%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   3092064k total,  1574460k used,  1517604k free,   168944k buffers
Swap:  1998840k total,        0k used,  1998840k free,   672756k cached

program has, a malloc the size of (5*10000000) * 8bytes =382MB, then fill with 0s and a read of what is stored in tomBOLA:

long int **tomBOLA;

if((tomBOLA=(long int **)malloc(5))==NULL){ /*MALLOC()*/
    printf("\n\tMEMORY ERROR-1");
    exit(1);
}
for(i=0;i<5;i++){
    if((tomBOLA[i]=(long int *)malloc(10000000*sizeof(long int)))==NULL){
        printf("\n\tMEMORY ERROR-2");
        exit(1);
    }
}                                          /*MALLOC()*/
for(i=0;i<5;i++){
    for(j=0;j<10000000;j++){
        tomBOLA[i][j]=0;
    }
}                                         /*FILL WITH 0s before using*/
for(i=0;i<5;i++){
    for(j=0;j<10000000;j++){
        printf("%ld ",tomBOLA[i][j]);
    }
printf("\n\n\n\n\n");
}                                         /*CONTROL OF 0s*/

gdb gives

Program received signal SIGSEGV, Segmentation fault.
_int_malloc (av=<value optimized out>, bytes=<value optimized out>) at malloc.c:4708
4708    malloc.c: No such file or directory.
    in malloc.c
(gdb) bt
#0  _int_malloc (av=<value optimized out>, bytes=<value optimized out>) at malloc.c:4708
#1  0x001dfd4c in *__GI___libc_malloc (bytes=40000000) at malloc.c:3660
#2  0x0804ca86 in main ()
(gdb) 

about memory, what am i doing or assuming wrong ??

Upvotes: 0

Views: 120

Answers (3)

nos
nos

Reputation: 229108

Your first malloc is wrong. You need to allocate enough memory for the pointers.

if((tomBOLA=(long int **)malloc(5))==NULL){ /*MALLOC()*/

should be

if((tomBOLA=(long int **)malloc(5 * sizeof *tomBOLA))==NULL){ /*MALLOC()*/

(sizeof *tomBOLA here would be the same as sizeof(long int *) in this case, but it follows the general pattern of SomeType *p = malloc(sizeof *p))

Upvotes: 2

unwind
unwind

Reputation: 399833

This:

tomBOLA=(long int **)malloc(5))

is wrong, it's allocating 5 bytes, leading to immediate buffer overrun when you treat it as an array of five pointers (needing probably 20 bytes). You meant:

tomBOLA = malloc(5 * sizeof *tomBOLA);

to get five pointers to unsigned long.

Note that there's no point in doing this dynamically, it's way simpler to just say:

unsigned long *tomBOLA[5];

Also:

  1. Get rid of the casts of malloc()'s return value.
  2. Stop using magical constants (the solution above makes the 5 go away in the rest of the code, since you can then use sizeof tomBOLA / sizeof *tomBOLA in place of 5).

Upvotes: 7

Bathsheba
Bathsheba

Reputation: 234715

You may well have that memory available but not in a contiguous block which is what the malloc call is trying to give you.

Upvotes: 0

Related Questions