By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

new/delete

P: n/a
Hi,
I am using pointer in my program. I write them like:
*DIST=new int [M+1];
and when I want to make the memory free, I write:
delete DIST;
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

Jun 30 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
c language schrieb:
Hi,
I am using pointer in my program. I write them like:
*DIST=new int [M+1];
and when I want to make the memory free, I write:
delete DIST;
For every new a delete, for every new[] a delete[].
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.


This is a C group, not a C++ group. You are offtopic here.

Thomas
Jun 30 '06 #2

P: n/a
c language wrote:
Hi,
I am using pointer in my program. I write them like:
*DIST=new int [M+1];
and when I want to make the memory free, I write:
delete DIST;
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


new/delete live in C++ world. In C the correct way is

/* some code */

int *DIST = malloc((M+1)*sizeof *DIST);
if (DIST==NULL)
{
/* error allocating memory, do whatever you wish */
}

/* some code */

free(DIST);
If you still want new/delete, please ask in compl.lang.c++ instead.

--
one's freedom stops where others' begin

Giannis Papadopoulos
Computer and Communications Engineering dept. (CCED)
University of Thessaly
http://dop.freegr.net/
Jun 30 '06 #3

P: n/a

c language wrote:
Hi,
I am using pointer in my program. I write them like:
*DIST=new int [M+1];
and when I want to make the memory free, I write:
delete DIST;
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


First, new and delete are C++ operators, not C. You might want to ask
this in comp.lang.c++.

Second, it would help if you could post a small but *complete* program
that demonstrates the problem, otherwise you aren't going to get a lot
of help. From what you've posted here, I *think* your problem is that
you're dereferencing DIST when you shouldn't be; replace your
allocation above with

DIST = new int [M+1]; // note that I've removed the * operator

but you haven't given enough information for me to be sure.

Jun 30 '06 #4

P: n/a
c language said:
Hi,
I am using pointer in my program. I write them like:
*DIST=new int [M+1];
In C, that's a syntax error.
and when I want to make the memory free, I write:
delete DIST;


In comp.lang.c++ they will explain to you about the difference between new
and new [], and how to destroy objects created with either operator. Or
more likely, they'll tell you to read your C++ book more carefully, or get
a better book.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.