realloc function problem
-
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 -
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 ? thanksI 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);
-
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 ? thanksCan 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[][^]
-
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);
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 ?
-
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 ?
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
-
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
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 -
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[][^]
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 ! -
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, 2007you 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