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; }
This is a discussion on oparator new within the C++ Programming forums, part of the General Programming Boards category; Why do I get always 4 for strlen(buf) ? Code: int main() { int i = 1; char * buf ...
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; }
Compiler MSVC++ 2010 with Code::Blocks.
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.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
Thanks, so I have to delete it every time before I want to reallocate it?
Ps. I support UKIP also. ;-)
Compiler MSVC++ 2010 with Code::Blocks.
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.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
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.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
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?
Compiler MSVC++ 2010 with Code::Blocks.
Yes, if you put that code in a loop.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
> 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.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
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"
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; }
Compiler MSVC++ 2010 with Code::Blocks.
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.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
Why do you need to use new in the first place?
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
Well then what else should I use? I don't know what size to allocate in advance.
Compiler MSVC++ 2010 with Code::Blocks.
Use a std::string, std::vector<char>, etc.Originally Posted by Ducky
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^