|
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)
{
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?
|
|
|
|
|
"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??
|
|
|
|
|
No, it only releases memory allocated on the heap. To release memory on the stack you have to use assembly langauge (in which case things get complicated) or let it go out of scope (like you should).
|
|
|
|
|
void StrDelete1(char ch[], int pos,int 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]);
}
First 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));
return realloc(ch,strlen(ch));
}
modified on Wednesday, April 7, 2010 8:27 PM
|
|
|
|
|
real fine solution
i wouldnt realloc for performance reasons
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
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.
|
|
|
|
|
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 it looks like a 'hot fix'.
And therefor makes an additional comment for that performance risk sense
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
What would you like to see ?
A possible task could be:
1) Given: "abcdef"
2) Cut: 2 characters from the position 3
3) Result: "abcf"
virtual void BeHappy() = 0;
|
|
|
|
|
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
|
|
|
|
|
Oh,I don not do this,sorry! I am freshman and does not use the debugger to step through the coder
|
|
|
|
|
hello, i need help on this: the error i get is: error C2440: '=' : cannot convert from 'double' to 'class _variant_t *' on this line:
Filtered_Average[ik] = filtered_sum/95;
the portion of the code is:
vector<_variant_t*> Filtered_Average(9990);<br />
for(k = 0; k < Filtered_Average.size(); ++k)<br />
{<br />
Filtered_Average[k] = new _variant_t();<br />
} ......
<br />
for (int ik = 1; ik<=N; ik++)<br />
{<br />
double filtered_sum = 0.0;<br />
<br />
for (int jk= 1; jk<=N2; jk++) <br />
{<br />
filtered_sum = filtered_sum + (double)results2[ik][jk];<br />
}<br />
<br />
Filtered_Average[ik] = filtered_sum/95;<br />
}
|
|
|
|
|
_variant_t& operator=(double dblSrc);
is valid, so you can convert from double to _variant_t. So, your problem is your trying to make a pointer equal a double.
Maybe:
*(Filtered_Average[ik]) = filtered_sum/95;
?
I'd also question the 95... if you know N2 = 95, then you don't need N2. If you don't know, then you can't use 95...
Iain.
I have now moved to Sweden for love (awwww).
|
|
|
|
|
Try it :
*(Filtered_Average[ik]) = filtered_sum/95;
virtual void BeHappy() = 0;
|
|
|
|
|
I would like to use a shortcut to point to a file in some other directory. In windows C++ how can I get at the target the shortcut is pointing to in order to then open that specific file?
|
|
|
|
|
This[^] article might help.
|
|
|
|
|
I am not sure where to post this , so it is here.
I am a lazy programmer and have a hard time keeping track of #include(s) nesting in my messy program.
I started to use "forward declaration" to mask #include files dependencies.
It works most of the time.
But is it a good practice?
Would it be going too far if I just forward declare all my classes in stdafx.h? Is portability main issue here or what is the issue?
Thanks for reading.
Vaclav
|
|
|
|
|
i don't like forward declarations, but i'm not exactly sure why. i always feel weird when i have to make one.
but, sometimes they're just plain necessary.
|
|
|
|
|
Vaclav_Sal wrote: I started to use "forward declaration" to mask #include files dependencies.
Like you said, if you want to minimise link dependecies it's a great tool. Change one header file and you will not have to recompile a rats tail of other object files, just because the class is used in protected/private methods of other classes. Generally I include only header files absolutely necessary to compile in header files... and put the rest into the individual source files. With bigger projects it can be a time saver.
Vaclav_Sal wrote: Would it be going too far if I just forward declare all my classes in stdafx.h
I would personally use stdafx.h only to what it was intended for, speed up compilation time. For example have library and rarely changing header files in it. However I keep dependencies working without precompiled headers, in case I rip out a file/module and reuse it in another project. That's just my personal design taste, hope it helped.
/M
Webchat in Europe Now with 26% more Twitter
modified on Wednesday, April 7, 2010 1:54 PM
|
|
|
|
|
Quit editing quick answers and not changing anything...who wants to see a question from March brought back up to the top of the list just so you can get some editing points?!?
|
|
|
|
|
William, I was retagging the questions for better findability.
In my opinion such changes should not bring back quick answers to the top of the "last changes" list and also give no editing points (actually they don't). You can report it here[^].
Cheers
|
|
|
|
|
There is nothing wrong with forward declarations. In fact, some very well known and useful C++ idioms depend on that technique: Pimpl (opaque pointer)[^], for instance.
|
|
|
|
|
There was a good word said by Chris: "sometimes",
that is not associated for me with "stdafx.h"...
virtual void BeHappy() = 0;
|
|
|
|