C 2D arrays of string of unknown length, Crashing when accessing the elements

0

so I've been running into this error and I cant seem to find an answer for it so far, apologies if this question has been asked before. Heres the affected code:

char **List_Of_Words;
int List_Index=0;

List_Of_Words = malloc(Number_of_Words *sizeof(char*));

//Processed_Word word is a word that has been read from a file, its in a loop.
strcpy(*List_Of_Words[List_Index], Processed_Word);
List_Index++;


//im looping through the array to print each word thats stored there
 for(int i = 0;i < List_Index; i++)
{
printf("%s\n", *List_Of_Words[List_Index]);
} 

When I debugged with Visual Studio, I get this error:

Unhandled exception at 0x01229240 in Wordcount.exe: 0xC0000005: Access violation writing location 0x00000030.

So, I'm assuming that my program is trying to access memory that it doesn't have access to. C is a new language for me so I don't really know how to deal with this.

c
multidimensional-array
c-strings
asked on Stack Overflow Feb 21, 2018 by Radium • edited Feb 21, 2018 by Claudio Cortese

4 Answers

1

There's a number of problems with your code.

You're firstly not allocating enough memory. List_Of_Words needs to be allocated enough memory for each word, which you're doing just fine. But each List_Of_Words[] also needs memory allocated to contain the words too.

Then you're doing this, which is wrong because you're passing in a char as the first argument instead of a char *

strcpy(*List_Of_Words[List_Index], Processed_Word);

Fixing this and the previous problem your code should look like

List_Of_Words[List_Index]=malloc(strlen(Processed_Word)+1);
strcpy(List_Of_Words[List_Index], Processed_Word);

or if available you can use strdup which combines the two lines into one

List_Of_Words[List_Index]=strdup(Processed_Word);

You repeat the over de-referencing problem when printing out the strings too, so it should look like

printf("%s\n", List_Of_Words[i]);

unless of course you're just printing out the first character in which case you should use the %c formatting code.

answered on Stack Overflow Feb 21, 2018 by Chris Turner • edited Feb 21, 2018 by Chris Turner
0

The problem here is that you have defined what List_Of_Words is but forgot to allocate memory to it.

Let r = total number of words you want to store in List_Of_Words.

The following line of code allocates r chunks of data type char* where each chunk will hold a word.

char **List_Of_Words = (char **)malloc(r * sizeof(char *));

The following line of code allocates c chunks of data type char where each chunk will hold a character and stores the data in it. Here c is the size of the processed_word to be stored.

Inside the loop:
    int c = strlen(processed_word) + 1
    List_Of_Words[i] = (char *)malloc(c * sizeof(char));
    strcpy(List_Of_Words[i], processed_word);
answered on Stack Overflow Feb 21, 2018 by Mahidhar Bandaru • edited Feb 21, 2018 by Mahidhar Bandaru
0

This line

strcpy(*List_Of_Words[List_Index], Processed_Word);

does not allocate memory to the copy, it assumes you already have enough space allocated. If your system has strdup, you can use that to allocate and copy the string (however, strdup is not portable - it's not in the C standard or the Posix standard).

List_Of_Words[List_Index] = strdup(Processed_Word);

If you don't have strdup, allocate memory before doing strcpy.

List_of_Words[List_Index] = malloc(strlen(Processed_Word) + 1); // + 1 for the \0 at the end
strcpy(List_Of_Words[List_Index], Processed_Word);

Notice also that using subscripts is the same as doing a dereference. List_of_Words[index] is a char* i.e. it is already the string you want. *List_of_Words[index] is a char i.e. it is the first character of the string at List_of_Words[index]. Treating a char as a pointer can only lead to trouble. You need to drop the * prefix from every use of *List_of_Words[...] in your code

answered on Stack Overflow Feb 21, 2018 by JeremyP
0

You say "//Processed_Word word is a word that has been read from a file, its in a loop." - Hope you don't have that List_of_Words malloc inside the loop. I'd expect you to do it outside the loop that reads the words once

Also, I'd expect you to be allocating memory for each entry in the List_of_Words, but you don't seem to be doing it.

A 2D array like the one you try to make is practically a 1D array (the one dimension) that holds references to other 1D arrays (the second dimension). The content cells exist at those other 1D arrays (the arrays of the last dimension if you have multiple ones).

answered on Stack Overflow Feb 21, 2018 by George Birbilis

User contributions licensed under CC BY-SA 3.0