2

Why when i do this i get a segfault ? I'm just giving the value 5 to the first place of my int array ?

#include <stdlib.h>
int main()
{
int     **tab;
int     i;
int     j;

i = 0;
j = 0;
tab = (int **)malloc(sizeof(int *) * 100);
tab[i][j] = 5;
return(0);
}

4 Answers4

2

Because you have not allocated your memory properly. What you actually have done is allocate only the rows of your table. You have not allocated any space for the columns, but you still try to access them and this will cause undefined behavior. In order to fix it, replace the part :

tab = (int **)malloc(sizeof(int *) * 100);
tab[i][j] = 5;

with :

tab = malloc(sizeof(int *) * 100);
for (i = 0; i < 100; i++)
    tab[i] = malloc(sizeof(int) * NUM) ;   //where NUM is the size you want to allocate
tab[i][j] = 5;

Take a look at this link to understand how the memory allocation for pointer to pointer works.


Also, see why you should not cast the result of malloc.

Community
  • 1
  • 1
Marievi
  • 4,951
  • 1
  • 16
  • 33
  • For 2D arrays, using this method is always a bad idea. [See this](http://stackoverflow.com/a/32050859/584518). Also the link you provided goes to some hobbyist amateur site... I wouldn't look at it. – Lundin Jun 09 '16 at 10:50
  • 1
    @Lundin the OP used dynamic memory allocation so I continued his approach. As for the link, it may be amateur but I find it very simple to understand and explanatory. – Marievi Jun 10 '16 at 06:10
  • I haven't said that he shouldn't use dynamic allocation, I said that he shouldn't allocate a segmented pointer-to-pointer thing scattered all over the heap, for no reason. While he just wants a 2D array. The link may be easy to understand, but **the contents are incorrect and teaches bad practice**. Again, please [read this](http://stackoverflow.com/questions/32050256/function-to-dynamically-allocate-matrix/32050859#32050859). – Lundin Jun 10 '16 at 06:49
  • @Lundin thanks for posting me an answer of yours, will take a look. – Marievi Jun 10 '16 at 06:51
2

The memory could be allocated with this. Check the return of malloc and when no longer needed, free the memory.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int row = 23;
    int col = 100;
    int i = 0;
    int j = 0;
    int (*tab)[col] = NULL;
    if ( ( (tab) = malloc( sizeof(*tab) * row)) == NULL) {
        printf ( "malloc failed\n");
        return 1;
    }
    tab[i][j] = 5;
    free ( tab);
    return 0;
}
user3121023
  • 8,181
  • 5
  • 18
  • 16
1

You allocating the first part (rows) of your "matrix"

You must allocate the columns then, e.g.

#define ROWS 100
#define COLS 100

tab = malloc(sizeof(int *) * ROWS);
for (i = 0; i < ROWS; i++)
    tab[i] = malloc(sizeof(int) * COLS) ;   
tab[i][j] = 5;

And free all at the end in reverse order

for (i = 0; i < ROWS; i++)
    free(tab[i]);
free(tab);
LPs
  • 16,045
  • 8
  • 30
  • 61
  • For 2D arrays, using this method is always a bad idea. [See this](http://stackoverflow.com/a/32050859/584518). – Lundin Jun 09 '16 at 10:49
  • @Lundin Surely +1 by me, but OP didn't say it wants a 2D matrix, I guessed it. I based my answer to pointer to pointer allocation. I mean: "matrix" with variable columns width per row. ;) – LPs Jun 09 '16 at 12:16
  • The important part here is that we have to stop people from teaching this pointer to pointer method. Most newbies abuse it when they really just need a 2D array, which is complete nonsense. – Lundin Jun 09 '16 at 12:36
  • @Lundin Well, I agree. I'll apply that philosophy in future. – LPs Jun 09 '16 at 12:37
0

tab[i] is the same as *(tab + i), but the values in your array are uninitialized, so you are attempting to dereference an uninitialized pointer, which produces undefined behaviour.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084