0

Another one in my series of problems with this code. I have below function which is comparing arg with every string in the array of strings reference :

char compare(char *arg)
{
        int iter=0;
        char retchar='0';

        while(iter < no_of_ref)
        {
        //      printf("arg : %s , reference : %s \n",arg,reference[iter]);
                if((strstr(reference[iter],arg) != NULL) || (strstr(arg,reference[iter]) != NULL))
                {
                        retchar='1';
                        break;
                }
          iter++;
        }
return retchar;
}

reference is global char ** , but built up dynamically inside main as below:

reference = calloc(CHUNK, sizeof(char *));

Then some code, then:

                        temp_in[pre_pip+1]='\0';
                        reference[no_of_ref]=malloc(strlen(temp_in) + 1);
                        strcpy(reference[no_of_ref++],temp_in);
                        memset(&temp_in,'\0',sizeof(temp_in));
                        pre_pip = -1;
   printf("INDEX: %d, address : %p , val : %s\n",no_of_ref-1,reference[no_of_ref-1],reference[no_of_ref-1]);      //DEBUG
                }
                /*If allocated buffer is at brim, extend it for CHUNK char *  further*/
                if(no_of_ref == (tr*CHUNK - 2))
                {
                        current_size = tr*CHUNK*sizeof(char *);

                        char *retalloc = realloc(reference,current_size + CHUNK*sizeof(char *));
                                if(retalloc == NULL)
                                        perror("ERROR on realloc");
                                else
                                {
                                        printf("Realloced successfully: %p\n",retalloc);
                                        tr++;
                                }

The code running fine for test case where no need to realloc arises, i.e. Number of input strings is less than CHUNK. In case of realloc, I'm getting SEGFAULT from function. Below is for one of the run:

Program terminated with signal 11, Segmentation fault.
#0  __strstr_sse42 (s1=0x3839393433333230 <Address 0x3839393433333230 out of bounds>, s2=0x6020c0 <cmp> "8956549122") 
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Diwakar Sharma
  • 415
  • 1
  • 9
  • 26

2 Answers2

1

You need to put parenthesis for expression in realloc() as

//---------------------------------v -------------------v
char *retalloc = realloc(reference,(current_size + CHUNK)*sizeof(char *));

Assume CHUNK=100 and current_size=200, your code will allocate 200+100*8=1000 bytes while you want (200+100)*8 = 2400 bytes

Also, make sure you assign retalloc to reference variable after reallocation.

Rohan
  • 52,392
  • 12
  • 90
  • 87
  • agree with the reassigning part, but I think you are getting the sizing wrong. Whatever be the current size I just want space for CHUNK more char * . so CHUNK*sizeof(char *) bytes + current size bytes. – Diwakar Sharma Nov 05 '13 at 07:30
1

When realloc actually reallocates the memory you pass to it, then that pointer you pass as an argument still points to the old memory area. The realloc function returns a pointer to the new memory, so you have to assign that to e.g. reference.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I tried like this, since reference is a char ** : printf("Realloced successfully: %p\n",retalloc); retalloc=*reference; tr++; but no luck – Diwakar Sharma Nov 05 '13 at 07:13
  • @DiwakarSharma The other way around: `reference = (char **) retalloc;` *after* the call to `realloc. (The cast is needed since `retalloc` is a `char*`) – Some programmer dude Nov 05 '13 at 07:17
  • Now this has cleared SEGFAULT..!! but how does it work I am confused. retalloc will point to the start of newly allocated memory. When we assign that to reference, so reference will loose original position right? – Diwakar Sharma Nov 05 '13 at 07:23
  • @DiwakarSharma Yes, but `realloc` allocates new memory (the pointer it returns), copies the old memory (previously pointed to by `reference`), and then *frees* the old memory. You might want to read e.g. [this reference](http://en.cppreference.com/w/c/memory/realloc). – Some programmer dude Nov 05 '13 at 07:25
  • you are a saviour as always..! :) – Diwakar Sharma Nov 05 '13 at 07:28