Search code examples
cpointersmallocfree

Malloc 2D pyramid array in C


I'm having trouble creating a 2D array where every "row" would be different size array, with (2*row)+1 integers starting with row=0.

The thing that troubles me it that the code work for some small inputs (1,2,3) but then crashes on 4 if I try to free the structure after printing or on 5 if I don't free the memory.

typedef struct{
    int nrow;
    int** numbers;
} pyramid;

pyramid* create_pyramid(int nrow){
    //Allocate memory for pyramid
    pyramid *p = (pyramid *) malloc (sizeof(pyramid));
    if(p == NULL) exit(1);

    p->nrow = nrow;

    //Allocate memory for numbers
    p->numbers = (int**) malloc (sizeof(int*));
    if(p->numbers == NULL) exit(1);

    //Alocate memory for numbers[i]
    int i, nums;
    for(i = 0; i < nrow; i++){
        nums = (i*2) + 1;

        p->numbers[i] = (int*) malloc (nums * sizeof(int));
        if(p->numbers[i] == NULL) exit(1);
    }

    return p;
}

void print_pyramid(pyramid *p){
    int i, j, nums, spaces;
    for(i = 0; i < p->nrow; i++){
        nums = (2*i)+1;
        spaces = p->nrow - (i+1);
        for(j = 0; j < spaces; j++) printf(" ");
        for(j = 0; j < nums; j++){
            printf("%d",p->numbers[i][j]);
        }
        printf("\n");
    }
}

void free_pyramid(pyramid *p){
    int i;
    for(i = 0; i < p->nrow; i++){
        free(p->numbers[i]);
    }
    free(p->numbers);
    free(p);
}

int main(int argc, char *argv[]){
    if(argc<2) exit(1);

    int nrow; sscanf(argv[1], "%d", &nrow);
    pyramid *p = create_pyramid(nrow);
    print_pyramid(p);
    free_pyramid(p);
    exit(0);
}

This is what I get when running this code with command line arguments 0-4:

userT@userT-VirtualBox:~/Desktop/pyr$ ./a.out 1
0
userT@userT-VirtualBox:~/Desktop/pyr$ ./a.out 2
 0
000
userT@userT-VirtualBox:~/Desktop/pyr$ ./a.out 3
  0
 000
00000
userT@userT-VirtualBox:~/Desktop/pyr$ ./a.out 4
   0
  000
 00000
0000000
*** Error in `./a.out': double free or corruption (out): 0x0000000001441050 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7fcc5a32d7e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x7fe0a)[0x7fcc5a335e0a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7fcc5a33998c]
./a.out[0x400887]
./a.out[0x40092a]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fcc5a2d6830]
./a.out[0x4005e9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:01 38534                              /home/userT/Desktop/pyr/a.out
00600000-00601000 r--p 00000000 08:01 38534                              /home/userT/Desktop/pyr/a.out
00601000-00602000 rw-p 00001000 08:01 38534                              /home/userT/Desktop/pyr/a.out
01441000-01462000 rw-p 00000000 00:00 0                                  [heap]
7fcc54000000-7fcc54021000 rw-p 00000000 00:00 0 
7fcc54021000-7fcc58000000 ---p 00000000 00:00 0 
7fcc5a0a0000-7fcc5a0b6000 r-xp 00000000 08:01 7136                       /lib/x86_64-linux-gnu/libgcc_s.so.1
7fcc5a0b6000-7fcc5a2b5000 ---p 00016000 08:01 7136                       /lib/x86_64-linux-gnu/libgcc_s.so.1
7fcc5a2b5000-7fcc5a2b6000 rw-p 00015000 08:01 7136                       /lib/x86_64-linux-gnu/libgcc_s.so.1
7fcc5a2b6000-7fcc5a475000 r-xp 00000000 08:01 7098                       /lib/x86_64-linux-gnu/libc-2.23.so
7fcc5a475000-7fcc5a675000 ---p 001bf000 08:01 7098                       /lib/x86_64-linux-gnu/libc-2.23.so
7fcc5a675000-7fcc5a679000 r--p 001bf000 08:01 7098                       /lib/x86_64-linux-gnu/libc-2.23.so
7fcc5a679000-7fcc5a67b000 rw-p 001c3000 08:01 7098                       /lib/x86_64-linux-gnu/libc-2.23.so
7fcc5a67b000-7fcc5a67f000 rw-p 00000000 00:00 0 
7fcc5a67f000-7fcc5a6a5000 r-xp 00000000 08:01 7070                       /lib/x86_64-linux-gnu/ld-2.23.so
7fcc5a887000-7fcc5a88a000 rw-p 00000000 00:00 0 
7fcc5a8a1000-7fcc5a8a4000 rw-p 00000000 00:00 0 
7fcc5a8a4000-7fcc5a8a5000 r--p 00025000 08:01 7070                       /lib/x86_64-linux-gnu/ld-2.23.so
7fcc5a8a5000-7fcc5a8a6000 rw-p 00026000 08:01 7070                       /lib/x86_64-linux-gnu/ld-2.23.so
7fcc5a8a6000-7fcc5a8a7000 rw-p 00000000 00:00 0 
7ffc634cc000-7ffc634ed000 rw-p 00000000 00:00 0                          [stack]
7ffc635d6000-7ffc635d8000 r--p 00000000 00:00 0                          [vvar]
7ffc635d8000-7ffc635da000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)

And this if what I get if I don't call the free_pyramid function at the end of main:

userT@userT-VirtualBox:~/Desktop/pyr$ ./a.out
userT@userT-VirtualBox:~/Desktop/pyr$ ./a.out 1
0
userT@userT-VirtualBox:~/Desktop/pyr$ ./a.out 2
 0
000
userT@userT-VirtualBox:~/Desktop/pyr$ ./a.out 3
  0
 000
00000
userT@userT-VirtualBox:~/Desktop/pyr$ ./a.out 4
   0
  000
 00000
0000000
userT@userT-VirtualBox:~/Desktop/pyr$ ./a.out 5
    39268576
   000
  00000
 0000000
000000000

Solution

  • Just like you do in

    p->numbers[i] = (int*) malloc (nums * sizeof(int));
    

    you need to allocate memory for an array of variables, not only one variable. So this line:

    p->numbers = (int**) malloc (sizeof(int*));
    

    should be

    p->numbers = (int**) malloc (nrow * sizeof(int*));
    

    In both cases (int*) and (int**) are not needed.