Advertisement

new hangs me program

Started by May 27, 2000 05:55 AM
16 comments, last by zyzzy 24 years, 7 months ago
Currently these lines of code hangs me program char *pnt; pnt = new char[10000]; <--- Here it hangs! delete pnt; If I change the value 10000 to something smaller it''s ok. The only thing I can think of is that my computer is running out of memory but shouldn''t new return an error value instead of hanging the program. It worked just a few minutes ago. I have tried to reboot my computer. In other places I am allocating memory too and it works just fine, but here.... Why does it feel that this error is out of my control?
I'm no expert at C++, and know almost nothing about new/delete, but it appears as if you are not deleting all of the memory. Try this:

char *pnt;
pnt = new char[10000];
delete[] pnt;

If that doesn't work, try this:

char *pnt;
pnt = new char[10000];
delete pnt[];

Not sure which one will work, but one of them works

Since just using delete pnt; won't delete all memory, you'll run out of memory after starting the program a couple of times.

Correct me if I'm wrong.

/. Muzzafarath
Visit my website, Mad House Software

Edited by - Muzzafarath on May 27, 2000 8:11:15 AM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Advertisement
Muzzafarath >> Your are right about that it doesn''t delete all of the memory. The correct way is the first you wrote.

char *pnt;
pnt = new char[10000];
delete[] pnt;

Is correct.
When deleting, try this instead:

char *pnt = NULL;
pnt = new char[10000];
if(pnt) delete pnt;

I suppose this would check if any memory was actually allocated which might be the problem. As I said before, don't know much about C++, and what I just said may be terribly wrong

/. Muzzafarath
Visit my website, Mad House Software

Cogito ergo doleo - I think, therefore I am depressed.

Edited by - Muzzafarath on May 27, 2000 12:20:35 PM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Small note on delete: you don't first have to check if the pointer is null before calling delete. The C++ language guarantees that operator delete() isn't called by a delete expression when the pointer operand is set to 0. So, if you consequently set a pointer to 0 after deleting the memory it points to, you don't have to check if the pointer is 0 before deleting.

Now, on to the question: new generally throws an exception when it can't allocate the memory. So embed the new expression in a try/catch block (see example) and see if an expression is thrown. Allocating 10000 chars shouldn't be a problem, but maybe you're short on memory? That could be possible, because in the code you display, delete pnt only deletes the first element of the 10000. So 9999 bytes of memory are being leaked... When using new type[..], make sure you use delete[].

Example code:

#include <exception>#include <iostream>char *pnt = 0;try{    pnt = new char[10000];}catch (std::exception& e){    std::cerr << e.what() << std::endl;}delete[] pnt;  


Erik

Edited by - Erik Post on May 27, 2000 12:38:58 PM
Erik Post, are you the same guy who wrote the DirExtractor utility for Commandos? Your name is the same as the guy who made it and you come from the same country. You even have the same e-mail address so I suppose you are the same person Am I correct?

/. Muzzafarath
Visit my website, Mad House Software

Te audire no possum. Musa sapientum fixa est in aure - I can't hear what you're saying. I have a banana in my ear.

Edited by - Muzzafarath on May 27, 2000 2:17:38 PM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Advertisement
Sorry I wrote wrong. It was supposed to be delete [] ptr.
Note that it does not reach the delete stuff.
I don''t think I am short of memory because it does not work on two different computers. And as I said it worked fine a while ago. I''ll try that try-catch block.
Muzzafarath: yup, that''s me

Never imagined that DIRExtractor''d be known here

Erik
>> Never imagined that DIRExtractor'd be known here <<

I used to run a Commandos site (The Commando Centre), that's why I know about DIRExtractor. Those were they days

/. Muzzafarath
Visit my website, Mad House Software

Si minor plus est ergo nihil sunt omnia - If less is more, then nothing should be everything.

Edited by - Muzzafarath on May 27, 2000 3:58:30 PM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
C/C++ does not initialize pointers or variables like
VB. It is better if you wrote

char* ptr = NULL;

Other wise it may not be pointing to NULL. This is
according to the C++ standard, and may be different
for some compilers. But because it will not always
be initialized, it is better to say it in code, than
to be wrong some times.

This topic is closed to new replies.

Advertisement