Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. realloc function problem

realloc function problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ayman Mashal
    wrote on last edited by
    #1

    Hi i have a function that gets a parameter of type (char **ptr) i want to reallocate the space for this pointer (reduce or increase) i have the followig code that raise an error when realloc change the original pointer **(*ptr)=(char *)realloc(*ptr,(originalLength+shift) * sizeof(char));** what is the problem in this line note : its work fine when realloc returns the same pointer ? thanks

    B P 2 Replies Last reply
    0
    • A Ayman Mashal

      Hi i have a function that gets a parameter of type (char **ptr) i want to reallocate the space for this pointer (reduce or increase) i have the followig code that raise an error when realloc change the original pointer **(*ptr)=(char *)realloc(*ptr,(originalLength+shift) * sizeof(char));** what is the problem in this line note : its work fine when realloc returns the same pointer ? thanks

      B Offline
      B Offline
      blue_rabbit
      wrote on last edited by
      #2

      I have a idea. Are you sure ptr is not null (0) before you call realloc. Because if ptr is NULL (0) then when you call *ptr is wrong. you can add a code to report that ptr is null like this: if(!ptr) MessageBox(0,"ptr is null","error",0);

      A 1 Reply Last reply
      0
      • A Ayman Mashal

        Hi i have a function that gets a parameter of type (char **ptr) i want to reallocate the space for this pointer (reduce or increase) i have the followig code that raise an error when realloc change the original pointer **(*ptr)=(char *)realloc(*ptr,(originalLength+shift) * sizeof(char));** what is the problem in this line note : its work fine when realloc returns the same pointer ? thanks

        P Offline
        P Offline
        prasad_som
        wrote on last edited by
        #3

        Can you show how this memory is previously allocated, and used (code)? Possibly, its getting corrupted due to using it wrong way.

        Prasad Notifier using ATL | Operator new[],delete[][^]

        A 1 Reply Last reply
        0
        • B blue_rabbit

          I have a idea. Are you sure ptr is not null (0) before you call realloc. Because if ptr is NULL (0) then when you call *ptr is wrong. you can add a code to report that ptr is null like this: if(!ptr) MessageBox(0,"ptr is null","error",0);

          A Offline
          A Offline
          Ayman Mashal
          wrote on last edited by
          #4

          no its not null because when i debug the code *ptr contains a string . and kindly note the the realloc code that i added is used in many calls before for the same array and its work fine when the array size is in smaller size but if i added some characters to it then the allocation failed and i get the error (if i added one character !!) i think this happen when realloc moves the array to another place and returns a new pointer ?

          B 1 Reply Last reply
          0
          • A Ayman Mashal

            no its not null because when i debug the code *ptr contains a string . and kindly note the the realloc code that i added is used in many calls before for the same array and its work fine when the array size is in smaller size but if i added some characters to it then the allocation failed and i get the error (if i added one character !!) i think this happen when realloc moves the array to another place and returns a new pointer ?

            B Offline
            B Offline
            blue_rabbit
            wrote on last edited by
            #5

            yeh, so how you add character to this pointer. If possible, you can copy your code and paste here. It is hard to explain if you only talk without code. Ah, I think maybe you allocated memory before for *ptr by another function like *ptr = new char[]. Don't you do that. If it's, it's wrong. -- modified at 3:41 Sunday 4th March, 2007

            A 1 Reply Last reply
            0
            • B blue_rabbit

              yeh, so how you add character to this pointer. If possible, you can copy your code and paste here. It is hard to explain if you only talk without code. Ah, I think maybe you allocated memory before for *ptr by another function like *ptr = new char[]. Don't you do that. If it's, it's wrong. -- modified at 3:41 Sunday 4th March, 2007

              A Offline
              A Offline
              Ayman Mashal
              wrote on last edited by
              #6

              the array is created by adding characters using the following function void Append(char **buffer,int size,char c) { (*buffer)=(char *)realloc(*buffer,(size+1) * sizeof(char)); (*buffer)[size-1]=c; (*buffer)[size]='\0'; } after the array is created i am running the function that raise the error several times and its work fine and i increase and decrease its content using the following function : void replaceStr(char **replaceIn,char *replaceWith,int fromIndx,int toIndx) { char *ptr=(*replaceIn)+fromIndx; int replaceWithLen=(replaceWith==NULL)?0:strlen(replaceWith); int replacedStrLen=toIndx-fromIndx; int originalStrLen=strlen(*replaceIn); //int str3Len=strlen(str3); int shift=replaceWithLen-replacedStrLen; int bytesToMove=strlen(ptr)+1; int originalLength; printf("%s\n",*replaceIn); if(replaceWithLen==0) { memmove(ptr-1,ptr+replacedStrLen,bytesToMove); } else { if(replaceWithLen>replacedStrLen) { originalLength=strlen(*replaceIn); **(*replaceIn)=(char *)realloc(*replaceIn,(originalLength+shift+1) * sizeof(char));** if(*replaceIn==NULL) printf("Error while allocating memory"); *((*replaceIn)+originalStrLen)='\0'; ptr=(*replaceIn)+fromIndx; memmove(ptr+replaceWithLen,ptr+replacedStrLen,bytesToMove); } else memmove(ptr+replaceWithLen,ptr+replacedStrLen,bytesToMove); } memcpy(ptr,replaceWith,replaceWithLen); } the error happen at some point in the line in bold ! -- modified at 4:21 Sunday 4th March, 2007

              B 1 Reply Last reply
              0
              • P prasad_som

                Can you show how this memory is previously allocated, and used (code)? Possibly, its getting corrupted due to using it wrong way.

                Prasad Notifier using ATL | Operator new[],delete[][^]

                A Offline
                A Offline
                Ayman Mashal
                wrote on last edited by
                #7

                the array is created by adding characters using the following function void Append(char **buffer,int size,char c) { (*buffer)=(char *)realloc(*buffer,(size+1) * sizeof(char)); (*buffer)[size-1]=c; (*buffer)[size]='\0'; } after the array is created i am running the function that raise the error several times and its work fine and i increase and decrease its content using the following function : void replaceStr(char **replaceIn,char *replaceWith,int fromIndx,int toIndx) { char *ptr=(*replaceIn)+fromIndx; int replaceWithLen=(replaceWith==NULL)?0:strlen(replaceWith); int replacedStrLen=toIndx-fromIndx; int originalStrLen=strlen(*replaceIn); //int str3Len=strlen(str3); int shift=replaceWithLen-replacedStrLen; int bytesToMove=strlen(ptr)+1; int originalLength; printf("%s\n",*replaceIn); if(replaceWithLen==0) { memmove(ptr-1,ptr+replacedStrLen,bytesToMove); } else { if(replaceWithLen>replacedStrLen) { originalLength=strlen(*replaceIn); (*replaceIn)=(char *)realloc(*replaceIn,(originalLength+shift+1) * sizeof(char)); if(*replaceIn==NULL) printf("Error while allocating memory"); *((*replaceIn)+originalStrLen)='\0'; ptr=(*replaceIn)+fromIndx; memmove(ptr+replaceWithLen,ptr+replacedStrLen,bytesToMove); } else memmove(ptr+replaceWithLen,ptr+replacedStrLen,bytesToMove); } memcpy(ptr,replaceWith,replaceWithLen); } the error happen at some point in the line in bold !

                1 Reply Last reply
                0
                • A Ayman Mashal

                  the array is created by adding characters using the following function void Append(char **buffer,int size,char c) { (*buffer)=(char *)realloc(*buffer,(size+1) * sizeof(char)); (*buffer)[size-1]=c; (*buffer)[size]='\0'; } after the array is created i am running the function that raise the error several times and its work fine and i increase and decrease its content using the following function : void replaceStr(char **replaceIn,char *replaceWith,int fromIndx,int toIndx) { char *ptr=(*replaceIn)+fromIndx; int replaceWithLen=(replaceWith==NULL)?0:strlen(replaceWith); int replacedStrLen=toIndx-fromIndx; int originalStrLen=strlen(*replaceIn); //int str3Len=strlen(str3); int shift=replaceWithLen-replacedStrLen; int bytesToMove=strlen(ptr)+1; int originalLength; printf("%s\n",*replaceIn); if(replaceWithLen==0) { memmove(ptr-1,ptr+replacedStrLen,bytesToMove); } else { if(replaceWithLen>replacedStrLen) { originalLength=strlen(*replaceIn); **(*replaceIn)=(char *)realloc(*replaceIn,(originalLength+shift+1) * sizeof(char));** if(*replaceIn==NULL) printf("Error while allocating memory"); *((*replaceIn)+originalStrLen)='\0'; ptr=(*replaceIn)+fromIndx; memmove(ptr+replaceWithLen,ptr+replacedStrLen,bytesToMove); } else memmove(ptr+replaceWithLen,ptr+replacedStrLen,bytesToMove); } memcpy(ptr,replaceWith,replaceWithLen); } the error happen at some point in the line in bold ! -- modified at 4:21 Sunday 4th March, 2007

                  B Offline
                  B Offline
                  blue_rabbit
                  wrote on last edited by
                  #8

                  you have to give me an example that raises error. I try your code with my example:

                  int main(){	
                  	char *lpstr1 = 0,*lpstr2 = 0;
                  	lpstr1 = (char *)malloc(10);
                  	lpstr2 = (char *)malloc(20);
                  	strcpy(lpstr2,"damnit");
                  	strcpy(lpstr1,"kha");	
                  	replaceStr(&lpstr1,lpstr2,2,10);
                  	cout<<"str1 "<
                  

                  there is no problem. I think your code is hard to read. I do not understand, what do you want when replaceWithLen = 0, i think you dont have to do any think. The logic is complex. I think you need a new version. I write this code, I hope it matchs your will. Please try it. void myreplaceStr(char **replaceIn,char *replaceWith,int fromIndx,int toIndx){ if((replaceWith==0)||(replaceIn==0)){ printf("%s","nothing to do here\n"); return; } // if(*replaceIn == 0){ printf("%s","nothing will modified here\n"); return; } // if(fromIndx > strlen(*replaceIn)){ printf("%s","index outside of string, commonly we dont want this happen\n"); } //if we donot have enough memory in *replaceIn DWORD minSize = (strlen(replaceWith) > toIndx - fromIndx) ? (toIndx-fromIndx+1): strlen(replaceWith); if(strlen(replaceWith+fromIndx) < minSize){ *replaceIn = (char *)realloc(*replaceIn,strlen(*replaceIn + fromIndx) + minSize); } //now we have enough memory in *replace to contain replaceWith we just copy it memcpy(*replaceIn + fromIndx,replaceWith,strlen(replaceWith)); }; -- modified at 6:21 Sunday 4th March, 2007

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups