I am a bit of a beginner in C language. I was trying out a task of dynamically reallocating memory as the input comes through(although for this test I am doing a normal task, later will try to scale it up). the issue I am facing is I am unable to access the memory while writing into it. Can anyone please help me understand where I am going wrong in this code. thanks in advance.
code:
#include<stdio.h>
#include<stdlib.h>
struct ai {
int* a;
char* b;
};
int main()
{
struct ai *test;
test = (struct ai*) malloc(sizeof(struct ai));
test[0].a = (int*)malloc(sizeof(int));
test[0].b = (char*)malloc(sizeof(char));
/// storing info
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
test[i].a[j] = j;
test[i].b[j] = 65 + j;
test[i].a = (int *) realloc(test[i].a, (j + 2) * sizeof(int));
test[i].b = (char*) realloc(test[i].b, (j + 2) * sizeof(char));
}
test = (struct ai*)realloc(test, (i + 2) * sizeof(struct ai));
}
// printing the block out
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
printf("%d , %c\n", test[i].a[j], test[i].b[j]);
}
}
return 0;
} ```
You have missing initialization of your pointers and some other issues...
In C you should not cast the result of
malloc. In best case it is useless. In worst case it hides real problems.Here comes the reason for your segmentation fault: You do not have any memory reserved for
aandbexcept for the first elementtest[0]. All other array elementstest[i]do not contain any useful data. Dereferencing these pointers is undefined behaviour.You must allocate memory for each field after you enlarged your array.
You should never assign the return value of
reallocto the same variable that you passed into the function. In case of an error you will getNULLreturn value and then your initiali pointer is lost.Also here: don't cast for
reallocas well.Same here: Don't assign to same variable and don't cast the result of
realloc.Finally: Here goes the missing part:
Of course you should check all return values for error results.
Not related to the segmentation fault, but still worth to fix: You increment the size of each array after you used the last element. This prepares the new element for being filled in next iteration of the loops. But in case of the last iteration it allocated an unused element for both the intern pointers
aandbas well as the outer arraytest.You will have some unused elements:
An improved solution could look like this (not tested)
I start with each pointer being
NULLand usereallocright before the new element is used to avoid the extra elements.