Home > Archive > C > June 2006 > new/delete
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| c language 2006-06-30, 6:56 pm |
| 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
| |
| Thomas J. Gritzan 2006-06-30, 6:56 pm |
| 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
| |
| Giannis Papadopoulos 2006-06-30, 6:56 pm |
| 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/
| |
| John Bode 2006-06-30, 6:56 pm |
|
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.
| |
| Richard Heathfield 2006-06-30, 6:56 pm |
| 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)
|
|
|
|
|