Thread: free problem with struct

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    31

    Question 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

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    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.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    31
    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....

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    102

    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;
    }
    Saravanan.T.S.
    Beginner.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    31
    but i want to free id_client...

    what i have to do ???

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    31
    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......

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    31
    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 ...

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    31
    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...

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please use code tags when posting code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with struct?
    By cangel in forum C Programming
    Replies: 8
    Last Post: 09-27-2008, 11:35 PM
  2. pointer problem or so...
    By TL62 in forum C Programming
    Replies: 19
    Last Post: 01-12-2008, 11:45 PM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. very weird problem (pointers I think)
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 10-11-2005, 06:45 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM