Re: new/delete




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.

.



Relevant Pages

  • Re: new/delete
    ... c language wrote: ... I am using pointer in my program. ... and when I want to make the memory free, ... Computer and Communications Engineering dept. ...
    (comp.lang.c)
  • Re: new/delete
    ... I am using pointer in my program. ... and when I want to make the memory free, ... have some segmentation fault error. ...
    (comp.lang.c)
  • Re: Question on LSP
    ... We are talking about sibling subtypes in an existing tree. ... But that is not known until the Object Reference type is instantiated; yet the semantics of Object Reference must exist in the language meta-model prior to instantiation. ... A pointer points only to an object and messages are sent to objects. ...
    (comp.object)
  • Pointers and Teaching C++ [was Re: A little disappointed]
    ... like Java where *many* facilities such as raw pointer access / manipulation ... are removed in order to create a 'safer' language and stop programmers ... then it is probably wiser to teach programming using ... pointer access / manipulation is not a 'high level language'. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: The Decline of C/C++, the rise of X
    ... > tend to be most useful for encoding Boolean values in data structures ... to freea pointer obtained from the gc, ... >> to be a useful attribute of a language. ... Template arguments are evaluated in the scope of the point of instantiation, ...
    (comp.programming)