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;
}
Printable View
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;
}
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.
Thanks, so I have to delete it every time before I want to reallocate it?
Ps. I support UKIP also. ;-)
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;
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?
Yes, if you put that code in a loop.
> 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.
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.
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;
}
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
Why do you need to use new in the first place?
Well then what else should I use? I don't know what size to allocate in advance.
Use a std::string, std::vector<char>, etc.Quote:
Originally Posted by Ducky