Thread: realloc realloc realloc

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    71

    realloc realloc realloc

    Perhaps this will make the code I posted a little while ago easier to understand:

    PHP Code:
    /* if srchitem is found in the array, its position is 
    returned, however many times it occurs (the positions 
    are to be written to 'foundpositions' array). 
    size is the size of the array, fsize is the size of 
    the positions array. */ 

    int *seqsearch (double *aradouble srchitemint size) { 
       
    int *foundpositions
       
    int fsize 0// size of foundpositions 
       
    for ( int x=0x<sizex++) 
          if (*(
    ara+x) == srchitem) { 
          
    foundpositions = (int *) realloc (foundpositions, ( (fsize+1)*sizeof(int) )); 
          
    // allocate memory to foundpositions array 
          // as elements matching srchitem are found. 
          
    fsize ++; 
          *(
    foundpositions+x) = x
       } 
       return (
    foundpositions); 


  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Try initialising your foundpositions to NULL.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    Doesn't realloc initialize it to null the first time anyway?

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    71

    Thumbs up

    No, that actually worked, thanks!

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >Doesn't realloc initialize it to null the first time anyway?

    realloc doesn't have to move the allocated block. If it initialised the pointer to NULL everytime then it may be destroying your previously allocated data. How does it know that it's the first time?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. did i understood right this explantion of realloc..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 10-24-2008, 07:26 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. using realloc
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 12-06-2005, 05:00 PM
  4. segfault on realloc
    By ziel in forum C Programming
    Replies: 5
    Last Post: 03-16-2003, 04:40 PM
  5. Realloc inappropriate for aligned blocks - Alternatives?
    By zeckensack in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 02:10 PM