......
char *tojumble = 0;
char *jumbling = 0;
int *flag = 0;
char *jumbled = 0;
......
......
class Word
{
public:
std::string word;
std::string jumbledword;
int Word::num_letters()
{
return (word.size());
}
int Word::jumble()
{
int letters = Word::num_letters();
jumbling = new char[letters];
flag = new int[letters];
jumbled = new char[letters];
tojumble = new char[letters];
do
{
for (int t=0; t
now if my understanding is correct right before the return 0; i am deleting the arrays i created at the top of my code. My problem is i have a runtime error when i have those 4 delete commands in there. When i comment them out the program works fine. i do have to delete them because i used the new command with the 4 of them but where am i going wrong?
Any help would be greatly appreciatted!
werdy666
[edited by - werdy666 on May 13, 2002 9:54:30 AM]
newbie: deleting dynamic arrays??
hi,
I'm having afew problems and hope someone can point me in the right direction. First of all some code.....
i would say the problem is the placement of the brackets in the delete commands. They should be directly after the delete.
Try out
delete[] jumbling;
instead of
delete []jumbling;
Hope that helps.
blw
Try out
delete[] jumbling;
instead of
delete []jumbling;
Hope that helps.
blw
640 kByte of memory is all that any application should ever need(Bill Gates 1981)
Using free() on new''ed memory is a no-no. Same for delete on malloc''ed memory.
I also doubt the placement of [] matters, since the compiler seldom cares about white spaces.
I also doubt the placement of [] matters, since the compiler seldom cares about white spaces.
yes brother bob. I tried both ideas but i still get a runtime error.
What exactly happens if this piece of code runs 100 times then shuts down. It use 100 times the memory that it should and then gives it back? or does it not give it back at all until i restart windows??
What exactly happens if this piece of code runs 100 times then shuts down. It use 100 times the memory that it should and then gives it back? or does it not give it back at all until i restart windows??
You should test the newly "new"ed (alloced) memory before using it.. just to be sure that it was allocated.
you may also want to do that before you call delete.
you may also want to do that before you call delete.
I'm not the brightest something or other in a group or similar.~ me ~
Another idea:
What is the purpose of the line
jumbling = NULL; flag = NULL; ...
do you need this line? Maybe this causes the problem because you try to access the variables after the delete.
blw
What is the purpose of the line
jumbling = NULL; flag = NULL; ...
do you need this line? Maybe this causes the problem because you try to access the variables after the delete.
blw
640 kByte of memory is all that any application should ever need(Bill Gates 1981)
Doing that is good.. you could also just say
jumble = 0; // and for all others..
If you call delete on it now, it wont hurt (as much).
I can''t remember which, dangling or stray pointer, hrm.. but that is what you''ll have (I think) if you don''t do this, and it will save you (no crashy).
jumble = 0; // and for all others..
If you call delete on it now, it wont hurt (as much).
I can''t remember which, dangling or stray pointer, hrm.. but that is what you''ll have (I think) if you don''t do this, and it will save you (no crashy).
I'm not the brightest something or other in a group or similar.~ me ~
This will not help you, but anyway:
- (jumbled == tojumble) is always false.
- Move the srand out of the loop, or move it out of the method entirely.
- The goto could be replaced by a do-while loop.
Another thing: Why do you have the tojumble, jumbling, flag and jumbled variables as globals when you always make new/delete on them on each call to jumble()?
[edited by - dalleboy on May 13, 2002 11:20:06 AM]
- (jumbled == tojumble) is always false.
- Move the srand out of the loop, or move it out of the method entirely.
- The goto could be replaced by a do-while loop.
int newlocation;do{ newlocation = rand() % letters;} while (flag[newlocation] == 1);flag[newlocation] = 1;jumbled[newlocation] = tojumble[j];
Another thing: Why do you have the tojumble, jumbling, flag and jumbled variables as globals when you always make new/delete on them on each call to jumble()?
[edited by - dalleboy on May 13, 2002 11:20:06 AM]
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
quote:
Original post by werdy666
What exactly happens if this piece of code runs 100 times then shuts down. It use 100 times the memory that it should and then gives it back? or does it not give it back at all until i restart windows??
It will get a new buffer on each call to the method, and at the end of the method the buffer will be released when you delete it. If the program would crash or exit abruptly, Windows and any other OS would release all the memory allocated.
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement