Dynamically create and initialize an array of pointers to struct in C - c

Here is an example code of what I'm trying to achieve
typedef struct
{
int x_pos, y_pos;
} A;
typedef struct
{
A **arrayAp;
} B;
int main(void) {
B B;
int n = 10;
A *array[n];
B.arrayAp = array;
for (int i = 0; i < 10; i++)
{
B.arrayAp[i] = malloc(sizeof(A));
B.arrayAp[i]->x_pos = i;
B.arrayAp[i]->y_pos = i + 1;
}
printf("%d",B.arrayAp[0]->x_pos);
}
It works as I want it to work. I can access elements of "array" using "arrayAp" pointer. But when I try to move some of this code to function for example:
A * makeAnArray()
{
int n = 10;
A *array[n];
return &array;
}
and then assign value that it returns to "arrayAp"
B.arrayAp = makeAnArray();
Now I can't access first element of the array. Program just crash.
printf("%d",B.arrayAp[0]->x_pos);
But starting from second element everything works the same.
But the bigger problem I get when I try to move to function this code:
void initializeAnArray(B *B)
{
for (int i = 0; i < 10; i++)
{
B->arrayAp[i] = malloc(sizeof(A));
B->arrayAp[i]->x_pos = i;
B->arrayAp[i]->y_pos = i + 1;
}
}
Seems like it has no effect. When i'm trying to access array member through "arrayAp" pointer i'm getting some "random" values from memory.
typedef struct
{
int x_pos, y_pos;
} A;
typedef struct
{
A **arrayAp;
} B;
A * makeAnArray()
{
int n = 10;
A *array[n];
return &array;
}
void initializeAnArray(B *B)
{
for (int i = 0; i < 10; i++)
{
B->arrayAp[i] = malloc(sizeof(A));
B->arrayAp[i]->x_pos = i;
B->arrayAp[i]->y_pos = i + 1;
}
}
int main(void) {
B B;
B.arrayAp = makeAnArray();
initializeAnArray(&B);
printf("%d",B.arrayAp[1]->x_pos);
}
Sorry for my poor English.

Your function should be:
A * makeAnArray()
{
int n = 10;
//A *array[n]; --> This is local to this function so won't work
A *array = malloc(sizeof(A)*n); //--> Dynamic allocation
return array;
}

A * makeAnArray()
{
int n = 10;
A *array[n];
return &array;
}
You are returning a local pointer which dies after the function ends and causes UB.
Moreover return type of your function should be A** and you should allocate memory with *alloc and return to ensure every instance is different and is alive after the function call.
Possible fix
A * makeAnArray()
{
int n = 10;
A *array = malloc(n * sizeof(A));
return array;
}

The part
A * makeAnArray()
{
int n = 10;
A *array[n];
return &array;
}
is not going to work. The array array might be allocated on the stack, it cannot be accessed via the return value after termination of makeAnArray; the result is undefined behaviour.

Related

C, Segmentation fault while using dynamic array in struct

I'm trying to add new element to dynamic array in C (I know that I must free all memory. I will do it later), but I get this error every time:
But, what is strange, if I compile from terminal, like that, code works properly.
So, where is the error and how i can beat it?
Thank you!
All my code:
main.c
#include <stdio.h>
#include <stdlib.h>
typedef struct vector
{
int size;
int *array;
int alreadyIn;
}vector;
vector *vectorInit(int size)
{
vector *newVec = (vector *)malloc(sizeof(vector));
if(!newVec){printf("No memory!\n"); return NULL;}
newVec->size = size;
newVec->array = (int *)malloc(size * sizeof(int));
return newVec;
}
void allocNewMemory(vector *vect, int howMuch)
{
vect->array = (int *)realloc(vect->array ,(vect->size + howMuch) * sizeof(int));
vect->size += howMuch;
}
void pushBack(vector *vect, int number)
{
int howMuch = 5;
if(vect && vect->alreadyIn < vect->size)
{
vect->array[vect->alreadyIn] = number;
vect->alreadyIn++;
}
else
{
printf("Alloc new memory for %d elements...\n", howMuch);
allocNewMemory(vect, howMuch);
pushBack(vect, number);
}
}
void printVector(vector *vect)
{
for (int i = 0; i < vect->alreadyIn; i++)
{
printf("%d ", vect->array[i]);
}
printf("\n");
}
int main()
{
int startSize = 4;
vector * vec = vectorInit(startSize);
for (int i = 0; i < 6; i++)
{
pushBack(vec, i+1);
}
printVector(vec);
return 0;
}
You never initialize the alreadyIn member in the structure. That means its value will be indeterminate (and seemingly garbage or random).
You need to explicitly initialize it to zero:
vector *vectorInit(int size)
{
vector *newVec = malloc(sizeof(vector));
if(!newVec)
{
printf("No memory!\n");
return NULL;
}
newVec->size = size;
newVec->array = malloc(size * sizeof(int));
newVec->alreadyIn = 0; // Remember to set this to zero
return newVec;
}
This problem should have been easy to detect in the debugger.
Also note that I removed the casts from malloc. One should not cast the result of malloc, or really any function returning void *.

Unable to return array from another function to main function

I have been trying to pass my array address to print array values in main function. But it is not working as it gives "Count by X.exe has stopped working". It also shows a warning message which says "function is returning address of local variables". I am not able to find the problems. It would be helpful if anyone finds pointer related problem of my code which is given below.
#include<stdio.h>
int * countBy(int x, int n)
{
int arr[n];
int count = x;
for(int i = 0; i < n; i++)
{
arr[i] = count;
count = count + x;
}
return arr;
}
int main()
{
int x = 2, n = 10;
int * prr;
prr = countBy(x, n);
for(int i = 0; i < 10; i++)
{
printf("%d ", prr[i]);
}
return 0;
}
You cannot return arrays in C. You would either need to create the array in the main function and pass it to the function or use dynamic allocation.
Passing an output array:
void countBy(int x, int n, int *arr)
{
int count = x;
for(int i = 0; i < n; i++) {
arr[i] = count;
count = count + x;
}
}
int main(void)
{
int x = 2, n = 10;
int arr[n];
countBy(x, n, arr);
}
Dynamic allocation:
int * countBy(int x, int n)
{
int *arr = malloc(n * sizeof(*arr));
int count = x;
for(int i = 0; i < n; i++) {
arr[i] = count;
count = count + x;
}
return arr;
}
int main(void)
{
int x = 2, n = 10;
int *prr;
prr = countBy(x, n);
free(prr); // Remember to use free to not cause memory leaks
}
The local variables have a lifetime which extends only inside the block in which it is defined. The moment the control goes outside the block in which the local variable is defined, the storage for the variable is no more allocated (not guaranteed). Therefore, using the memory address of the variable outside the lifetime area of the variable will be undefined behaviour.
On the other hand you can do the following, replace the int arr[n] with a static array but the size of the array must be declared.
...
static int arr[10];
...
This will fix the problem but you couldnt change the size of the array if the user inputs the wanted size of it.

Expanding Static Arrays in C

Is it possible to extend a static array in C?
I tried creating a function which allocates a new dynamic array, copies contents to it and returns it's pointer; but It didn't work correctly, I'm getting debug error everytime.
I need to do something like this;
int a1[5] = { 1,2,5,4,1 };
extend(a1); //or a1* = extend(a1);
.
.
.
a1[9] = 2;
My extend function so far;
int extend(int x[], int oldSize, int newSize) {
int *extendedX = (int*)calloc(newSize, sizeof(int));
int i = 0;
for (; i < oldSize;i++) {
extendedX[i] = x[i];
}
for (; i < newSize; i++) {
extendedX[i] = 0;
}
return *extendedX;
}
extend(a1); //or a1* = extend(a1);
Your function extend needs to return int * instead of just int.
int* extend(int x[], int oldSize, int newSize);
Also, you need to create a new pointer a2 to be assigned to the return value of extend function:
int *a2 = extend( a1, oldSize, newSize );
You can't extend static array.
you can do it like this:
#include <stdio.h>
int *extends(int ar[],int oldsz,int newsz){
int *newAr = malloc(newsz*sizeof(int));
for(int i = 0; i < oldsz;i++){
newAr[i] = ar[i];
}
return newAr;
}
int main(void) {
int i,a[5] = { 1, 2, 3, 4, 5};
int *a1 = extends(a,5,10);
a1[5] = 6;
a1[6] = 7;
a1[7] = 8;
a1[8] = 9;
a1[9] = 10;
a1[10] = 11;
a1[11] = 12;
for(i = 0; i < 12; i++)printf("%d ",a1[i]);
return 0;
}
You can't extend a static array, it's size is determined at compile time.
However, you could declare a1 as a pointer, and use a static initializer to set the initial value,
int *a1 = (int[]){ 1, 2, 5, 4, 1};
then later assign the result of the extend function (which should return an int * like #artm suggested).

Return a 2d array from a function

I am a newbie to C.
I am trying to return a 2d array from a function.
It is something like this
int *MakeGridOfCounts(int Grid[][6])
{
int cGrid[6][6] = {{0, }, {0, }, {0, }, {0, }, {0, }, {0, }};
int (*p)[6] = cGrid;
return (int*)p;
}
I know this causes an error, need help. thanks
The C language has a basic flaw: it is impossible to return arrays from functions.
There are many workarounds for this; i'll describe three.
Replace by a pointer to an array
Return a pointer instead of an array itself. This leads to another problem in C: when a function returns a pointer to something, it should usually allocate the something dynamically. You should not forget to deallocate this later (when the array is not needed anymore).
typedef int (*pointer_to_array)[6][6];
pointer_to_array workaround1()
{
pointer_to_array result = malloc(sizeof(*result));
(*result)[0][0] = 0;
(*result)[1][0] = 0;
(*result)[2][0] = 0;
(*result)[3][0] = 0;
(*result)[4][0] = 0;
(*result)[5][0] = 0;
return result;
}
Replace by a pointer to int
A 2-D array appears just as a sequence of numbers in memory, so you can replace it by a pointer to first element. You clearly stated that you want to return an array, but your example code returns a pointer to int, so maybe you can change the rest of your code accordingly.
int *workaround2()
{
int temp[6][6] = {{0}}; // initializes a temporary array to zeros
int *result = malloc(sizeof(int) * 6 * 6); // allocates a one-dimensional array
memcpy(result, temp, sizeof(int) * 6 * 6); // copies stuff
return result; // cannot return an array but can return a pointer!
}
Wrap with a structure
It sounds silly, but functions can return structures even though they cannot return arrays! Even if the returned structure contains an array.
struct array_inside
{
int array[6][6];
};
struct array_inside workaround3()
{
struct array_inside result = {{{0}}};
return result;
}
It sounds like you want a function returning pointer to array[6] of int:
int (*makeGrid())[6]
{
return calloc(6*6,sizeof(int)); //zeros the memory as in your example.
}
You would call and use it like so:
int (*arr)[6] = makeGrid();
arr[4][3] = 3; //etc...
Try this out, compiles fine with GCC on my mac..
typedef struct _intGrid_t{
int **data;
int width;
int height;
} *IntGrid;
IntGrid makeGridWithSize(int width, int height);
IntGrid makeGridWithSize(int width, int height)
{
IntGrid grid = malloc(sizeof(struct _intGrid_t));
int **data = (int **) malloc(sizeof(int *) * width);
for (int i = 0; i < width; i++) {
data[i] = malloc(sizeof(int) * height);
memset(data[i], 0, sizeof(int) * height);
}
grid->data = data;
grid->width = width;
grid->height = height;
return grid;
}
void printGrid(IntGrid grid);
void printGrid(IntGrid grid)
{
printf(" { \n");
for (int i =0; i < grid->width; i++) {
printf(" { ");
for (int j = 0; j < grid->height; j++) {
printf("%i", grid->data[i][j]);
if (j != grid->height - 1)
{
printf(", ");
}
}
printf(" } \n");
}
printf(" } \n");
}
void freeGrid(IntGrid grid);
void freeGrid(IntGrid grid)
{
for (int i = 0; i < grid->width; i++) {
free(grid->data[i]);
}
free(grid->data);
free(grid);
}
int main (int argc, const char * argv[])
{
srand((int) time(NULL));
IntGrid grid = makeGridWithSize(10, 10);
for (int i = 0; i < grid->width; i++) {
for (int j = 0; j < grid->height; j++) {
grid->data[i][j] = rand() % 10;
}
}
printGrid(grid);
freeGrid(grid);
return 0;
}

Unable to get array of structures initialized

I am passing a pointer to function and I want to initialze the array of structures in called function and want to use that array main function. But I was unable to get it in main function.
Here is my code:
typedef struct _testStruct
{
int a;
int b;
} testStruct;
void allocate(testStruct** t)
{
int nCount = 0;
int i = 0;
printf("allocate 1\n");
t = (testStruct**)malloc(10 * sizeof(testStruct));
for(i = 0; i < 10; i++)
{
t[i] = (testStruct *) malloc( 10 * sizeof(testStruct));
}
for(nCount = 0 ; nCount < 10; nCount++)
{
t[nCount]->a = nCount;
t[nCount]->b = nCount + 1;
printf( "A === %d\n", t[nCount]->a);
}
}
int main()
{
int nCount = 0;
testStruct * test = NULL;
int n = 0;
allocate(&test);
for(nCount = 0 ; nCount < 10; nCount++ )
{
if (test == NULL)
{
printf( "Not Allocated\n");
exit(0);
}
//printf("a = %d\n",test[nCount]->a);
/*printf("a = %d\n",test->a);
printf("b = %d\n",test->b); */
}
return 0;
}
Please note I have to pass double pointer to function as it is required.
Thank you for helping.
#include <stdio.h>
#include <stdlib.h>
typedef struct _testStruct
{
int a;
int b;
} testStruct;
void allocate(testStruct** t)
{
int nCount = 0;
printf("allocate 1\n");
testStruct *newT = (testStruct*)malloc(10 * sizeof(testStruct));
for(nCount = 0 ; nCount < 10; nCount++)
{
newT[nCount].a = nCount;
newT[nCount].b = nCount + 1;
printf( "A === %d\n", newT[nCount].a);
}
*t = newT;
}
int main()
{
int nCount = 0;
testStruct * test = NULL;
allocate(&test);
for(nCount = 0 ; nCount < 10; nCount++ )
{
printf("a = %d\n",test[nCount].a);
printf("a = %d\n",test[nCount].b);
}
return 0;
}
Should work.
t = (testStruct**)malloc(10 * sizeof(testStruct));
is assigning to t, not test. Perhaps you want
*t = (testStruct*)malloc(10 * sizeof(testStruct));
instead? I'm not sure, I tend to get lost when so many pointers are around. Anyway, you don't seem to be assigning anything into the pointer you pass to your function.
You say you want to create an array of structures, but your allocate function creates a data structure more like two-dimensional array. In addition, you don't return that structure back to the caller in any way that makes sense. I think you have come confusion about pointers, malloc() and all of the indirection you're doing. Check out #Ed Heal's answer for a corrected program.

Resources