|
-
Aug 19th, 2003, 02:09 PM
#1
Thread Starter
Hyperactive Member
Character array array (array of strings pretty much) won't deallocate
I have an array of strings which is pretty much like this:
PHP Code:
char *Filenames[256];
Filenames = (char (*)[256]) calloc (FileCount, 256);
/* blah blah blah */
free (Filenames) /*causes problem of some sort*/
I've also tried this way:
PHP Code:
char *Filenames[256];
Filenames = new char[FileCount][256];
/* blah blah blah */
delete [] Filenames /*causes EXACT same problem */
Both of which behave the same way when I want to use them, I can still access character 5 of the 5th string by going Filenames[5][5] but I can't delete it, and it's kind of annoying, because I want to resize the array (and clear it at the same time). Any ideas on why I get these problems? It says "User breakpoint called from code at 0x77f97704", but I have no breakpoints or anything (it starts debugging the ASM).
Last edited by Dreamlax; Aug 19th, 2003 at 02:18 PM.
-
Aug 19th, 2003, 04:34 PM
#2
Junior Member
When you declare Filenames in the way you are doing it, the Filenames array itself is created on the stack. In your example, 256 uninitialized char *'s are created. Because those are created on the stack, you don't need to delete the Filenames array. What you might be trying to do is allocate each of the file name strings on the heap with new. To do that look at this code. It also shows how to properly delete that memory.
Code:
int main()
{
const int FileCount = 5;
char *Filenames[FileCount]; // Filenames array created on stack
for (int i = 0; i < FileCount; i++)
Filenames[i] = new char[256]; // space for c-style string allocated on heap
/* blah blah blah */
for (i = 0; i < FileCount; i++)
delete [] Filenames[i]; // delete corresponding to the new above
// No delete [] Filenames necessary here, it was created on the stack.
return 0;
}
This is only an example, it is not necessarily the best way to do it. If FileCount is not fixed at compile time, then you may need to create FileNames with new also. However, in my opinion, if you are programming in C++ (using new/delete) then it is much easier and smarter to do this with the standard string class.
-
Aug 19th, 2003, 04:59 PM
#3
Thread Starter
Hyperactive Member
Thanks, but after revising my code I realised that it's not an array of 256 char pointers, because it's declared:
char (*Filename)[256];
So, that makes it a pointer to an array of 256 chars? I'm not sure hehehe. And yeah it makes more sense to use the string library but I like doing things my way, makes me feel like I have more control over the program, even if it is less stable !
-
Aug 19th, 2003, 05:14 PM
#4
Junior Member
Ahhh, yes. I was thinking some well placed parentheses might help, I just couldn't find the spot. So Filenames is an array of char[256] strings, and you are using something like this:
Code:
#include <memory>
int main()
{
const int FileCount = 5;
char (*Filenames)[256];
Filenames = new char[FileCount][256];
for (int i = 0; i < FileCount; i++)
memset(Filenames[i], 0, 256); // Or use the string in some other nifty way.
// blah blah blah
delete [] Filenames;
return 0;
}
Sounds good to me.
-
Aug 20th, 2003, 01:29 AM
#5
I had this discussion before. Usually it's nice if you want to do it your own way, but for string arrays it's simply not a good idea.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|