I'm attempting a C programming assignment where I need to iterate through each index of each line of a document, and set an integer value at each character index in a corresponding array ar:
//Jagged array ar containing pointers to each row
int* ar[line_count];
//The size of each row is the line width * the size of an int ptr
const int line_size = max_width * sizeof(int*);
//For each line
for (i = 0; i < line_count; i++)
{
//If the first runthrough, copy the blank array
if (i == 0)
{
ar[i] = malloc(line_size);
memcpy(ar[i], blank_ar, line_size);
}
//Otherwise, copy from the last row
else
{
ar[i] = malloc(line_size);
//This is set to a null pointer after several runthroughs
memcpy(ar[i], ar[i - 1], line_size);
}
//Edit the current row ar[i]
}
The only problem is, after some 9 iterations, malloc starts returning a null pointer that causes memcpy to (obviously) not work.
Is there any reason this is happening? There is no way I'm running out of memory as I only allocate these tiny arrays 9 times.
errnosay?line_counta compile time constant (macro, enum) or a variable? It looks to me thataris a VLA, which probably is not a good idea, since you might smash your "stack".line_countis a variable whose value is not known at compile time, thenint *ar[line_count]is definitely a VLA.