I'm currently working on structures for a personnal project. I'm trying to use dynamically-allocated bidimensionnal arrays, and then free the space they're taking. An error occurs when I try to free the allocated space.
I was able to narrow down the source of the issue down to the function I had set up to destroy my structures, but wasn't able to pinpoint the cause of the error within. More concerning is the fact that the error, an access violation, only triggers half of the time.
I've included the functions below, with a few comments. I'm sorry if I'm including too much code here, but I'm geniunely lost at words and do feel like the way I initialize the structures may have an impact on whether I'm destroying them correctly.
#include <stdio.h>
#include <stdlib.h>
typedef struct f_part f_part;
typedef struct field field;
//these are the structures I used:
//part of a field: includes a character and a color
struct f_part
{
char caractere;
short int color;
};
//field: points to a bidimensionnal array of f_part, and remember its size
struct field
{
int fsize_x;
int fsize_y;
f_part **fbody;
};
field* fieldInitialize(const int size_x, const int size_y) //this function seems to work correctly, I've mostly added it as an indicator
{
field* terrain = malloc(sizeof(*terrain));
if (terrain == NULL)
printf("fail1");
terrain->fsize_x = size_x;
terrain->fsize_y = size_y;
f_part* ptrToFPart = NULL;
terrain->fbody = malloc(sizeof(ptrToFPart) * size_x); //this is where I allocate the space for an array of pointers
if (terrain->fbody == NULL)
printf("fail2");
int i,j;
for (i = 0 ; i < size_x ; i++)
{
terrain->fbody[i] = malloc(sizeof(f_part) * size_y);
for (j = 0 ; j < size_y ; j++)
{
terrain->fbody[i][j].caractere = 'a';
terrain->fbody[i][j].color = 0;
}
}
terrain->fsize_x = size_x;
terrain->fsize_y = size_y;
return terrain;
}
void fieldDestroy(field* terrain) //this is the function that is supposed to destroy the object and free the memory, and fails
{
int i;
for (i = 0 ; i < terrain->fsize_x ; i++)
{
free(terrain->fbody[i]); //this part always goes well
}
printf("flag 1\n");
free(terrain->fbody); //this is where the access violation happens, when it does
printf("flag 2\n");
free(terrain); //this part goes well too
printf("flag 3\n");
}
int main()
{
field* testField = fieldInitialize(5, 5);
fieldDestroy(testField); //This is the function that fails. Sometimes.
return 0;
}
The error systematically happens on that second-to-last line, when I try to free the space allocated for the array of pointers. Except it doesn't always happen! Sometimes, I can free terrain->fbody, and everything goes well, but other times, I can't free the space.
The error I get is 0xC0000005, which apparently translates to "access violation". Which I understand would be common when dealing with dynamic memory allocation, but then why do I seem to get the error only half of the time?
Edit : Okay, so I've edited a bit of the code. Interestingly enough, while my Windows 10 would fail between flags 1 and 2, my Windows 7 fails between flags 2 and 3, and returns too a 0xC0000005 error. But again, only occasionally.
In C, you must precede structure names with struct. So your functions should be declared:
struct field* fieldInitialize( ... )
and your allocations should be
struct field* terrain = malloc(sizeof(struct field));
You could also define these structures as types:
typedef struct
{
int fsize_x;
int fsize_y;
struct f_part **fbody;
} field;
Related
I am using an array of structs and then set up the elements like the following:
#include <stdio.h>
#include <stdlib.h>
typedef struct _point {
char s;
unsigned int x;
unsigned int y;
} point;
point* vehicle;
int main(int argc, char *argv[]) {
/* 26 Vehicles */
vehicle = malloc(26*sizeof(point*));
for (int i = 0; i < 26; i++) {
vehicle[i].s = ' ';
}
/* Print already existing vehicles */
for (int i = 0; i < 26; i++) {
if (vehicle[i].s != ' ') {
printf("%c: x=%d y=%d\n", vehicle[i].s, vehicle[i].x, vehicle[i].y);
}
}
return 0;
}
NOTE: this is not the actual code (which is too big to post) but the set up of the array and structs is the same.
As you can see, I set every vehicle[i].s to the space character, but the loop prints the following (not it this example code but in my actual code):
: x=32215344 y=0
P: x=0 y=33
: x=2105376 y=0
Question: how can it be that after the first loop, some elements are modified in the "background" without assigning them in the code? Or can some other malloc operations overwrite/reallocate the memory?
The problem, as I see it is in
vehicle = malloc(26*sizeof(point*));
you're allocating memory for the pointer-to-the-data-type type, whereas you should be allocating for the data type itself.
To elaborate, you want to allocate memory for 26 elements of type point (i.e., struct _point), not 26 point *.
Change
vehicle = malloc(26*sizeof(point));
or, for better,
vehicle = malloc(26*sizeof * vehicle);
Otherwise, you're running short of allocated memory when you try to dererference the pointer to access ns of instances. So, you end up accessing out-of-bound memory which causes undefined behavior.
That said, just an advice, if you know the size to be allocated beforehand, (26, for example), don't use dynamic memory, there's no need for it. Use an array.
My code contains the struct BeforeTriag which is from type Patient** .
here is the structs and it's fields:
typedef struct{
char Id[ID_SIZE];
char Name[NAME_SIZE];
char LastName[NAME_SIZE];
char PhoneNum[PHONE_SIZE];
STATUS Status;
char Address[ADDRESS_SIZE];
}Patient;
Here is my initilization and allocation:
Patient** BeforeTriag = NULL;
int* BeforeTriagSize[1] = { 0 };
BeforeTriag = (Patient**)malloc(sizeof(Patient*));
if (!(BeforeTriag))
{
printf("ERROR!Out of memory!");
exit(1);
}
*BeforeTriag = (Patient*)malloc((PatientArraySize)* sizeof(Patient));
if (!(*BeforeTriag)){
printf("ERROR!Out of memory!");
exit(1);
}
here i'm tring to free each field in the struct:
for (i = 0; i < (*BeforeTriagSize); i++){
free((BeforeTriag)[i]->Id);
free((BeforeTriag)[i]->Name);
free((BeforeTriag)[i]->LastName);
free((BeforeTriag)[i]->Address);
free((BeforeTriag)[i]->PhoneNum);
}
free(BeforeTriag);
When I am debugging it crush on the first row of the free id:
free((BeforeTriag)[i]->Id);
What should i do to free as proper?
The individual fields within BeforeTriag[i] were not dynamically allocated by themselves, so you can't free them. You need to free the struct as a whole, because that's what was allocated:
for (i = 0; i < (*BeforeTriagSize); i++){
free(BeforeTriag[i]);
}
free(BeforeTriag);
You do not have to do this:
for (i = 0; i < (*BeforeTriagSize); i++){
free((BeforeTriag)[i]->Id);
free((BeforeTriag)[i]->Name);
free((BeforeTriag)[i]->LastName);
free((BeforeTriag)[i]->Address);
free((BeforeTriag)[i]->PhoneNum);
}
Because they are not dynamically allocated.
But you have to free BeforeTriag[i] inside the loop.
for (i = 0; i < (*BeforeTriagSize); i++){
free(BeforeTriag[i]);
}
free(BeforeTriag);
While #dbush and #RolBrok already pointed out correctly that there's no need to free the individual members, there is another bug in your code:
int* BeforeTriagSize[1] = { 0 };
This line initalizes a int ** to zero. I'm not really sure why you want this variable to be in an array, but anyway, the correct way to declare it would be
int BeforeTriagSize[1] = { 0 };
(If you only need one value for BeforeTriagSize anyway, just declare it as an int!)
Edit:
Another thing you should look closer into is the way you're allocating memory for your structs:
*BeforeTriag = (Patient*)malloc((PatientArraySize)* sizeof(Patient));
With this you're always writing to the same pointer. So when you are allocating the memory for the second struct, you are overwriting the position of the first one, basically causing a memory leak.
Consider something along the line of
BeforeTriag[BeforeTriagSize++] = (Patient*)malloc((PatientArraySize)* sizeof(Patient));
This ensures that you are writing to a new position in your array every time. (Assuming you changed BeforeTriagSize to an int - if you need to hand it over as a pointer to some functions just use the address operator (&))
I've got a problem with calling realloc on self-referencing structure. When I run this program, I get error *** Error in ...: realloc(): invalid next size: 0x0000000000602160 ***. I suppose the problem is related to the last line, because program runs without any problem if I comment it.
This is the smallest (not) working piece of code:
#include <string.h>
#include <stdlib.h>
typedef struct structure {
int connections;
struct structure *links;
} structure;
int main(int argc, char *argv[]) {
int struct_count;
int i, from, to, offset;
structure *structs;
struct_count = 2;
structs = malloc(sizeof(structure) * struct_count);
memset(structs, 0, sizeof(structure) * struct_count);
for(i = 0; i < struct_count; i++) {
structs[i].links = malloc(1);
structs[i].connections = 0;
}
for(i = 0; i < 100; i++) {
from = 0;
to = 1;
offset = structs[from].connections++;
structs[from].links = realloc(structs[from].links, sizeof(int) * (offset + 1));
structs[from].links[offset] = structs[to]; // This is the problematic line - why?
}
}
And my question: what's wrong in that code?
The problem is the first time you allocate it, it's not enough. To allocate n elements of a given type you can use
structs[i].links = malloc(n * sizeof(*structs[i].links));
and the same goes wit realloc() you also need to make sure that realloc() doesn't return NULL, suppose that after allocating space for n structures with the line above you want to resize to store n + 1 instances, then
struct structure *links;
links = realloc(structs[i].links, (n + 1) * sizeof(*links));
if (links == NULL)
{
/* Depending on the way your program is designed */
probably_free_links(&structs[i].links);
/* ^ make it `NULL' inside */
allocation_failure_do_something_about_it_but_do_not_continue();
}
structs[i].links = links;
you can make structs[i].links = NULL; initially and realloc() will behave as malloc() the first time.
Write your programs as if all errors are possible, and do something about them don't just let your program invoke undefined behavior and make it a mistery to you and your program users.
If You will increase the Blueprint of structure and decrease the size of loop it will run perfectly..
**For Example:-**without manipulating run your program just decrease the loop condition from 100 to 2. It will run perfectly.
If you want to increase loop size you have to increase the size of structure.
I am trying to use malloc to allocate heap memory for an array of struct pointers, but I cannot get it to work. Below is my code, but when I compile with gcc, I got errors like this "error: invalid type argument of ‘->’ "
The array I want to set up an array of mystruct_pointer, which should point to the actual __mystruct_t, and I think I can use "->" on its member field. Where is wrong with my code? I think it should work. thanks
typedef struct
{
int id;
bool status;
} __mystruct_t;
typedef __mystruct_t* mystruct_pointer;
mystruct_pointer struct_ptr_array;
void my_init(int number)
{
struct_ptr_array = (mystruct_pointer) malloc(sizeof(__mystruct_t) * number);
int i;
for (i = 0; i < number; i++) /* initialize the array of struct pointers */
{
struct_ptr_array[i]->id = i;
struct_ptr_array[i]->status = false;
}
}
Replace '->' by '.'. Since 'struct_ptr_array[i]' already dereference the pointer.
Problems like these come from doing obscure things. You typedef-hide a pointer and instantly you have made the program unreadable to yourself. And that's the only thing the typedef achieved. So never hide pointers with typedefs, it is very bad practice.
Other issues:
Avoid double underscore because that's reserved for compiler identifiers.
Don't cast the result of malloc, because doing so is completely pointless in C and also potentially dangerous on old C compilers.
Handle the case where malloc fails because there's no heap space available.
Never use variables with global scope, it is very bad practice that leads to spaghetti code. And there is never a reason to do so in C.
The code should be fixed as follows:
typedef struct
{
int id;
bool status;
} mystruct_t;
static mystruct_t* struct_ptr_array;
void my_init(int number)
{
struct_ptr_array = malloc(sizeof(mystruct_t) * number);
if(struct_ptr_array == NULL)
{
handle_error();
return ;
}
for (int i = 0; i < number; i++) /* initialize the array of struct pointers */
{
struct_ptr_array[i].id = i;
struct_ptr_array[i].status = false;
}
}
struct_ptr_array is a pointer, but you index it, so you get an actual __mystruct_t, not the pointer. Thus, simply use:
struct_ptr_array[i].id = i;
struct_ptr_array[i].status = false;
Replace two lines by
struct_ptr_array[i].id = i;
struct_ptr_array[i].status = false;
i have a struct "cell" defined as
typedef struct{
int id;
terrainType terrain;
} cell;
i then make a 2d array of cells with
cell** makeCellGrid(int sizeX, int sizeY)
{
cell** theArray;
int i;
theArray = (cell**) malloc(sizeX*sizeof(cell*));
for ( i = 0; i < sizeX; i++)
{
theArray[i] = (cell*) malloc(sizeY*sizeof(cell));
}
return theArray;
}
at first i thought this was working fine but a few seg faults later i discovered that with some values (e.g. makeCellGrid(32, 87) ) it breaks.
im fairly fresh with C pointers and memory junk and was hoping some one could point me in the right direction here.
with lower number bounds i had no issue accessing it with
map[i][j].id = x;
and so on
EDIT: forgot to add, from testing, the seg fault originate from
theArray[i] = (cell*) malloc(sizeY*sizeof(cell));
The code lacks error checking for the malloc() system call.
So if the first call to malloc() failed the second one (in the loop) tries to assign memory to NULL which indeed leads to the segmentation violation your are witnessing.
You might consider modifing you code like so:
#include <stdlib.h>
typedef struct {
int id;
TerrainType terrain;
} CellType;
void freeCellGrid(CellType ** ppCells, size_t sizeX)
{
size_t i = 0;
for (; i < sizeX; ++i)
{
free(ppCells[i]);
}
free(ppCells);
}
CellType ** makeCellGrid(size_t sizeX, size_t sizeY)
{
CellType ** ppCells = malloc(sizeX * sizeof(*ppCells));
if (ppCells)
{
size_t i = 0;
for (; i < sizeX; ++i)
{
ppCells[i] = malloc(sizeY * sizeof(**ppCells));
if (NULL == ppCells[i])
{
freeCellGrid(ppCells, i);
ppCells = NULL;
break;
}
}
}
return ppCells;
}
Notes on my modifications:
Always check system calls for errors (in the case of malloc() on error NULL is returned)
Better use an unsigned type to access memory/array indicies; size_t is meant for this
In C there is no need to cast the value returned by a void * function like malloc()
Always try to initialise variables as soon as possible; un-initilaised variables very easily lead to "irrational" behaviour of the application
If working with pointers, it might be helpfull to 'code' the level of indirection into their names (I did this here by using the prefix pp to indicated that it's a 2-level indirection)
types are different from variables: One way to distinguish this is by starting type names using capitals (CellType) and variables using small letters (ppCells).
If allocating memory to a pointer and it matters that the size of the allocated memory some suits the pointer's type it's always more secure to use the (dereferenced) pointer itself as argument to the sizeof operator then some type. As the declaration of the pointer the memory is allocated to might be changed during develpment and the adjustment of the argument to malloc() will be forgotten. To cut it short: doing as I did is less error prone.
If encapsulating the dynamical creation of structures (including arrays) it is a could idea to also implement a method which de-allocates it (here: freeCellGrid()). Even better start of with coding this deallocator first, as then you have it by hand when coding the allocator's error handling (as shown for the second call to malloc()).