4

So, I'm having trouble in allocating memory for a char *** type variable. My objective is to create a matrix of strings and the code I currently have for memory allocation is the following:

char ***matrix;

matrix = calloc(n*MAX_STR, sizeof(char**));
for(z = 0; z < n; z++) {
    matrix[z] = calloc(n, sizeof(char*));
    for(i = 0; i < MAX_STR; i++) {
        matrix[z][i] = calloc(MAX_STR, sizeof(char));
    }
}

I have successfully allocated memory for an array of strings, using this:

char **list;
list = calloc(n, sizeof(char *));
for (j = 0; j < n; j++){
list[j] = calloc(MAX_STR, sizeof(char));
}

but I'm now having problems with the matrix.

Running the program with --leak-check=full on Valgrind gives me the following message:

==5126== Invalid write of size 8
==5126==    at 0x400B9F: createmat (proj.c:100)
==5126==    by 0x401598: main (proj.c:237)
==5126==  Address 0x5210878 is 0 bytes after a block of size 72 alloc'd
==5126==    at 0x4C2ABB4: calloc (vg_replace_malloc.c:593)
==5126==    by 0x400B52: createmat (proj.c:98)
==5126==    by 0x401598: main (proj.c:237)

I'd like to figure out out to allocate memory for this, since I'm still a beginner when it comes to memory management in C. Any help would be appreciated, thanks.

EDIT: The matrix is supposed to store n arrays of strings, which correspond to the lines of the input (it's read with fgets later), and each array allocates whichever number of words the line has, with each word (read, each string) having at max a MAX_STR number of characters. n is a variable read from the input, while MAX_STR is a constant defined in the program.

Dusk252
  • 187
  • 3
  • 7
  • There's not much point in using `calloc` rather than `malloc`. `calloc` initializes the allocated space to all-bits-zero. This is *not* guaranteed to be the representation of a null pointer (though it commonly is). Suggestion: just use `malloc`, and be careful not to refer to the value of any pointer that you haven't explicitly initialized. – Keith Thompson May 13 '13 at 15:45

3 Answers3

7

Matrix of String or/ 3D char array:

Suppose you need N matrices, each matrix can store R strings of length MAX_STR-1 then you should allocated memory your loop as follows, like:

char ***matrix;

matrix = calloc(N, sizeof(char**)); 
for(z = 0; z < N; z++) { 
    matrix[z] = calloc(R, sizeof(char*));
    for(i = 0; i < R; i++) {
        matrix[z][i] = calloc(MAX_STR, sizeof(char));
    }
}

Its will create matrix like:

matrix
+-------------------+------------------+-----------------------+
| 0                 | 1                | 2                     |
+-------------------+------------------+-----------------------+        
 |                           |                        |
 ▼                           ▼                        ▼
+--+    +----------+       +--+    +----------+     +--+    +----------+
|0 +---►| MAX_STR  |       |0 +---►| MAX_STR  |     |0 +---►| MAX_STR  |
+--+    +----------+       +--+    +----------+     +--+    +----------+
|1 +---►| MAX_STR  |       |1 +---►| MAX_STR  |     |1 +---►| MAX_STR  |
+--+    +----------+       +--+    +----------+     +--+    +----------+
|2 +---►| MAX_STR  |       |2 +---►| MAX_STR  |*    |2 +---►| MAX_STR  |
+--+    +----------+       +--+    +----------+     +--+    +----------+
|3 +---►| MAX_STR  |       |3 +---►| MAX_STR  |     |3 +---►| MAX_STR  |
+--+    +----------+       +--+    +----------+     +--+    +----------+
|4 +---►| MAX_STR  |       |4 +---►| MAX_STR  |     |4 +---►| MAX_STR  |
+--+    +----------+       +--+    +----------+     +--+    +----------+
|5 +---►| MAX_STR  |       |5 +---►| MAX_STR  |     |5 +---►| MAX_STR  |
+--+    +----------+       +--+    +----------+     +--+    +----------+
|6 +---►| MAX_STR  |       |6 +---►| MAX_STR  |     |6 +---►| MAX_STR  |
+--+    +----------+       +--+    +----------+     +--+    +----------+
|7 +---►| MAX_STR  |       |7 +---►| MAX_STR  |     |7 +---►| MAX_STR  |
+--+    +----------+       +--+    +----------+     +--+    +----------+
 ^        ^ 
 |        |
 |        matrix[z][i]
 matrix[z]

Here N = 3, and
R = 8

Its char 3D array of size matrix[N][R][MAX_STR]

Suppose, if someone wants to printf string I marked * in diagram, that is third string in second array, then he/she need to index like

printf("%s",matrix[1][2]);

Although answer is accepted I am updating my answer so onw can find it helpful in future

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
5

Assuming you want to allocate storage for n arrays, each with n strings, each up to MAX_STR long, there are a couple of mistakes in the code

matrix = calloc(n*MAX_STR, sizeof(char**));

should be

matrix = calloc(n, sizeof(char**));

and

for(i = 0; i < MAX_STR; i++) {

should be

for(i = 0; i < n; i++) {

In a little more detail,

matrix = calloc(n*MAX_STR, sizeof(char**));
for(z = 0; z < n; z++) {

seems wrong. You allocate n*MAX_STR elements but only use n of them

matrix[z] = calloc(n, sizeof(char*));
for(i = 0; i < MAX_STR; i++) {

is also questionable and is wrong for n<MAX_STR. (You allocate n elements then write to MAX_STR of them.)

Finally, depending on whether you consider MAX_STR to include space for a null terminator, you may need to change

matrix[z][i] = calloc(MAX_STR, sizeof(char));

to

matrix[z][i] = calloc(MAX_STR+1, 1);
simonc
  • 41,632
  • 12
  • 85
  • 103
  • `n` is the number of lines from the input while `MAX_STR` is the number of characters allowed in a line. So basically, the second dimension of the matrix would have a max lenght of `MAX_STR`, being that it's supposedly divided by words inside it. So I don't really get why I should change the loop condition. – Dusk252 May 13 '13 at 12:34
  • I want to allocate `n` arrays, each with an arbitrary number of strings and each with up to `MAX_STR` length. (and thanks for your help so far) – Dusk252 May 13 '13 at 12:36
  • @Dusk252 I'm not sure I follow. If my ...more detail... section doesn't help, can you update your question to say exactly what you're trying to store please – simonc May 13 '13 at 12:36
  • Your second comment overlapped my question and helped clarify things for me. I think my suggested changes do what you want. Can you give them a try please? The second change is probably key. – simonc May 13 '13 at 12:39
2

As you have said, you were able to successfully create an array of strings using your below code:

char **list;
list = calloc(n, sizeof(char *));
for (j = 0; j < n; j++){
list[j] = calloc(MAX_STR, sizeof(char));
}

Now, you need an array of array of strings, so it should be:

char ***matrix;

matrix = calloc(n, sizeof(char**)); //This creates space for storing the address of 'n' array of strings 
for(z = 0; z < n; z++) { //This loop creates the 'n' array of strings.
    matrix[z] = calloc(n, sizeof(char*));
    for(i = 0; i < n; i++) {
        matrix[z][i] = calloc(MAX_STR, sizeof(char));
    }
}

So, basically, in the second code, you are just creating space for storing 'n' lists. Hope this makes it clear.

Jay
  • 24,173
  • 25
  • 93
  • 141