Thread: pointer to pointer realloc problem

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    10

    pointer to pointer realloc problem

    hi everyone,

    i have another problem with realloc with a pointer to a pointer,

    here is the code:
    Code:
    typedef struct posPoolspace{
    	char **posList;
    	int listSize;
    }posPool;
    
    typedef struct trieCDT {
    	posPool *pool;
    }trieCDT;
    
    typedef struct trieCDT *Trie;
    later in my code i did like this to realloc
    Code:
           char *plist=*(trie->pool->posList);
    	plist =realloc(plist,(trie->pool->listSize+1)*sizeof(100));
    	strcpy(plist+trie->pool->listSize,val);
    i get Segmentation Fault .. what am i doing worng??

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Underlying assumption is that the code has been compiled with debugging on.
    Did you run it through a symbolic debugger to pinpoint the source of the problem.
    The posted code is missing definitions/declarations of the key variables involved.

    Edit:
    shouldn't
    char *plist=*(trie->pool->posList);
    really be
    char *plist=*(Trie->pool->posList);
    Last edited by itCbitC; 04-05-2009 at 07:58 PM.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    10
    Sorry, i missed some codes

    Code:
    typedef struct posPoolspace{
    	char **posList;
    	int listSize;
    }posPool;
    
    typedef struct trieCDT {
    	posPool *pool;
    }trieCDT;
    
    typedef struct trieCDT *Trie;
    Also, initialization
    Code:
    	
    Trie createTrie(){
    	Trie trie;
    	trie = (Trie)malloc(sizeof(struct trieCDT));
    	trie->pool= (posPool*)malloc(sizeof(posPool));
    	trie->pool->listSize=2;
    	return trie;
    }
    later in my code i did like this to realloc
    Code:
    
    main(){
           .........
          Trie trie;
            trie= createTrie();
            addTrie(trie,"2565");
    }
    
    addTrie(Trie trie,char* val){
           ..........
            .......
            char *plist=*(trie->pool->posList);
    	plist =realloc(plist,(trie->pool->listSize+1)*sizeof(100));
    	strcpy(plist+trie->pool->listSize,val);
           trie->pool->listSize++;
           ........
          ......... 
    }
    the error occurs at realloc point
    Last edited by prakash0104; 04-05-2009 at 08:16 PM.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by prakash0104 View Post
    Code:
    typedef struct posPoolspace{
    	char **posList;
    	int listSize;
    }posPool;
    I've not seen that syntax before. Is it valid? If so, which is the typename, posPoolspace or posPool?

    Shouldn't struct syntax be either
    Code:
    struct posPoolspace{
    	char **posList;
    	int listSize;
    }posPool;
    or
    Code:
    typedef struct{
    	char **posList;
    	int listSize;
    }posPoolspace;
    posPoolspace posPool;

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Check the return value of realloc() to make sure it's not returning a NULL pointer.
    Post the debugger output used for locating the root cause of the problem.
    Perhaps post the entire code if it ain't too large.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    10
    itCbitC & R.Stiltskin thanks so much,,,
    i solved the error with some changes

    Code:
    	char **plist=(trie->pool->posList);
    	plist =realloc(plist,(trie->pool->listSize+1)*sizeof(char*));
    	if(plist==NULL)
    		printf("null!!");
    	*(plist+trie->pool->listSize)=malloc(100);
    	strcpy(*(plist+trie->pool->listSize),val);
    	trie->pool->posList=plist;
    some problems with the pointers..
    BTW,
    i want to ask 2 things,
    1, is the nametype declaration an error as R.Stiltskin said?
    2. i read the realloc may move the memory block to a new location is that a big memory issue,, my program has to handle a large data ... i ma bit confused whether to use it or not..

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by R.Stiltskin
    I've not seen that syntax before. Is it valid? If so, which is the typename, posPoolspace or posPool?
    Yes, it is valid and quite commonly used. The name of the type is struct posPoolspace, while the alias of the type is posPool. No objects of the type are created at this point.

    Quote Originally Posted by prakash0104
    1, is the nametype declaration an error as R.Stiltskin said?
    No, it is not an error.

    Quote Originally Posted by prakash0104
    2. i read the realloc may move the memory block to a new location is that a big memory issue,, my program has to handle a large data ... i ma bit confused whether to use it or not..
    How large is large? Take into account that you are allocating an array of pointers so the size of each element is relatively small.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    10
    thanks for the reply,
    the size is around 1 million...

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by prakash0104 View Post
    BTW,
    i want to ask 2 things,
    1, is the nametype declaration an error as R.Stiltskin said?
    no it's not as laserlight as already noted.
    Quote Originally Posted by prakash0104 View Post
    2. i read the realloc may move the memory block to a new location is that a big memory issue,, my program has to handle a large data ... i ma bit confused whether to use it or not..
    you will run into issues only if memory is fragmented in which case the OS won't be able to satisfy the request and return a NULL pointer; only other option to not using it is to allocate a large block statically.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by prakash0104 View Post
    thanks for the reply,
    the size is around 1 million...
    How many instances of plist does that correspond to?

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    10
    there are about 500,000 for the plist,

    here is the whole code, at first i programmed with static array ,, then i wanted to change it to dynamic,,, this is why i got the error,,

    i still get error

    pleaseeeeeeee,, help me find it


    the program basically creates trie and adds data to it.

    In the main function i have careated 2 tries . First, create first trie , add elements then destroy it , then the second trie. The problem is the second time the trie is created , data added and destroyed i have problem... how did the two instances of trie get tangled?

    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef int Value;
    typedef struct trieCDT *Trie;
    
    typedef struct posPoolspace{
    	char **posList;
    	int listSize;
    }posPool;
    
    typedef struct trieCDT {
    	posPool *pool;
    }trieCDT;
    
    Trie createTrie(){
    	Trie trie;
    	trie = malloc(sizeof(struct trieCDT));
    	trie->pool= malloc(sizeof(struct posPoolspace));
    	trie->pool->listSize=2;
    	return trie;
    }
    
    void addTrie(Trie trie, char *keys,char *val){
    	
    	Value value=0;
    	
    	if(trie == NULL){
    		trie= createTrie();
    	}
    
    	char **plist=(trie->pool->posList);
    	plist =realloc(plist,(trie->pool->listSize+1)*sizeof(char*));
    	if(plist==NULL){
    		printf("Error realloc couldn't allocate heap space!!");
    		exit(0);
    	}
    	*(plist+trie->pool->listSize)=malloc(200);
    	strcpy(*(plist+trie->pool->listSize),val);
    	trie->pool->posList=plist;
    
    	value=trie->pool->listSize;
    	trie->pool->listSize++;
    }
    
    void destroyTrie(Trie trie){
    
    	posPool *pool =trie->pool;
    	int i=0;
    	char **plist=(trie->pool->posList);
    	for(i=0;i<pool->listSize;i++){
    		free(plist[i]);
    	}
    	free(trie->pool->posList);
    	free(trie->pool);
    	free(trie);
    }
    
    main(){
    
    	//Create First Trie
    	Trie trie;
    	trie = createTrie();
    		
    	addTrie(trie, "abf", "56");
    	addTrie(trie, "abcde", "56");
    	addTrie(trie, "prameshh", "26");
    	addTrie(trie, "pramesh", "15");
    	
    	destroyTrie(trie);
    
    	//Create Second Trie
    	
    	Trie trie2;
    	trie2 = createTrie();
    	
    	
    	addTrie(trie2, "abf", "56");
    	addTrie(trie2, "abcde", "56");
    	addTrie(trie2, "prameshh", "26");
    	addTrie(trie2, "pramesh", "15");
    
    	destroyTrie(trie2);
    	
    }
    error

    *** glibc detected *** ./trie: free(): invalid pointer: 0xb7f593a0 ***
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6[0xb7e6d454]
    /lib/tls/i686/cmov/libc.so.6(cfree+0x96)[0xb7e6f4b6]
    ./trie[0x80488ab]
    ./trie[0x8048b91]
    /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe5)[0xb7e14685]
    ./trie[0x80484a1]
    ======= Memory map: ========
    08048000-08049000 r-xp 00000000 08:07 7215635 /home/praka
    Last edited by prakash0104; 04-06-2009 at 06:38 PM.

  12. #12
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    you should keep in mind that when using free() function, you should pass the base address of the dynamic memory allocated.

    for example.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
       char *a;
       a=malloc(10);
       a+=5;         //moving  
       free(a);       //this will cause an error as a does not point at base address of a.
       return 0;      //this will never be executed.
    }
    OP:
    Code:
    [c_d@localhost c scratchpad]$ ./a.out
    *** glibc detected *** ./a.out: free(): invalid pointer: 0x0834e00d ***
    ======= Backtrace: =========
    /lib/libc.so.6[0x48b3a4]
    /lib/libc.so.6(cfree+0x96)[0x48d356]
    ./a.out[0x8048423]
    /lib/libc.so.6(__libc_start_main+0xe5)[0x4326e5]
    ./a.out[0x8048361]
    ======= Memory map: ========
    00110000-00111000 r-xp 00110000 00:00 0          [vdso]
    003f7000-00417000 r-xp 00000000 08:02 805385327  /lib/ld-2.9.so
    00418000-00419000 r--p 00020000 08:02 805385327  /lib/ld-2.9.so
    00419000-0041a000 rw-p 00021000 08:02 805385327  /lib/ld-2.9.so
    0041c000-0058a000 r-xp 00000000 08:02 808903508  /lib/libc-2.9.so
    0058a000-0058c000 r--p 0016e000 08:02 808903508  /lib/libc-2.9.so
    0058c000-0058d000 rw-p 00170000 08:02 808903508  /lib/libc-2.9.so
    0058d000-00590000 rw-p 0058d000 00:00 0 
    00a6c000-00a79000 r-xp 00000000 08:02 808904130  /lib/libgcc_s-4.3.2-20081105.so.1
    00a79000-00a7a000 rw-p 0000c000 08:02 808904130  /lib/libgcc_s-4.3.2-20081105.so.1
    08048000-08049000 r-xp 00000000 08:02 1151873    /home/c_d/workspace/c scratchpad/a.out
    08049000-0804a000 rw-p 00000000 08:02 1151873    /home/c_d/workspace/c scratchpad/a.out
    0834e000-0836f000 rw-p 0834e000 00:00 0          [heap]
    b7e00000-b7e21000 rw-p b7e00000 00:00 0 
    b7e21000-b7f00000 ---p b7e21000 00:00 0 
    b7fda000-b7fdb000 rw-p b7fda000 00:00 0 
    b8001000-b8002000 rw-p b8001000 00:00 0 
    bfaec000-bfb01000 rw-p bffeb000 00:00 0          [stack]
    Aborted
    [c_d@localhost c scratchpad]$
    and now see this
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
       char *a;
       a=malloc(10);
       a+=5;            //moving  address
       a-=5;            //go back to base address   
       free(a);
       printf("freed");       
       return 0;      
    }
    OP:
    Code:
    [c_d@localhost c scratchpad]$ gcc temp4.c
    [c_d@localhost c scratchpad]$ ./a.out
    freed[c_d@localhost c scratchpad]$
    Last edited by creeping death; 04-06-2009 at 07:56 AM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  13. #13
    Registered User
    Join Date
    Dec 2008
    Posts
    10
    I am not sure if that is the problem... as you can see the tries in the main() function are created and destroyed twice,,, the first destroy doesn't produce error just the second one(last line of code in the program) produces error,,, so i am thinking i have some problems with pointers... please give me some directions~~~

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Compile source code with the "-g" option.
    Run executable on the command line.
    Pass the corefile and objfile to gdb.
    Post output of the gdb run here.

  15. #15
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    This is probably not what you want to end up with, but changing the initial value of i in the for loop will stop it from crashing.

    Code:
    void destroyTrie(Trie trie){
    
    	posPool *pool =trie->pool;
    	int i=0;
    	char **plist=(trie->pool->posList);
    	for(i=2;;i<pool->listSize;i++){     // here
    		free(plist[i]);
    	}
    	free(trie->pool->posList);
    	free(trie->pool);
    	free(trie);
    }
    You are crashing because in addTrie() you begin allocating posList memory at *(posList+2) but in destroyTrie() you are freeing beginning at *posList.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with function's pointer!
    By Tirania in forum C Programming
    Replies: 5
    Last Post: 11-28-2008, 04:50 AM
  2. printf output makes no sense. Is it a Pointer problem?
    By officedog in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 09:01 AM
  3. Replies: 3
    Last Post: 08-22-2008, 11:12 AM
  4. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM