Characters matrix written in a binary file












0















I have a text file from which I read words. After that, I have to write in a binary file each word and near it the row and column where it appears. At _strdup(p) my programm crashes. Does anyone know why? I would appreciate your help. Here is the code:



void create(const char *filename, const char ****matrix) {

FILE *u, *f;
u = fopen(filename, "wb");
assert(u != NULL);
f = fopen("in.txt", "r");
assert(f != NULL);
(*matrix) = (char ***)malloc(sizeof(char **) * 1);
int i = 0;
int j=0; char buff[1024];
while (fgets(buff, 1024, f)!=NULL) {

(*matrix) = realloc((*matrix), (i + 1) * sizeof(char **));
char *p = strtok(buff, " ().,");
(*matrix)[i] = (char **)malloc(sizeof(char *));
while (p) {

(*matrix)[i] = (char **)realloc((*matrix)[i], sizeof(char *)*(j + 1));
strcpy((*matrix)[i], buff);
(*matrix)[i][j] = _strdup(p);

fwrite((*matrix)[i][j], sizeof(char *), 1, u);
fwrite(&i, sizeof(int), 1, u);
fwrite(&i, sizeof(int), 1, u);
j++;

(*matrix)[i][j] = NULL;

p = strtok(NULL, " ().,");
}
(*matrix)[i] = NULL;
i++;
printf("n");

}
fclose(u);
fclose(f);
}









share|improve this question

























  • Why are you writing pointer values to a file (at fwrite((*matrix)[i][j], sizeof(char *), 1, u);)?

    – Ian Abbott
    Jan 2 at 11:20











  • You probably need to set j = 0; inside the outer while loop, before the start of the inner while loop.

    – Ian Abbott
    Jan 2 at 11:22











  • (*matrix)[i][j] = NULL; and (*matrix)[i] = NULL; leak memory.

    – Ian Abbott
    Jan 2 at 11:24













  • Just before the string duplicate call, you have allocated memory of sizeof(char *)*(j + 1));, where j is 0. So, 1 char. Surely you need to check how long your strings are?

    – doctorlove
    Jan 2 at 11:32











  • @doctorlove I think that is just extending the array of char pointers by 1. The allocation for the string is done by the _strdup call and the returned pointer is stored in the newly created space.

    – Ian Abbott
    Jan 2 at 11:37
















0















I have a text file from which I read words. After that, I have to write in a binary file each word and near it the row and column where it appears. At _strdup(p) my programm crashes. Does anyone know why? I would appreciate your help. Here is the code:



void create(const char *filename, const char ****matrix) {

FILE *u, *f;
u = fopen(filename, "wb");
assert(u != NULL);
f = fopen("in.txt", "r");
assert(f != NULL);
(*matrix) = (char ***)malloc(sizeof(char **) * 1);
int i = 0;
int j=0; char buff[1024];
while (fgets(buff, 1024, f)!=NULL) {

(*matrix) = realloc((*matrix), (i + 1) * sizeof(char **));
char *p = strtok(buff, " ().,");
(*matrix)[i] = (char **)malloc(sizeof(char *));
while (p) {

(*matrix)[i] = (char **)realloc((*matrix)[i], sizeof(char *)*(j + 1));
strcpy((*matrix)[i], buff);
(*matrix)[i][j] = _strdup(p);

fwrite((*matrix)[i][j], sizeof(char *), 1, u);
fwrite(&i, sizeof(int), 1, u);
fwrite(&i, sizeof(int), 1, u);
j++;

(*matrix)[i][j] = NULL;

p = strtok(NULL, " ().,");
}
(*matrix)[i] = NULL;
i++;
printf("n");

}
fclose(u);
fclose(f);
}









share|improve this question

























  • Why are you writing pointer values to a file (at fwrite((*matrix)[i][j], sizeof(char *), 1, u);)?

    – Ian Abbott
    Jan 2 at 11:20











  • You probably need to set j = 0; inside the outer while loop, before the start of the inner while loop.

    – Ian Abbott
    Jan 2 at 11:22











  • (*matrix)[i][j] = NULL; and (*matrix)[i] = NULL; leak memory.

    – Ian Abbott
    Jan 2 at 11:24













  • Just before the string duplicate call, you have allocated memory of sizeof(char *)*(j + 1));, where j is 0. So, 1 char. Surely you need to check how long your strings are?

    – doctorlove
    Jan 2 at 11:32











  • @doctorlove I think that is just extending the array of char pointers by 1. The allocation for the string is done by the _strdup call and the returned pointer is stored in the newly created space.

    – Ian Abbott
    Jan 2 at 11:37














0












0








0








I have a text file from which I read words. After that, I have to write in a binary file each word and near it the row and column where it appears. At _strdup(p) my programm crashes. Does anyone know why? I would appreciate your help. Here is the code:



void create(const char *filename, const char ****matrix) {

FILE *u, *f;
u = fopen(filename, "wb");
assert(u != NULL);
f = fopen("in.txt", "r");
assert(f != NULL);
(*matrix) = (char ***)malloc(sizeof(char **) * 1);
int i = 0;
int j=0; char buff[1024];
while (fgets(buff, 1024, f)!=NULL) {

(*matrix) = realloc((*matrix), (i + 1) * sizeof(char **));
char *p = strtok(buff, " ().,");
(*matrix)[i] = (char **)malloc(sizeof(char *));
while (p) {

(*matrix)[i] = (char **)realloc((*matrix)[i], sizeof(char *)*(j + 1));
strcpy((*matrix)[i], buff);
(*matrix)[i][j] = _strdup(p);

fwrite((*matrix)[i][j], sizeof(char *), 1, u);
fwrite(&i, sizeof(int), 1, u);
fwrite(&i, sizeof(int), 1, u);
j++;

(*matrix)[i][j] = NULL;

p = strtok(NULL, " ().,");
}
(*matrix)[i] = NULL;
i++;
printf("n");

}
fclose(u);
fclose(f);
}









share|improve this question
















I have a text file from which I read words. After that, I have to write in a binary file each word and near it the row and column where it appears. At _strdup(p) my programm crashes. Does anyone know why? I would appreciate your help. Here is the code:



void create(const char *filename, const char ****matrix) {

FILE *u, *f;
u = fopen(filename, "wb");
assert(u != NULL);
f = fopen("in.txt", "r");
assert(f != NULL);
(*matrix) = (char ***)malloc(sizeof(char **) * 1);
int i = 0;
int j=0; char buff[1024];
while (fgets(buff, 1024, f)!=NULL) {

(*matrix) = realloc((*matrix), (i + 1) * sizeof(char **));
char *p = strtok(buff, " ().,");
(*matrix)[i] = (char **)malloc(sizeof(char *));
while (p) {

(*matrix)[i] = (char **)realloc((*matrix)[i], sizeof(char *)*(j + 1));
strcpy((*matrix)[i], buff);
(*matrix)[i][j] = _strdup(p);

fwrite((*matrix)[i][j], sizeof(char *), 1, u);
fwrite(&i, sizeof(int), 1, u);
fwrite(&i, sizeof(int), 1, u);
j++;

(*matrix)[i][j] = NULL;

p = strtok(NULL, " ().,");
}
(*matrix)[i] = NULL;
i++;
printf("n");

}
fclose(u);
fclose(f);
}






c matrix character






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 13:59









Joey Mallone

2,26561933




2,26561933










asked Jan 2 at 11:12









AndreAndre

1




1













  • Why are you writing pointer values to a file (at fwrite((*matrix)[i][j], sizeof(char *), 1, u);)?

    – Ian Abbott
    Jan 2 at 11:20











  • You probably need to set j = 0; inside the outer while loop, before the start of the inner while loop.

    – Ian Abbott
    Jan 2 at 11:22











  • (*matrix)[i][j] = NULL; and (*matrix)[i] = NULL; leak memory.

    – Ian Abbott
    Jan 2 at 11:24













  • Just before the string duplicate call, you have allocated memory of sizeof(char *)*(j + 1));, where j is 0. So, 1 char. Surely you need to check how long your strings are?

    – doctorlove
    Jan 2 at 11:32











  • @doctorlove I think that is just extending the array of char pointers by 1. The allocation for the string is done by the _strdup call and the returned pointer is stored in the newly created space.

    – Ian Abbott
    Jan 2 at 11:37



















  • Why are you writing pointer values to a file (at fwrite((*matrix)[i][j], sizeof(char *), 1, u);)?

    – Ian Abbott
    Jan 2 at 11:20











  • You probably need to set j = 0; inside the outer while loop, before the start of the inner while loop.

    – Ian Abbott
    Jan 2 at 11:22











  • (*matrix)[i][j] = NULL; and (*matrix)[i] = NULL; leak memory.

    – Ian Abbott
    Jan 2 at 11:24













  • Just before the string duplicate call, you have allocated memory of sizeof(char *)*(j + 1));, where j is 0. So, 1 char. Surely you need to check how long your strings are?

    – doctorlove
    Jan 2 at 11:32











  • @doctorlove I think that is just extending the array of char pointers by 1. The allocation for the string is done by the _strdup call and the returned pointer is stored in the newly created space.

    – Ian Abbott
    Jan 2 at 11:37

















Why are you writing pointer values to a file (at fwrite((*matrix)[i][j], sizeof(char *), 1, u);)?

– Ian Abbott
Jan 2 at 11:20





Why are you writing pointer values to a file (at fwrite((*matrix)[i][j], sizeof(char *), 1, u);)?

– Ian Abbott
Jan 2 at 11:20













You probably need to set j = 0; inside the outer while loop, before the start of the inner while loop.

– Ian Abbott
Jan 2 at 11:22





You probably need to set j = 0; inside the outer while loop, before the start of the inner while loop.

– Ian Abbott
Jan 2 at 11:22













(*matrix)[i][j] = NULL; and (*matrix)[i] = NULL; leak memory.

– Ian Abbott
Jan 2 at 11:24







(*matrix)[i][j] = NULL; and (*matrix)[i] = NULL; leak memory.

– Ian Abbott
Jan 2 at 11:24















Just before the string duplicate call, you have allocated memory of sizeof(char *)*(j + 1));, where j is 0. So, 1 char. Surely you need to check how long your strings are?

– doctorlove
Jan 2 at 11:32





Just before the string duplicate call, you have allocated memory of sizeof(char *)*(j + 1));, where j is 0. So, 1 char. Surely you need to check how long your strings are?

– doctorlove
Jan 2 at 11:32













@doctorlove I think that is just extending the array of char pointers by 1. The allocation for the string is done by the _strdup call and the returned pointer is stored in the newly created space.

– Ian Abbott
Jan 2 at 11:37





@doctorlove I think that is just extending the array of char pointers by 1. The allocation for the string is done by the _strdup call and the returned pointer is stored in the newly created space.

– Ian Abbott
Jan 2 at 11:37












1 Answer
1






active

oldest

votes


















0














The call to _strdup is likely failing because memory for (*matrix)[i][j] has not been allocated properly..



Using as many pointers as you are to read words from a file is not necessary, but given that you are using them, memory needs to be created for each, in the right order.



That is



matrix = malloc(sizeof(char *));


is the first of locations required before creating any others



For example, the method to create a properly allocated collection of pointers, for a 4 dimension array, might look like this:



char **** Create4D(int hR, int p, int c, int r)     
{
char ****arr;
int w,x,y;

arr = calloc(hR, sizeof(char *));
for(w=0;w<hR;w++)
{
arr[w] = calloc(p, sizeof(char *));
for(x=0;x<p;x++)
{
arr[w][x] = calloc(c, sizeof(char *));
for(y=0;y<c;y++)
{
arr[w][x][y] = calloc(r, 1);
// ^ sizeof(char) == 1
}
}
}
return arr;
}


Note the italicized is to remind you that this is not actually creating a matrix, just a collection of memory addresses, some with space for pointers, others with space for strings of char.



To call this function:



char ****matrix = Create4D(2, 4, 6, 8);
if(matrix)//always test for success. If failed, matrix is set to NULL.
{
//use matrix
//With these arguments, 48 (2*4*6) pointers are created,
//each with space allocated to contain strings of 8 char each
....


Note also that in all of these calls, casting the return of [m][c]alloc is not used here, because it is not recommended in C. Also, if you plan create all of this memory, corresponding calls to free() must be made for each call to calloc().



Memory from calling the method above on my system is illustrated here:



enter image description here






share|improve this answer


























  • ok, I've got the memory thing, but I have a question: why at the first calloc instead of arr = calloc(hR, sizeof(char *)); you did't do arr = calloc(hR, sizeof(char ***)); and the following ones with sizeof(char **), (char *) ?

    – Andre
    Jan 2 at 23:00













  • Because the size of memory being created is == sizeof (char *). That is a 32bit memory location for a char *** is the same as that for a char * and the same for a char ******. They are all 4 bytes. If using 64bit addressing, then the size for each of them would be 8 bytes. You can easily prove this by simply executing int size = sizeof(char *); then int size2 = sizeof(char ******);. size and size2 will be the same value. They are just pointers. The first is a pointer to a char sized memory location, the address for which takes 4 bytes, the second is a ...

    – ryyker
    Jan 3 at 12:52











  • ... pointer to a bunch of other pointers, i.e. char *****, still only requires 4 bytes, as does char ****, and char ***, and so on. Keep in mind using more than 2 pointers (eg. char **var is rarely if ever needed to read and parse a file. But when they are used it is for the purpose of creating addressable locations to simulate array like indexing, as illustrated in the answer, where In the example above, there are 2 blocks, each with 4 rows, each of those with 6 columns. All 48 of those are memory locations pointing to indexable space enough for 8 char.

    – ryyker
    Jan 3 at 13:23













  • okk, but if I've got a matrix of strings like in my programm, I don't know the right dimensions, right? I could I know how much memory I should storage , without giving random numbers at malloc?

    – Andre
    Jan 3 at 21:27











  • @Andre - I typically read through the file first (using fopen, fgets, strtok, fclose, etc., to determine things like longest line and number of lines, or word count, longest word, depending on what I need in that program. Then use those values to create a 2D matrix with number of strings, and length of longest string as the arguments (like the example code I provided, except with only 2 arguments)

    – ryyker
    Jan 3 at 22:05













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005280%2fcharacters-matrix-written-in-a-binary-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The call to _strdup is likely failing because memory for (*matrix)[i][j] has not been allocated properly..



Using as many pointers as you are to read words from a file is not necessary, but given that you are using them, memory needs to be created for each, in the right order.



That is



matrix = malloc(sizeof(char *));


is the first of locations required before creating any others



For example, the method to create a properly allocated collection of pointers, for a 4 dimension array, might look like this:



char **** Create4D(int hR, int p, int c, int r)     
{
char ****arr;
int w,x,y;

arr = calloc(hR, sizeof(char *));
for(w=0;w<hR;w++)
{
arr[w] = calloc(p, sizeof(char *));
for(x=0;x<p;x++)
{
arr[w][x] = calloc(c, sizeof(char *));
for(y=0;y<c;y++)
{
arr[w][x][y] = calloc(r, 1);
// ^ sizeof(char) == 1
}
}
}
return arr;
}


Note the italicized is to remind you that this is not actually creating a matrix, just a collection of memory addresses, some with space for pointers, others with space for strings of char.



To call this function:



char ****matrix = Create4D(2, 4, 6, 8);
if(matrix)//always test for success. If failed, matrix is set to NULL.
{
//use matrix
//With these arguments, 48 (2*4*6) pointers are created,
//each with space allocated to contain strings of 8 char each
....


Note also that in all of these calls, casting the return of [m][c]alloc is not used here, because it is not recommended in C. Also, if you plan create all of this memory, corresponding calls to free() must be made for each call to calloc().



Memory from calling the method above on my system is illustrated here:



enter image description here






share|improve this answer


























  • ok, I've got the memory thing, but I have a question: why at the first calloc instead of arr = calloc(hR, sizeof(char *)); you did't do arr = calloc(hR, sizeof(char ***)); and the following ones with sizeof(char **), (char *) ?

    – Andre
    Jan 2 at 23:00













  • Because the size of memory being created is == sizeof (char *). That is a 32bit memory location for a char *** is the same as that for a char * and the same for a char ******. They are all 4 bytes. If using 64bit addressing, then the size for each of them would be 8 bytes. You can easily prove this by simply executing int size = sizeof(char *); then int size2 = sizeof(char ******);. size and size2 will be the same value. They are just pointers. The first is a pointer to a char sized memory location, the address for which takes 4 bytes, the second is a ...

    – ryyker
    Jan 3 at 12:52











  • ... pointer to a bunch of other pointers, i.e. char *****, still only requires 4 bytes, as does char ****, and char ***, and so on. Keep in mind using more than 2 pointers (eg. char **var is rarely if ever needed to read and parse a file. But when they are used it is for the purpose of creating addressable locations to simulate array like indexing, as illustrated in the answer, where In the example above, there are 2 blocks, each with 4 rows, each of those with 6 columns. All 48 of those are memory locations pointing to indexable space enough for 8 char.

    – ryyker
    Jan 3 at 13:23













  • okk, but if I've got a matrix of strings like in my programm, I don't know the right dimensions, right? I could I know how much memory I should storage , without giving random numbers at malloc?

    – Andre
    Jan 3 at 21:27











  • @Andre - I typically read through the file first (using fopen, fgets, strtok, fclose, etc., to determine things like longest line and number of lines, or word count, longest word, depending on what I need in that program. Then use those values to create a 2D matrix with number of strings, and length of longest string as the arguments (like the example code I provided, except with only 2 arguments)

    – ryyker
    Jan 3 at 22:05


















0














The call to _strdup is likely failing because memory for (*matrix)[i][j] has not been allocated properly..



Using as many pointers as you are to read words from a file is not necessary, but given that you are using them, memory needs to be created for each, in the right order.



That is



matrix = malloc(sizeof(char *));


is the first of locations required before creating any others



For example, the method to create a properly allocated collection of pointers, for a 4 dimension array, might look like this:



char **** Create4D(int hR, int p, int c, int r)     
{
char ****arr;
int w,x,y;

arr = calloc(hR, sizeof(char *));
for(w=0;w<hR;w++)
{
arr[w] = calloc(p, sizeof(char *));
for(x=0;x<p;x++)
{
arr[w][x] = calloc(c, sizeof(char *));
for(y=0;y<c;y++)
{
arr[w][x][y] = calloc(r, 1);
// ^ sizeof(char) == 1
}
}
}
return arr;
}


Note the italicized is to remind you that this is not actually creating a matrix, just a collection of memory addresses, some with space for pointers, others with space for strings of char.



To call this function:



char ****matrix = Create4D(2, 4, 6, 8);
if(matrix)//always test for success. If failed, matrix is set to NULL.
{
//use matrix
//With these arguments, 48 (2*4*6) pointers are created,
//each with space allocated to contain strings of 8 char each
....


Note also that in all of these calls, casting the return of [m][c]alloc is not used here, because it is not recommended in C. Also, if you plan create all of this memory, corresponding calls to free() must be made for each call to calloc().



Memory from calling the method above on my system is illustrated here:



enter image description here






share|improve this answer


























  • ok, I've got the memory thing, but I have a question: why at the first calloc instead of arr = calloc(hR, sizeof(char *)); you did't do arr = calloc(hR, sizeof(char ***)); and the following ones with sizeof(char **), (char *) ?

    – Andre
    Jan 2 at 23:00













  • Because the size of memory being created is == sizeof (char *). That is a 32bit memory location for a char *** is the same as that for a char * and the same for a char ******. They are all 4 bytes. If using 64bit addressing, then the size for each of them would be 8 bytes. You can easily prove this by simply executing int size = sizeof(char *); then int size2 = sizeof(char ******);. size and size2 will be the same value. They are just pointers. The first is a pointer to a char sized memory location, the address for which takes 4 bytes, the second is a ...

    – ryyker
    Jan 3 at 12:52











  • ... pointer to a bunch of other pointers, i.e. char *****, still only requires 4 bytes, as does char ****, and char ***, and so on. Keep in mind using more than 2 pointers (eg. char **var is rarely if ever needed to read and parse a file. But when they are used it is for the purpose of creating addressable locations to simulate array like indexing, as illustrated in the answer, where In the example above, there are 2 blocks, each with 4 rows, each of those with 6 columns. All 48 of those are memory locations pointing to indexable space enough for 8 char.

    – ryyker
    Jan 3 at 13:23













  • okk, but if I've got a matrix of strings like in my programm, I don't know the right dimensions, right? I could I know how much memory I should storage , without giving random numbers at malloc?

    – Andre
    Jan 3 at 21:27











  • @Andre - I typically read through the file first (using fopen, fgets, strtok, fclose, etc., to determine things like longest line and number of lines, or word count, longest word, depending on what I need in that program. Then use those values to create a 2D matrix with number of strings, and length of longest string as the arguments (like the example code I provided, except with only 2 arguments)

    – ryyker
    Jan 3 at 22:05
















0












0








0







The call to _strdup is likely failing because memory for (*matrix)[i][j] has not been allocated properly..



Using as many pointers as you are to read words from a file is not necessary, but given that you are using them, memory needs to be created for each, in the right order.



That is



matrix = malloc(sizeof(char *));


is the first of locations required before creating any others



For example, the method to create a properly allocated collection of pointers, for a 4 dimension array, might look like this:



char **** Create4D(int hR, int p, int c, int r)     
{
char ****arr;
int w,x,y;

arr = calloc(hR, sizeof(char *));
for(w=0;w<hR;w++)
{
arr[w] = calloc(p, sizeof(char *));
for(x=0;x<p;x++)
{
arr[w][x] = calloc(c, sizeof(char *));
for(y=0;y<c;y++)
{
arr[w][x][y] = calloc(r, 1);
// ^ sizeof(char) == 1
}
}
}
return arr;
}


Note the italicized is to remind you that this is not actually creating a matrix, just a collection of memory addresses, some with space for pointers, others with space for strings of char.



To call this function:



char ****matrix = Create4D(2, 4, 6, 8);
if(matrix)//always test for success. If failed, matrix is set to NULL.
{
//use matrix
//With these arguments, 48 (2*4*6) pointers are created,
//each with space allocated to contain strings of 8 char each
....


Note also that in all of these calls, casting the return of [m][c]alloc is not used here, because it is not recommended in C. Also, if you plan create all of this memory, corresponding calls to free() must be made for each call to calloc().



Memory from calling the method above on my system is illustrated here:



enter image description here






share|improve this answer















The call to _strdup is likely failing because memory for (*matrix)[i][j] has not been allocated properly..



Using as many pointers as you are to read words from a file is not necessary, but given that you are using them, memory needs to be created for each, in the right order.



That is



matrix = malloc(sizeof(char *));


is the first of locations required before creating any others



For example, the method to create a properly allocated collection of pointers, for a 4 dimension array, might look like this:



char **** Create4D(int hR, int p, int c, int r)     
{
char ****arr;
int w,x,y;

arr = calloc(hR, sizeof(char *));
for(w=0;w<hR;w++)
{
arr[w] = calloc(p, sizeof(char *));
for(x=0;x<p;x++)
{
arr[w][x] = calloc(c, sizeof(char *));
for(y=0;y<c;y++)
{
arr[w][x][y] = calloc(r, 1);
// ^ sizeof(char) == 1
}
}
}
return arr;
}


Note the italicized is to remind you that this is not actually creating a matrix, just a collection of memory addresses, some with space for pointers, others with space for strings of char.



To call this function:



char ****matrix = Create4D(2, 4, 6, 8);
if(matrix)//always test for success. If failed, matrix is set to NULL.
{
//use matrix
//With these arguments, 48 (2*4*6) pointers are created,
//each with space allocated to contain strings of 8 char each
....


Note also that in all of these calls, casting the return of [m][c]alloc is not used here, because it is not recommended in C. Also, if you plan create all of this memory, corresponding calls to free() must be made for each call to calloc().



Memory from calling the method above on my system is illustrated here:



enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 13:39

























answered Jan 2 at 13:28









ryykerryyker

12.6k22959




12.6k22959













  • ok, I've got the memory thing, but I have a question: why at the first calloc instead of arr = calloc(hR, sizeof(char *)); you did't do arr = calloc(hR, sizeof(char ***)); and the following ones with sizeof(char **), (char *) ?

    – Andre
    Jan 2 at 23:00













  • Because the size of memory being created is == sizeof (char *). That is a 32bit memory location for a char *** is the same as that for a char * and the same for a char ******. They are all 4 bytes. If using 64bit addressing, then the size for each of them would be 8 bytes. You can easily prove this by simply executing int size = sizeof(char *); then int size2 = sizeof(char ******);. size and size2 will be the same value. They are just pointers. The first is a pointer to a char sized memory location, the address for which takes 4 bytes, the second is a ...

    – ryyker
    Jan 3 at 12:52











  • ... pointer to a bunch of other pointers, i.e. char *****, still only requires 4 bytes, as does char ****, and char ***, and so on. Keep in mind using more than 2 pointers (eg. char **var is rarely if ever needed to read and parse a file. But when they are used it is for the purpose of creating addressable locations to simulate array like indexing, as illustrated in the answer, where In the example above, there are 2 blocks, each with 4 rows, each of those with 6 columns. All 48 of those are memory locations pointing to indexable space enough for 8 char.

    – ryyker
    Jan 3 at 13:23













  • okk, but if I've got a matrix of strings like in my programm, I don't know the right dimensions, right? I could I know how much memory I should storage , without giving random numbers at malloc?

    – Andre
    Jan 3 at 21:27











  • @Andre - I typically read through the file first (using fopen, fgets, strtok, fclose, etc., to determine things like longest line and number of lines, or word count, longest word, depending on what I need in that program. Then use those values to create a 2D matrix with number of strings, and length of longest string as the arguments (like the example code I provided, except with only 2 arguments)

    – ryyker
    Jan 3 at 22:05





















  • ok, I've got the memory thing, but I have a question: why at the first calloc instead of arr = calloc(hR, sizeof(char *)); you did't do arr = calloc(hR, sizeof(char ***)); and the following ones with sizeof(char **), (char *) ?

    – Andre
    Jan 2 at 23:00













  • Because the size of memory being created is == sizeof (char *). That is a 32bit memory location for a char *** is the same as that for a char * and the same for a char ******. They are all 4 bytes. If using 64bit addressing, then the size for each of them would be 8 bytes. You can easily prove this by simply executing int size = sizeof(char *); then int size2 = sizeof(char ******);. size and size2 will be the same value. They are just pointers. The first is a pointer to a char sized memory location, the address for which takes 4 bytes, the second is a ...

    – ryyker
    Jan 3 at 12:52











  • ... pointer to a bunch of other pointers, i.e. char *****, still only requires 4 bytes, as does char ****, and char ***, and so on. Keep in mind using more than 2 pointers (eg. char **var is rarely if ever needed to read and parse a file. But when they are used it is for the purpose of creating addressable locations to simulate array like indexing, as illustrated in the answer, where In the example above, there are 2 blocks, each with 4 rows, each of those with 6 columns. All 48 of those are memory locations pointing to indexable space enough for 8 char.

    – ryyker
    Jan 3 at 13:23













  • okk, but if I've got a matrix of strings like in my programm, I don't know the right dimensions, right? I could I know how much memory I should storage , without giving random numbers at malloc?

    – Andre
    Jan 3 at 21:27











  • @Andre - I typically read through the file first (using fopen, fgets, strtok, fclose, etc., to determine things like longest line and number of lines, or word count, longest word, depending on what I need in that program. Then use those values to create a 2D matrix with number of strings, and length of longest string as the arguments (like the example code I provided, except with only 2 arguments)

    – ryyker
    Jan 3 at 22:05



















ok, I've got the memory thing, but I have a question: why at the first calloc instead of arr = calloc(hR, sizeof(char *)); you did't do arr = calloc(hR, sizeof(char ***)); and the following ones with sizeof(char **), (char *) ?

– Andre
Jan 2 at 23:00







ok, I've got the memory thing, but I have a question: why at the first calloc instead of arr = calloc(hR, sizeof(char *)); you did't do arr = calloc(hR, sizeof(char ***)); and the following ones with sizeof(char **), (char *) ?

– Andre
Jan 2 at 23:00















Because the size of memory being created is == sizeof (char *). That is a 32bit memory location for a char *** is the same as that for a char * and the same for a char ******. They are all 4 bytes. If using 64bit addressing, then the size for each of them would be 8 bytes. You can easily prove this by simply executing int size = sizeof(char *); then int size2 = sizeof(char ******);. size and size2 will be the same value. They are just pointers. The first is a pointer to a char sized memory location, the address for which takes 4 bytes, the second is a ...

– ryyker
Jan 3 at 12:52





Because the size of memory being created is == sizeof (char *). That is a 32bit memory location for a char *** is the same as that for a char * and the same for a char ******. They are all 4 bytes. If using 64bit addressing, then the size for each of them would be 8 bytes. You can easily prove this by simply executing int size = sizeof(char *); then int size2 = sizeof(char ******);. size and size2 will be the same value. They are just pointers. The first is a pointer to a char sized memory location, the address for which takes 4 bytes, the second is a ...

– ryyker
Jan 3 at 12:52













... pointer to a bunch of other pointers, i.e. char *****, still only requires 4 bytes, as does char ****, and char ***, and so on. Keep in mind using more than 2 pointers (eg. char **var is rarely if ever needed to read and parse a file. But when they are used it is for the purpose of creating addressable locations to simulate array like indexing, as illustrated in the answer, where In the example above, there are 2 blocks, each with 4 rows, each of those with 6 columns. All 48 of those are memory locations pointing to indexable space enough for 8 char.

– ryyker
Jan 3 at 13:23







... pointer to a bunch of other pointers, i.e. char *****, still only requires 4 bytes, as does char ****, and char ***, and so on. Keep in mind using more than 2 pointers (eg. char **var is rarely if ever needed to read and parse a file. But when they are used it is for the purpose of creating addressable locations to simulate array like indexing, as illustrated in the answer, where In the example above, there are 2 blocks, each with 4 rows, each of those with 6 columns. All 48 of those are memory locations pointing to indexable space enough for 8 char.

– ryyker
Jan 3 at 13:23















okk, but if I've got a matrix of strings like in my programm, I don't know the right dimensions, right? I could I know how much memory I should storage , without giving random numbers at malloc?

– Andre
Jan 3 at 21:27





okk, but if I've got a matrix of strings like in my programm, I don't know the right dimensions, right? I could I know how much memory I should storage , without giving random numbers at malloc?

– Andre
Jan 3 at 21:27













@Andre - I typically read through the file first (using fopen, fgets, strtok, fclose, etc., to determine things like longest line and number of lines, or word count, longest word, depending on what I need in that program. Then use those values to create a 2D matrix with number of strings, and length of longest string as the arguments (like the example code I provided, except with only 2 arguments)

– ryyker
Jan 3 at 22:05







@Andre - I typically read through the file first (using fopen, fgets, strtok, fclose, etc., to determine things like longest line and number of lines, or word count, longest word, depending on what I need in that program. Then use those values to create a 2D matrix with number of strings, and length of longest string as the arguments (like the example code I provided, except with only 2 arguments)

– ryyker
Jan 3 at 22:05






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005280%2fcharacters-matrix-written-in-a-binary-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory