new & delete

J

jafarikia

Hi,
I am using pointer in my program. I write them like:

*DIST=new int [M];

and when I want to make the memory free, I write:

delete DIST; just before the "return 0".

I don't know if the way that I am doing is correct or not because I
have some segmentation fault (core dumped) error.
Any suggestions?

Thanks,
Mohsen
 
M

mlimber

Hi,
I am using pointer in my program. I write them like:

*DIST=new int [M];

and when I want to make the memory free, I write:

delete DIST; just before the "return 0".

I don't know if the way that I am doing is correct or not because I
have some segmentation fault (core dumped) error.
Any suggestions?

We can't know for sure since we don't know the type of DIST (show us
more code, as per
http://parashift.com/c++-faq-lite/how-to-post.html#faq-5.8), but it
looks like you're allocating with new[], but deleting with delete
rather than delete[], which is bad. See this FAQ:

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.12

BTW, standard practice is to reserve all caps for macros only (but
don't use macros if you don't have to:
http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.5).

Cheers! --M
 
S

Scott McPhillips [MVP]

Hi,
I am using pointer in my program. I write them like:

*DIST=new int [M];

and when I want to make the memory free, I write:

delete DIST; just before the "return 0".

I don't know if the way that I am doing is correct or not because I
have some segmentation fault (core dumped) error.
Any suggestions?

Thanks,
Mohsen

What is the type of DIST?
Why did you allocate to *DIST but not delete *DIST?
Why did you allocate an array ([]) but not delete an array (delete []).
 
D

Daniel T.

Hi,
I am using pointer in my program. I write them like:

*DIST=new int [M];

and when I want to make the memory free, I write:

delete DIST; just before the "return 0".

That should be:

delete [] DIST;
 
H

Howard

Daniel T. said:
Hi,
I am using pointer in my program. I write them like:

*DIST=new int [M];

and when I want to make the memory free, I write:

delete DIST; just before the "return 0".

That should be:

delete [] DIST;

Actually, if the line where he uses new is correct:
*DIST=new int [M];
then the correct way to delete would be:
delete [] *DIST;

(But I'd be willing to bet - at least a little - that the assignment was
wrong, too.)

-Howard
 
B

benben

Why did you allocate to *DIST but not delete *DIST?

Well, it seemed that the OP's code did get compiled so it is safe to
assume he/she did not allocate to *DIST (but DIST.)

Just my guessing.

Ben
 
R

Rolf Magnus

benben said:
Well, it seemed that the OP's code did get compiled

It seems the OP lied to us by not showing the code he tried, but rather
something else that contains some additional/different errors.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,181
Messages
2,570,971
Members
47,537
Latest member
BellCorone

Latest Threads

Top