from the index pos in ch[],delete the the string of len
-
I try to do this but the first function is error ,I don not know why ,could someone help me ,thank in advantage!!
void StrDelete1(char ch[], int pos,int len)
/*from the index pos in ch[],delete the the string of len*/
{
if (pos<0 || pos >(MAX-len))
{
printf("the string delete error!");
exit(1);
}
int i;
for (i=0; i<MAX; i++)
printf("%c", ch[i]);char \*temp = (char\*)malloc(sizeof(char)); if (temp == NULL) { exit(1); } for (i=pos; i<MAX-len; i++) { temp = &ch\[i\]; free(temp); } printf("\\n"); for(i=0; i<MAX; i++) printf("%c", ch\[i\]);
}
void StrDelete2(char ch[], int pos,int len)
{
if (pos<0 || pos >(MAX-len))
{
printf("the string delete error!");
exit(1);
}
int i;
printf("\n");
for (i=0; i<MAX; i++)
printf("%c", ch[i]);char \*temp = (char\*)malloc(MAX-len); if (temp == NULL) { exit(1); } for (i=0; i<pos; i++) temp\[i\] = ch\[i\]; for (i=pos; i<MAX-len; i++) { temp\[i\]=ch\[i+len\]; } free(ch); printf("\\n"); for (i=0; i<MAX-len; i++) printf("%c", temp\[i\]);
}
-
I try to do this but the first function is error ,I don not know why ,could someone help me ,thank in advantage!!
void StrDelete1(char ch[], int pos,int len)
/*from the index pos in ch[],delete the the string of len*/
{
if (pos<0 || pos >(MAX-len))
{
printf("the string delete error!");
exit(1);
}
int i;
for (i=0; i<MAX; i++)
printf("%c", ch[i]);char \*temp = (char\*)malloc(sizeof(char)); if (temp == NULL) { exit(1); } for (i=pos; i<MAX-len; i++) { temp = &ch\[i\]; free(temp); } printf("\\n"); for(i=0; i<MAX; i++) printf("%c", ch\[i\]);
}
void StrDelete2(char ch[], int pos,int len)
{
if (pos<0 || pos >(MAX-len))
{
printf("the string delete error!");
exit(1);
}
int i;
printf("\n");
for (i=0; i<MAX; i++)
printf("%c", ch[i]);char \*temp = (char\*)malloc(MAX-len); if (temp == NULL) { exit(1); } for (i=0; i<pos; i++) temp\[i\] = ch\[i\]; for (i=pos; i<MAX-len; i++) { temp\[i\]=ch\[i+len\]; } free(ch); printf("\\n"); for (i=0; i<MAX-len; i++) printf("%c", temp\[i\]);
}
Here, you are freeing memory that the function didn't allocate:
for (i=pos; i<MAX-len; i++)
{
temp = &ch[i];
free(temp);
}Why are you calling
free
there? -
I try to do this but the first function is error ,I don not know why ,could someone help me ,thank in advantage!!
void StrDelete1(char ch[], int pos,int len)
/*from the index pos in ch[],delete the the string of len*/
{
if (pos<0 || pos >(MAX-len))
{
printf("the string delete error!");
exit(1);
}
int i;
for (i=0; i<MAX; i++)
printf("%c", ch[i]);char \*temp = (char\*)malloc(sizeof(char)); if (temp == NULL) { exit(1); } for (i=pos; i<MAX-len; i++) { temp = &ch\[i\]; free(temp); } printf("\\n"); for(i=0; i<MAX; i++) printf("%c", ch\[i\]);
}
void StrDelete2(char ch[], int pos,int len)
{
if (pos<0 || pos >(MAX-len))
{
printf("the string delete error!");
exit(1);
}
int i;
printf("\n");
for (i=0; i<MAX; i++)
printf("%c", ch[i]);char \*temp = (char\*)malloc(MAX-len); if (temp == NULL) { exit(1); } for (i=0; i<pos; i++) temp\[i\] = ch\[i\]; for (i=pos; i<MAX-len; i++) { temp\[i\]=ch\[i+len\]; } free(ch); printf("\\n"); for (i=0; i<MAX-len; i++) printf("%c", temp\[i\]);
}
void StrDelete1(char ch[], int pos,int len)
/*from the index pos in ch[],delete the the string of len*/
{
if (pos<0 || pos >(MAX-len)) {
printf("the string delete error!");
exit(1);
}
int i;
for (i=0; iFirst off, you should remove "char *temp = (char*)malloc(sizeof(char));". The allocated memory is never used, and it is never released.
To my knowledge, free() releases a block of memory, not just that at the address pointed to. Therefore what you are trying to do may not be possible. I suggest you either call free() on the array (if it is allocated with malloc()) and release it, or set the element at pos to '\0'.
Also, you are accessing memory you are trying to free at the end of the function. Do not do that.
EDIT:
Okay, I think I have figured out away to accomplish you goal. Here is a sample to get you started in the right direction (you may want to revise it):
char * DeleteString1(char * ch,int pos,int len) {
memmove(ch+pos,ch+pos+len,strlen(ch)-(pos+len));//Move content past pos+len to pos.
return realloc(ch,strlen(ch));//Return a downsized memory block - should be the same as ch, but it is good to be sure.
}modified on Wednesday, April 7, 2010 8:27 PM
-
I try to do this but the first function is error ,I don not know why ,could someone help me ,thank in advantage!!
void StrDelete1(char ch[], int pos,int len)
/*from the index pos in ch[],delete the the string of len*/
{
if (pos<0 || pos >(MAX-len))
{
printf("the string delete error!");
exit(1);
}
int i;
for (i=0; i<MAX; i++)
printf("%c", ch[i]);char \*temp = (char\*)malloc(sizeof(char)); if (temp == NULL) { exit(1); } for (i=pos; i<MAX-len; i++) { temp = &ch\[i\]; free(temp); } printf("\\n"); for(i=0; i<MAX; i++) printf("%c", ch\[i\]);
}
void StrDelete2(char ch[], int pos,int len)
{
if (pos<0 || pos >(MAX-len))
{
printf("the string delete error!");
exit(1);
}
int i;
printf("\n");
for (i=0; i<MAX; i++)
printf("%c", ch[i]);char \*temp = (char\*)malloc(MAX-len); if (temp == NULL) { exit(1); } for (i=0; i<pos; i++) temp\[i\] = ch\[i\]; for (i=pos; i<MAX-len; i++) { temp\[i\]=ch\[i+len\]; } free(ch); printf("\\n"); for (i=0; i<MAX-len; i++) printf("%c", temp\[i\]);
}
What would you like to see ? :) A possible task could be: 1) Given:
"abcdef"
2) Cut:2
characters from the position3
3) Result:"abcf"
virtual void BeHappy() = 0;
-
Here, you are freeing memory that the function didn't allocate:
for (i=pos; i<MAX-len; i++)
{
temp = &ch[i];
free(temp);
}Why are you calling
free
there? -
void StrDelete1(char ch[], int pos,int len)
/*from the index pos in ch[],delete the the string of len*/
{
if (pos<0 || pos >(MAX-len)) {
printf("the string delete error!");
exit(1);
}
int i;
for (i=0; iFirst off, you should remove "char *temp = (char*)malloc(sizeof(char));". The allocated memory is never used, and it is never released.
To my knowledge, free() releases a block of memory, not just that at the address pointed to. Therefore what you are trying to do may not be possible. I suggest you either call free() on the array (if it is allocated with malloc()) and release it, or set the element at pos to '\0'.
Also, you are accessing memory you are trying to free at the end of the function. Do not do that.
EDIT:
Okay, I think I have figured out away to accomplish you goal. Here is a sample to get you started in the right direction (you may want to revise it):
char * DeleteString1(char * ch,int pos,int len) {
memmove(ch+pos,ch+pos+len,strlen(ch)-(pos+len));//Move content past pos+len to pos.
return realloc(ch,strlen(ch));//Return a downsized memory block - should be the same as ch, but it is good to be sure.
}modified on Wednesday, April 7, 2010 8:27 PM
-
real fine solution :thumbsup: i wouldnt realloc for performance reasons :rolleyes:
Press F1 for help or google it. Greetings from Germany
-
"Why are you calling free there?" Yeah ,I only want to delete the number in the arry,Is it delete the memory in the stack??
-
I try to do this but the first function is error ,I don not know why ,could someone help me ,thank in advantage!!
void StrDelete1(char ch[], int pos,int len)
/*from the index pos in ch[],delete the the string of len*/
{
if (pos<0 || pos >(MAX-len))
{
printf("the string delete error!");
exit(1);
}
int i;
for (i=0; i<MAX; i++)
printf("%c", ch[i]);char \*temp = (char\*)malloc(sizeof(char)); if (temp == NULL) { exit(1); } for (i=pos; i<MAX-len; i++) { temp = &ch\[i\]; free(temp); } printf("\\n"); for(i=0; i<MAX; i++) printf("%c", ch\[i\]);
}
void StrDelete2(char ch[], int pos,int len)
{
if (pos<0 || pos >(MAX-len))
{
printf("the string delete error!");
exit(1);
}
int i;
printf("\n");
for (i=0; i<MAX; i++)
printf("%c", ch[i]);char \*temp = (char\*)malloc(MAX-len); if (temp == NULL) { exit(1); } for (i=0; i<pos; i++) temp\[i\] = ch\[i\]; for (i=pos; i<MAX-len; i++) { temp\[i\]=ch\[i+len\]; } free(ch); printf("\\n"); for (i=0; i<MAX-len; i++) printf("%c", temp\[i\]);
}
wbgxx wrote:
...but the first function is error...
Which means what exactly?
wbgxx wrote:
...I don not know why...
Have you used the debugger to step through the code to find out why?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
Given the sample code given, I assumed they want to release memory, which realloc would do. However, it is true that doing so is not needed unless memory is in short supply.
-
wbgxx wrote:
...but the first function is error...
Which means what exactly?
wbgxx wrote:
...I don not know why...
Have you used the debugger to step through the code to find out why?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
this is an complicated theme, because the memory is from a bigger block which is in a list. So it can happen that only these function cost some CPU operations but also you get fragmented memory. :~
Press F1 for help or google it. Greetings from Germany
Yes, down sizing the memory is problematic, but that is one more reason my sample should be revised. It was not intended to be fully functional, just showcase what could be done to get the desired results. I spent less than a minute on it so it is likely full of problems; since the enquirer is a student thinking about improve it would be good practice.
-
Yes, down sizing the memory is problematic, but that is one more reason my sample should be revised. It was not intended to be fully functional, just showcase what could be done to get the desired results. I spent less than a minute on it so it is likely full of problems; since the enquirer is a student thinking about improve it would be good practice.