new/delete

C

c language

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
 
T

Thomas J. Gritzan

c said:
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
 
G

Giannis Papadopoulos

c said:
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/
 
J

John Bode

c said:
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.
 
R

Richard Heathfield

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.
 

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

No members online now.

Forum statistics

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

Latest Threads

Top