Why do I get always 4 for strlen(buf) ?
Code:int main() { int i = 1; char * buf ; while(i<11) { buf = new char[i++]; strcat(buf, "a"); cout << strlen(buf) <<"\n"; } delete [] buf; return 0; }
Why do I get always 4 for strlen(buf) ?
Code:int main() { int i = 1; char * buf ; while(i<11) { buf = new char[i++]; strcat(buf, "a"); cout << strlen(buf) <<"\n"; } delete [] buf; return 0; }
Using Windows 10 with Code Blocks and MingW.
Try strcpy instead of strcat.
The memory you get is uninitialised, and the first thing strcat does is go looking for a \0.
Your memory is filled with junk, so this is a crap-shoot as to what really happens.
Also, you're leaking all the memory except for the last iteration.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Thanks, so I have to delete it every time before I want to reallocate it?
Ps. I support UKIP also. ;-)
Using Windows 10 with Code Blocks and MingW.
You're not reallocating anything at the moment.
You need to start with something like
char *s = new char[1];
*s = '\0';
Then each loop iteration is
char *t = new char[i+1];
strcpy(t,s);
strcat(t,"a");
delete [ ] s;
s = t;
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Remember the rule: every new needs a corresponding delete. There is no many-to-one relationship here. Everytime you new, you must delete. If I see two new and one delete, then there's a bug. If there is one new and two delete, then there is a bug.
Thanks Elysia, that's what I wanted to know.
So in Salem's example we should "delete [] t" also at the end of every loop?
Using Windows 10 with Code Blocks and MingW.
> So in Salem's example we should "delete [] t" also at the end of every loop?
No you wouldn't.
Follow the memory, not the variable names.Code:new for ( .... ) { new .... delete } delete
If you delete t inside the loop, then you lose everything.
s = t; is the stepping stone from one block of memory to the next. If you remove this one from under your feet, you're just going to get wet.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
You should be aware that nobody does this sort of thing in the real world. To continually allocate something that is just one byte bigger and copy the existing contents across each time is madness. Have a read of this: Schlemiel the Painter's algorithm - Wikipedia, the free encyclopedia
In the real world, you either use a nice container that grows geometrically, or you perform one pass up front to work out the final size required, and then only allocate once.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Interesting enough going from C to C++ would actually solve the issue by using std::string, though fundamentally I agree with the argument that coming from a high level language can be harder to understand low level optimization.
It always struck me as odd the null terminating string concept in C. I think the LV (length- value) notion would just have been better. So first bytes have the total length of the string and then you (the parser) knows how much you need to read. Same memory footprint for strings less than 255 bytes as well. A struct is even better of course.
Ok, but that was just an example to understand what's going on.
In my real program I have a constantly changing size that I need to allocate.
In this case should I delete it in every loop or only when the loop has finished?
Code:// global char * buf; int main() { int newsize = 0; while(i<11) { GetNewsize(newsize); buf = new char[newsize]; function(buf); delete [] buf; } return 0; }
Using Windows 10 with Code Blocks and MingW.
Yes, that should be fine - well, no memory leaks anyway.
Though later on, you might want to introduce a 'maxsize' as well, so you can re-use the buffer if newsize <= maxsize
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Well then what else should I use? I don't know what size to allocate in advance.
Using Windows 10 with Code Blocks and MingW.