Thread: pntr values move

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    16

    pntr values move

    I am trying to allocate values at two different stages of the code
    Code:
    //FIRST STAGE
    pntr[0]->srvr->stage->final=(unihash*)malloc(sizeof(unihash)*(strlen(line2))*(cnt+2));
    strcpy(pntr[0]->srvr->stage->final[cnt].key,first);
    strcpy(pntr[0]->srvr->stage->final[cnt].value,secon);
    free(pntr[0]->srvr->stage->final);
    cnt++;
    
    // SECOND STAGE
    pntr[1]->srvr->stage->final = malloc(sizeof(unihash)*(9600)+1);
    strcpy(pntr[1]->srvr->stage->final[cnt].key,first);
    strcpy(pntr[1]->srvr->stage->final[cnt].value,secon);
    free(pntr[1]->srvr->stage->final);
    cnt++;
    I find that when I try to access the pntr[1]->srvr->stage->final, all the key-values assigned to pntr[0]->srvr->stage->final[cnt].value
    have moved to the pntr[1]->srvr->stage->final.
    Code:
       
    for(n=0 ; n < d; n++){
    printf(" %d %d pntr[1]->bee->stg->maindbm %s %s \n",m,n,pntr[1]->bee->stg->maindbm[n].key,pntr[1]->bee->stg->maindbm[n].value);
                 }
    Is there any way I can keep the contents of pntr[0]->srvr->stage->final away from pntr[1]->srvr->stage->final.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    free(pntr[0]->srvr->stage->final);
    If you are going to use pntr[0]->srvr->stage->final after this point, then you should not free it. Likewise for pntr[1]...->final - do not free it if you are going to use it later on.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    16
    hi Kernel hacker,
    I tried not freeing the
    // free(pntr[0]->srvr->stage->final);
    But I still get all the values moved over to
    free(pntr[1]->srvr->stage->final);

    Any other points that you can suggest....?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    My guess would be that some other part of your (rather long) chain of pointers is also pointing to the wrong place or freed memory that is later on used for something else. Try stopping the second stage in the debugger, and compare the pointers at the different levels - if a pointer points to the same place in both pntr[1] and pntr[0], then that's obviously wrong [unless you really want to have the same information in both].


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    16
    hi,
    I have a little update.
    I am pretty much using the same code as above (I commented the free statements)
    I am able to get a saperate key-values for the
    pntr[0]->srvr->stage->final
    and
    pntr[1]->srvr->stage->final

    But now the problem is that the pntr[0]->srvr->stage->final
    is able to save the list assigned to it and the
    pntr[1]->srvr->stage->final has only the last of key value pairs assigned to it.

    Could you suggest any thing from your vast experience....?

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    16
    What I am saying is that the pntr[1]->srvr->stage->final is able to store only the last value assigned instead of the 7 key-values assigned to it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program moving values in vectors... need help.
    By m_ziolo_86 in forum C Programming
    Replies: 11
    Last Post: 01-15-2006, 10:50 PM
  2. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 01:30 PM
  3. adding ASCII values
    By watshamacalit in forum C Programming
    Replies: 1
    Last Post: 12-26-2002, 07:16 PM
  4. How to read in empty values into array from input file
    By wpr101 in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2002, 10:59 PM
  5. Replies: 5
    Last Post: 09-08-2001, 09:18 AM