-
free problem with struct
hi i have a problem when i use free with a structure inside a structure ...this is the code...
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
char *table_name;
int id_client[10];
} TABLE;
typedef struct
{
char *db_name;
TABLE *db_table;
} DB_RESOURCE;
int main(int argc, char **argv)
{
DB_RESOURCE *db_list;
char dbname[] = "database1";
char dbtable[] = "table1";
db_list = (DB_RESOURCE *)malloc(sizeof(DB_RESOURCE)); // te lo dichiara di 1
db_list[0].db_name = (char *) malloc(sizeof(dbname));
db_list[0].db_table = (TABLE *) malloc(sizeof(TABLE));
db_list[0].db_table[0].table_name = (char *) malloc(sizeof(dbtable));
strcpy(db_list[0].db_name, "database1");
strcpy(db_list[0].db_table[0].table_name, "table1");
db_list[0].db_table[0].id_client[0] = 28;
printf("\n%s\n", db_list[0].db_name);
printf("\n%s\n", db_list[0].db_table[0].table_name);
printf("\n%ld\n", db_list->db_table->id_client[0]);
free(db_list[0].db_name);
free(db_list[0].db_table[0].table_name);
free(db_list->db_table->id_client);
printf("\n**** before the free ****\n");
printf("\n%s\n", db_list[0].db_name);
printf("\n%s\n", db_list[0].db_table[0].table_name);
printf("\n%ld\n", db_list[0].db_table[0].id_client[0]);
return 0;
}
and when i execute the program the latest free() does'nt have effect in fact the output is :
database1
table1
28
**** dopo la free ****
database1
hó@
28
help please....i don't know because i have this problem.....
Please use [code][/code]Tags
-
Code:
int id_client[10];
....
free(db_list->db_table->id_client);
The free function deallocates a memory block (memblock) that was previously allocated by a call to calloc, malloc, or realloc.
So your code is wrong.
-
so in this way the code is right???
free(db_list[0].db_table[0].id_client);
free(db_list[0].db_table[0].table_name);
free(db_list[0].db_name);
thx anyway.... :)
-
Suggestions
Hi,
Execute this code. You will not get error i hope. Some corrections are made explanations are commented.
code is
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
char *table_name;
int id_client[10];
} TABLE;
typedef struct
{
char *db_name;
TABLE *db_table;
} DB_RESOURCE;
int main(int argc, char **argv)
{
DB_RESOURCE *db_list;
char dbname[] = "database1";
char dbtable[] = "table1";
db_list = (DB_RESOURCE *)malloc(sizeof(DB_RESOURCE)); // te lo dichiara di 1
db_list->db_name = (char *) malloc(sizeof(dbname));
db_list->db_table = (TABLE *) malloc(sizeof(TABLE));
db_list->db_table->table_name = (char *) malloc(sizeof(dbtable));
strcpy(db_list->db_name, "database1");
strcpy(db_list->db_table->table_name, "table1");
db_list->db_table->id_client[0] = 28;
printf("\n%s\n", db_list->db_name);
printf("\n%s\n", db_list->db_table->table_name);
printf("\n%ld\n", db_list->db_table->id_client[0]);
//free(db_list->db_table->id_client);//error is here
free(db_list->db_table->table_name);
free(db_list->db_name);
//You should also include this
free(db_list->db_table);
printf("\n**** before the free ****\n");
//You cannot do this after freeing
/*printf("\n%s\n", db_list->db_name);
printf("\n%s\n", db_list->db_table->table_name);
printf("\n%ld\n", db_list->db_table->id_client[0]);*/
return 0;
}
-
but i want to free id_client...
what i have to do ???
-
but in this way i lost also char *table_name and i don't want to lost this information.... i want only to free data of id_client not of all struct TABLE......
-
i tried also in this way ubt the result is always the same...and i don't know how...
by me the problem is that there is a struct inside a struct and (i don't know for which problem) there is some problem...
plz help me ...
-
this is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define FD_SET_SIZE 11
typedef struct
{
char *table_name;
int *id_client;
} TABLE;
typedef struct
{
char *db_name;
TABLE *db_table;
} DB_RESOURCE;
DB_RESOURCE* newEntry (char dbname[], char tablename[], int id);
int main(int argc, char **argv)
{
DB_RESOURCE *db_list;
char dbname[] = "database1";
char dbtable[] = "table1";
db_list = newEntry(dbname, dbtable, 28);
printf("\n%s\n", db_list[0].db_name);
printf("\n%s\n", db_list[0].db_table[0].table_name);
// printf("\n%ld\n", db_list[0].cazzo[0]);
printf("\n%ld\n", db_list[0].db_table[0].id_client[0]);
free(db_list[0].db_table[0].id_client);//error is here
free(db_list[0].db_table[0].table_name);
free(db_list[0].db_name);
//You should also include this
//free(db_list->db_table);
printf("\n**** after the free ****\n");
printf("\n%s\n", db_list[0].db_name);
printf("\n%s\n", db_list[0].db_table[0].table_name);
printf("\n%ld\n", db_list[0].db_table[0].id_client[0]);
return 0;
}
DB_RESOURCE* newEntry (char dbname[], char tablename[], int id)
{
DB_RESOURCE *db_list;
db_list = (DB_RESOURCE *)malloc(sizeof(DB_RESOURCE)); // te lo dichiara di 1
db_list[0].db_name = (char *) malloc(sizeof(dbname));
db_list[0].db_table = (TABLE *) malloc(sizeof(TABLE));
db_list[0].db_table[0].table_name = (char *) malloc(sizeof(tablename));
db_list[0].db_table[0].id_client[0] = (int) malloc( 10 * sizeof(int));
db_list[0].db_table[0].id_client[0] = id;
strcpy(db_list[0].db_name, dbname);
strcpy(db_list[0].db_table[0].table_name, tablename);
db_list[0].db_table[0].id_client[0] = id;
return db_list;
}
i've just added a function that do the mallocand the assignments...
thx...
-
Please use code tags when posting code.