inicializing a structure with arrays of arrays [duplicate] - c

I'm trying to create a function that creates a variable sized 2D funct array. I'm using the following code, which seems to work just fine on its own:
typedef struct
{
//Starter Properties
int TypeB;
int TypeF;
int TypeW;
//Randomized Properties
int RandB;
int RandF;
int RandW;
//Derived Properties
int Speed;
} MapTileData;
MapTileData **Map;
int i, x=5, y=5;
//Allocate Initial Space
Map = (MapTileData**)calloc(x, sizeof(MapTileData));
for(i = 0; i < x; i++)
{
Map[i] = (MapTileData*)calloc(y, sizeof(MapTileData));
}
So the above code creates a 2D struct array. My attempts to move the code to a function have been less successful, giving segmentation faults when trying to print the array:
void CreateMap(MapTileData **Map, int xSize, int ySize)
{
//Variables
int i;
//Allocate Initial Space
Map = (MapTileData**)calloc(xSize, sizeof(MapTileData));
for(i = 0; i < xSize; i++)
{
Map[i] = (MapTileData*)calloc(ySize, sizeof(MapTileData));
}
}
Used in the code:
MapTileData **MapTile;
CreateMap(MapTile,5,5);
Any and all help is greatly appreciated!

Function arguments are passed by value in C and modifying arguments in callee won't affect caller's local variables.
Use pointers to modify caller's local variables.
void CreateMap(MapTileData ***Map, int xSize, int ySize)
{
//Variables
int i;
//Allocate Initial Space
*Map = calloc(xSize, sizeof(MapTileData));
for(i = 0; i < xSize; i++)
{
(*Map)[i] = calloc(ySize, sizeof(MapTileData));
}
}
Usage in the code:
MapTileData **MapTile;
CreateMap(&MapTile,5,5);
Alternate way: Pass the allocated array via the return value.
MapTileData **CreateMap(int xSize, int ySize)
{
//Variables
MapTileData **Map;
int i;
//Allocate Initial Space
Map = calloc(xSize, sizeof(MapTileData));
for(i = 0; i < xSize; i++)
{
Map[i] = calloc(ySize, sizeof(MapTileData));
}
//Return the value
return Map;
}
Usage in the code:
MapTileData **MapTile;
Maptile = CreateMap(5,5);
Also note that they say you shouldn't cast the result of malloc() and its family in C.

Related

LinkList not adding a new Node [duplicate]

I'm trying to create a function that creates a variable sized 2D funct array. I'm using the following code, which seems to work just fine on its own:
typedef struct
{
//Starter Properties
int TypeB;
int TypeF;
int TypeW;
//Randomized Properties
int RandB;
int RandF;
int RandW;
//Derived Properties
int Speed;
} MapTileData;
MapTileData **Map;
int i, x=5, y=5;
//Allocate Initial Space
Map = (MapTileData**)calloc(x, sizeof(MapTileData));
for(i = 0; i < x; i++)
{
Map[i] = (MapTileData*)calloc(y, sizeof(MapTileData));
}
So the above code creates a 2D struct array. My attempts to move the code to a function have been less successful, giving segmentation faults when trying to print the array:
void CreateMap(MapTileData **Map, int xSize, int ySize)
{
//Variables
int i;
//Allocate Initial Space
Map = (MapTileData**)calloc(xSize, sizeof(MapTileData));
for(i = 0; i < xSize; i++)
{
Map[i] = (MapTileData*)calloc(ySize, sizeof(MapTileData));
}
}
Used in the code:
MapTileData **MapTile;
CreateMap(MapTile,5,5);
Any and all help is greatly appreciated!
Function arguments are passed by value in C and modifying arguments in callee won't affect caller's local variables.
Use pointers to modify caller's local variables.
void CreateMap(MapTileData ***Map, int xSize, int ySize)
{
//Variables
int i;
//Allocate Initial Space
*Map = calloc(xSize, sizeof(MapTileData));
for(i = 0; i < xSize; i++)
{
(*Map)[i] = calloc(ySize, sizeof(MapTileData));
}
}
Usage in the code:
MapTileData **MapTile;
CreateMap(&MapTile,5,5);
Alternate way: Pass the allocated array via the return value.
MapTileData **CreateMap(int xSize, int ySize)
{
//Variables
MapTileData **Map;
int i;
//Allocate Initial Space
Map = calloc(xSize, sizeof(MapTileData));
for(i = 0; i < xSize; i++)
{
Map[i] = calloc(ySize, sizeof(MapTileData));
}
//Return the value
return Map;
}
Usage in the code:
MapTileData **MapTile;
Maptile = CreateMap(5,5);
Also note that they say you shouldn't cast the result of malloc() and its family in C.

C - Declaring a global array within a function not possible. Other solutions?

Here's what I'm trying to do:
I need a global array with a length that is dependent on a variable (NumOfRequests). That variable is set within a function. I don't think it's possible to declare a global array within a function as I tried to do in the following code:
static uint8 NumOfRequests;
typedef struct
{
uint16 IndexToRequest;
GdXRequestData_ts RequestData;
bool RequestSent;
} GdRequests;
static void SetupRequestStructures( void )
{
uint8 i;
for( i = 0; EepromData_ps.GD_Indices[i] != 0xFFFF; i++ )
{
NumOfRequests = i + 1;
}
GdRequests Requests[NumOfRequests];
for( i = 0; i < NumOfRequests; i++ )
{
Requests[i].IndexToRequest = EepromData_ps.GD_Indices[i];
}
}
It's not possible to declare an array and later decide what length it should be or change its length, as far as I know.
So does anyone know another solution for how to declare a global array with a length based on a variable that is set within a function?
Any feedback is appreciated. If you upvote or downvote, tell me why so I can improve with future questions.
You can use dynamic memory allocation to perform this task. Look at malloc and free. Here's a simple example of dynamic memory allocation on a global array of structs:
#include <stdio.h>
#include <stdlib.h>
typedef struct _MyStruct
{
int a;
int b;
} MyStruct;
MyStruct *g_Array = NULL;
void populatearray(MyStruct *array, int length)
{
int i;
for(i = 0; i < length; i++)
{
array[i].a = i;
array[i].b = i;
}
}
void printstructs(MyStruct *array, int length)
{
int i;
for(i = 0; i < length; i++)
{
printf("array[%d].a = %d\narray[%d].b = %d\n\n", i, array[i].a, i, array[i].b);
}
}
int main()
{
g_Array = malloc(50 * sizeof(MyStruct));
if(!g_Array)
{
puts("Malloc failed");
return 0;
}
populatearray(g_Array, 50);
printstructs(g_Array, 50);
free(g_Array);
return 0;
}

Plain C - Array pointers

I have structure to hold pointer to array of input numbers. When I create matrix I also create default data array. As I suppose the m.data = data; means that m.data pointer points at first element of the array. When I print data right after alocation everything seems ok. But when I print them after in the main function the result is different.
Why does output differs(commented sections)?
#include <stdio.h>
#include <stdlib.h>
typedef struct matrix {
int *data;
int rows;
} Matrix;
//Create Matrix
extern Matrix create_matrix(int size)
{
int data[size * size];
int i = 0;
for(i = 0; i < size * size; i++)
{
data[i] = 0;
}
Matrix m;
m.data = data;
for(i = 0; i < size * size; i++)
{
printf("%d\n",*(m.data + i)); //<--- m.data {0,0 ..... ,0,0,0}
}
m.rows = size;
return m;
};
extern void supply_row_data(Matrix m, int row, int* data)
{
int i = 0;
for(i = 0; i < m.rows; i++)
{
m.data[i] = data[i];
}
};
int main(){
int size = 4;
Matrix m = create_matrix(size);
int i = 0;
int *j = m.data;
for(i = 0; i < size * size; i++)
{
//*(j + i) = i;
printf("%d\n", *(j + i));
}
getch();
}
int data[size * size];
This is a local variable (array) to the function create_matrix It goes out of scope when the function terminates.
m.data = data;
This does not copy the data in the variable data to the data in the variable m.data but rather sets the pointer m.data to the beginning of the array data. When the function returns, the Matrix returned contains what is called a dangling pointer: a pointer to data that no longer exists.
Use the advice from #RSahu to fix this.
That's not how you can copy the data from an array in C.
You need to:
Allocate memory for the data.
m.data = malloc(sizeof(int)*size*size);
Copy the data one element at a time or using memcpy.
memcpy(m.data, data, sizeof(int)*size*size);
Make sure you deallocate the memory allocated to hold the matrix data.
free(m.data);
You should avoid using pointer to local data once function return address of array is no loger valid try to allocate array in following way
int* allocate_vector(size_t elem, int value){
int* vec = (int *) calloc(elem, sizeof(int));
int i = 0;
for( ; i < elem; i++){
*(vec + i) = value;
}
return vec;
}

Passing struct array of pointers as a function argument?

The code works if i use the name of the struct array directly for the allocation, but not from the function argument. Otherwise it returns memory error.
typedef struct COORD
{
int xp;
int yp;
} coord;
coord** xy;
void allocate(coord** COORD)
{
int i;
//allocate COORD[500][460]
COORD = (coord**)malloc(sizeof(coord*)*500);
for(i=0; i<500; i++)
{
COORD[i] = (coord*)malloc(sizeof(coord)*460);
}
// freeing
for (i=0; i<500; i++) free(COORD[i]);
free(COORD);
}
//function call: allocate(xy);
//That is the code that leeds to the error
Using just xy instead of COORD works. And i am all wondering why is that not working.
You are mixing up various coding styles here. It's not clear what exactly you want to achieve. Pick one according to your task.
Temporary buffer
You need a large temporary buffer that should be allocated on the heap and that does not need to be seen from outside. Just create a local variable:
void do_stuff(int w, int h)
{
coord **p;
int i;
p = malloc(h * sizeof(*p));
for (i = 0; i < h; i++) p[i] = malloc(w * sizeof(**p));;
// do stuff
for (i = 0; i < h; i++) free(p[i]);
free(p);
}
Allocate memory for further use
You want to allocate storage that your client code can use. Then provide two functions, one that allocates and one that frees the memory:
coord **create(int w, int h)
{
coord **p;
int i;
p = malloc(h * sizeof(*p));
for (i = 0; i < h; i++) p[i] = malloc(w * sizeof(**p));
return p;
}
void destroy(coord **p, int h)
{
int i;
for (i = 0; i < h; i++) free(p[i]);
free(p);
}
Your client code can then use the memory between these calls:
coord **p = create(500, 460);
// do stuff
drestroy(p, 500);
(Note that you have to pass the height to destroy, which is a bit unfortunate. It might be cleaner to create a wrapper struct that hold information about width and height and the pointer.)
Allocate memory for a global variable
You have a single instance of a global pointer. Then your functions always operate on that pointer and you don't need any further information on it (except the dimensions):
coord **global = NULL;
void destroy_global(int h)
{
int i;
for (i = 0; i < h; i++) free(global[i]);
free(global);
global = NULL;
}
void create_global(int w, int h)
{
int i;
if (global != NULL) free_global();
global = alloc(h * sizeof(*global));
for (i = 0; i < h; i++) global[i] = malloc(w * sizeof(**global));
}
Note that you should include <stdlib.h> for all memory functions and the NULL macro.
Addendum According to your comment, you want to allocate memory for a bitmap. That's option 2 above.
I recommend to create an object structure. You can pass a pointerv to that structure as handle to a bunch of functions. You can create the object with a function that returns that handle.
The following sketches a rough design for a bitmap object.
typedef struct Pixel Pixel;
typedef struct Bitmap Bitmap;
struct Pixel {
uint8_t r, g, b;
};
struct Bitmap {
int height;
int width;
Pixel **pixel;
};
Bitmap *bitmap_new(int w, int h)
{
Bitmap *bmp = malloc(sizeof(*bmp));
int i;
bmp->height = h;
bmp->width = w;
bmp->pixel = malloc(h * sizeof(*bmp->pixel));
for (i = 0; i < h; i++) {
bmp->pixel[i] = malloc(w * sizeof(**bmp->pixel));
}
return p;
}
void bitmap_delete(Bitmap *bmp)
{
int i;
for (i = 0; i < h; i++) free(bmp->pixel[i]);
free(bmp->pixel);
free(bmp);
}
Bitmap *bitmap_read(const char *fn)
{
Bitmap *bmp;
FILE *f = fopen(fn, "rb");
// read and allocate
return bmp;
}
void bitmap_blank(Bitmap *bmp, int r, int g, int b)
{
for (i = 0; i < bitmap->height; i++) {
for (j = 0; j < bitmap->width; j++) {
bmp->pixel[i][j].r = r;
bmp->pixel[i][j].g = g;
bmp->pixel[i][j].b = b;
}
}
}
void bitmap_mirror_x(Bitmap *bmp)
{
// do stuff
}
int bitmap_write(Bitmap *bmp, const char *fn)
{
FILE *f = fopen(fn, "rb");
// write bitmap to file
return 0;
}
The design is similar to the interface to FILE *: fopen gives you a handle (or NULL; error checking is omitted in the code above) and fread, fprintf, fseek and family take a pointer to the file as argument. Finally call fclose to close the file on disk and to free any ressources fopen has claimed.
Have you tried to compile this code? There are a number of errors.
First, the type of main should always be 'int main(int argc, char *argv[])'
Second, you need to '#include <stdlib.h>' at the top of your file to get the return type of malloc/free and friends.
Third, you are not declaring 'i'.
Fourth, you are using the same name 'COORD' as both a struct name and as a variable. Don't do this, it will cause you problems.
Sending incorrect code makes it very difficult to figure out what the root of your problem is, but I suspect it's the overloading of 'COORD'.
typedef struct COORD
{
int xp;
int yp;
} coord;
coord** xy;
void allocate(coord** COORD)
{
int i;
//allocate COORD[500][460]
COORD = (coord**)malloc(sizeof(coord*)*500);
for(i=0; i<500; i++)
{
COORD[i] = (coord*)malloc(sizeof(coord)*460);
}
// freeing
for (i=0; i<500; i++) free(COORD[i]);
free(COORD);
}
//function call: allocate();
//That is the code that works
The problem is that the function allocate() cannot change the value of xy outside itself. This is because C is call by value, the called function only gets the values of its arguments, not any kind of references to the expressions in the caller's context.
It needs to be:
void allocate(coord ***c)
{
}
and:
coord **xy;
allocate(&xy);
which of course is silly: the proper design would be for allocate() to return the new address:
coord ** allocate(void)
{
}
with use like:
coord **xy = allocate();
Probably it would be even better to have the dimensions as parameters to the function, since magic numbers are generally not a good thing:
coord ** allocate(size_t width, size_t height);
typedef struct
{
int xp;
int yp;
} Coord;
Coord **xy;
Coord** allocate(size_t height, size_t width)
{
int i;
Coord **arr;
arr = malloc(sizeof(Coord*)*height);
for(i=0; i<height; i++) {
arr[i] = malloc(sizeof(coord)*width);
}
return arr;
}
void allocate2(Coord ***p_arr, size_t height, size_t width)
{
int i;
Coord **arr;
arr = *p_arr;
arr = malloc(sizeof(Coord*)*height);
for(i=0; i<height; i++) {
arr[i] = malloc(sizeof(coord)*width);
}
}
void deallocate(Coord **arr, size_t height)
{
for (i=0; i<500; i++) {
free(arr[i]);
}
free(arr);
}
int main()
{
Coord **arr_2;
Coord ***p_arr_3;
allocate2(&xy, 500, 460);
/* do something with global array, xy, e.g. */
xy[1][2].xp = 100;
xy[1][2].yp = 200;
deallocate(xy, 500);
arr_2 = allocate(500, 460);
/* do something with local array, arr_2 */
deallocate(arr_2, 500);
allocate2(p_arr_3, 500, 460);
/* do something with ptr to local array, p_arr_3 */
deallocate(*p_arr_3, 500);
return 0;
}

Declaring global variable (array) inside a function in C

I need to declare a global two-dimensional array in C.
The size of the array is determined by the width and height of a given picture.
So I first have to load the picture, and only then create the array. But if I want a variable (in this case, my array) to be global, I have to declare it at the top of the file and not inside a function.
So how can I declare a array as global when I only know its size after the execution of the main() function?
EDIT:
(I've also tried the other solutions so this comments refers to all of them)#Mimisbrunnr
First, thanks for the quick response!
I've tried but I can't see to make it work. I'm probably missing something stupid, but how does "array" becomes global? It says on test() that 'array' is undeclared
int *buffer;
int main() {
int i;
int x_size=100;
int y_size=100;
int * buffer = malloc(sizeof(int)*x_size*y_size);
int ** array = malloc(sizeof(int*)*y_size);
for(i = 0; i<y_size; i++) {
array[i]=&buffer[i*x_size];
}
array[0][1] = 5;
test();
return 0;
}
void test(){
printf("%d",array[0][1]);
}
create a global pointer and then malloc the space into it.
char * buffer;
int main(void) {
buffer = malloc( /* Width * Height */ );
}
I didn't actual execute this code, but this should get you started.
int x_size = 100;
int y_size = 100;
int ** array;
array = malloc(sizeof(int *)*y_size);
for(int i = 0; i<y_size; i++)
array[i] = malloc(sizeof(int)*x_size);
larsmans made a good point.
what about this?
int x_size = 100;
int y_size = 100;
int * buffer = malloc(sizeof(int)*x_size*y_size);
int ** array = malloc(sizeof(int *)*y_size);
for(int i = 0; i<y_size; i++)
array[i] = &buffer[i*x_size];
It looks like you might need some basic C tutorial.
int *buffer;
int **array;
int main()
{
int x_size=100;
int y_size=100;
int i;
/*int * */ buffer = malloc(sizeof(int)*x_size*y_size);
/*int ** */ array = malloc(sizeof(int*)*y_size);
for(i = 0; i<y_size; i++)
array[i]=&buffer[i*x_size];
array[0][1] = 5;
test();
return 0;
}
void test()
{
printf("%d",array[0][1]);
}
use a static variable (pointer) and allocate the array dynamically using malloc.

Resources