Thread: *void allocating

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    14

    *void allocating

    Hi, firstly I forgot to thank you in some thread about allocating the struct... So I thank everyone who helped me improve my code...
    It will be very similiar right now, cause my program have to run under unix and I have some serious problems with my Putty right now... (should install some linux)

    Point is all functions are valid, its just all about the while cycle... cause I am afraid of memory leaks

    Code:
    //void* SQLgetLineFromTable(&listOfTables->table); returns null if end of file
    
    //void SQLinsertLineToTable(SQLtable* table,*void,t_size);
    
    void* d_data= SQLgetLineFromTable(&listOfTables->table);        SQLtable* tabulka = SQLcreateTable();
    
    
            while (d_data!=NULL){
            SQLinsertLineToTable(tabulka,d_data,SQL_DATA_SIZE);
            d_data = SQLgetLineFromTable(&listOfTables->table);
            }
    Should I allocate d_data and still reallocate them or its fine like this? Or is there any other way so I can controll end of file through SQLgetLineFromTable(&listOfTables->table); which returns null when reaches the EOF

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    Havent found anything to edit my previous post

    So I wanna allocate memory insade the structere, I wanna allocate it for SQLtable* table in SQLresult->data->table

    Any Ideas how to allocate memory for struct in struct
    Thanks
    Code:
    typedef struct {	SQLtypeOfResult type;
    	union {
    		int number;
    		SQLtable* table;
    	} data;
    } SQLresult;
    //i allocate memory for the struct
    SQLresult* pResult = malloc (sizeof(SQLresult));
    //this will allocate a memory just for table   
     SQLtable* tabulka = malloc (sizeof(SQLtable));
    
    //this doesnt work cause data is anonymous
    (pResult->data->table)* tabulka = malloc (sizeof(SQLtable));
    
    //SQLtable is just another struct

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by flegmik View Post
    Havent found anything to edit my previous post
    There's a 1 hour time limit on edits.

    So I wanna allocate memory insade the structere, I wanna allocate it for SQLtable* table in SQLresult->data->table

    Any Ideas how to allocate memory for struct in struct
    Thanks
    Code:
    typedef struct {    SQLtypeOfResult type;
        union {
            int number;
            SQLtable* table;
        } data;
    } SQLresult;
    //i allocate memory for the struct
    SQLresult* pResult = malloc (sizeof(SQLresult));
    //this will allocate a memory just for table   
     SQLtable* tabulka = malloc (sizeof(SQLtable));
    
    //this doesnt work cause data is anonymous
    (pResult->data->table)* tabulka = malloc (sizeof(SQLtable));
    
    //SQLtable is just another struct
    Try
    Code:
    pResult = malloc(sizeof(*pResult));  // this is a little safer, if the type of pResult changes, this will still allocate the correct number of bytes
    pResult->data.table = malloc(sizeof(*(pResult->data.table)));

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    yeah thanks. i have one lame question... if I have buffer, lets say
    Code:
     char* buffer = "this is my sentence";
               char* sth = buffer;
                *(sth+5) = '\0';
               free(buffer);
    Is this correct? Would valgrind say no memory leaks? Thanks for the help above.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Where did that come from? No, it's not a memory leak, but it's invalid. buffer points to a string constant. So does sth after line 2. You try to change the 5th char in that constant, but you can't because it's a constant. You need an array or malloc'ed memory for that to work. The call to free is also invalid. You have to give free an address that was returned from malloc. I think you should do some serious reading on structs, unions, pointers, strings and dynamic memory. We have a tutorial here (C, C++ Programming Tutorials - Cprogramming.com), and Google has lots more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-08-2011, 11:18 PM
  2. Replies: 12
    Last Post: 03-27-2009, 02:36 PM
  3. Passing a variable in void to another void function
    By stevedawg85 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 06:17 PM
  4. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 10:30 AM
  5. What is this mean? static void *name (void *data)
    By beyonddc in forum C++ Programming
    Replies: 14
    Last Post: 05-05-2004, 03:06 PM