Pointers and Delete Operator

D

Denisson

I know that when I allocate memory dinamically I must release it using
the delete operator. But when I don't use the new operator do I must
release the memory pointed by a pointer? For example:


int main(){
int age = 10;
int *agePtr = &age;
addAge(agePtr);
//do something more...
//so if i do not need agePtr at all should I code the following?
delete agePtr;

//do something more...


}

Thanks
 
D

Denisson

I've read this explanations you've done. But I thought that would
exist some tricky behaviour because that code was compiled succesful
in dev cpp. I was expecting an error.
Thanks
 
S

Stefan Ram

Denisson said:
int age = 10;

The age object has automatic storage duration, so it will
exist only until control is leaving its block. No delete is
needed or should be used.

After the end of the duration of the age object, the pointer
&age might still be stored somewhere, but it should not be use
anymore. Therefore, it is a bit risky to pass addresses of
objects with automatic storage duration to other functions or
to return them.

But since this object was declared in the main block of the
main function, it will endure until the end of the program.
(»The function main shall not be used within a program.«)
 
D

Denisson

You said:

Therefore, it is a bit risky to pass addresses of
objects with automatic storage duration to other functions or
to return them.

So, are you saying that is always better to use new allocation when I
pass a pointer to a function or return it? And I should create
pointers without new operator only if the pointer is suposed to be
used in his original scope?

Could someone give a piece of code when pass addresses of objects with
automatic storage duration to functions will cause some kind of
problem?

Thanks
 
S

Stefan Ram

Denisson said:
So, are you saying that is always better to use new allocation
when I pass a pointer to a function or return it?

»new« is used when you want explicit control of storage
duration.
And I should create pointers without new operator only if the
pointer is suposed to be used in his original scope?

An /identifier/ has scope, a pointer does not have scope.
Could someone give a piece of code when pass addresses of objects with
automatic storage duration to functions will cause some kind of problem?

Whether undefined behavior is deemed to be a problem, depends
on personal expectations and decisions which are not the
subject of this newsgroup.

The following programs are not guaranteed to print »22«, but
might still do so.

#include <iostream>
#include <ostream>
int *alpha(){ int beta = 22; return &beta; }
int main(){ int *gamma = alpha(); ::std::cout << *gamma << '\n'; }

#include <iostream>
#include <ostream>
int *zeta;
void epsilon( int * eta ){ zeta = eta; }
void gamma(){ int delta = 22; epsilon( &delta ); }
int main(){ gamma(); ::std::cout << *zeta << '\n'; }

If they should print »22«, this only shows that

»The fact that the program works has no relevance.«

http://www.relisoft.com/book/tech/intro.html
 
J

James Kanze

You said:
So, are you saying that is always better to use new allocation
when I pass a pointer to a function or return it?

I don't think that that was what he was saying; at any rate, it
seems to mix cause and effect: generally, you don't want to use
pointers unless you have to: there's almost never any reason
to have a pointer to an object which is cheap to copy, like int
or double, and even for more expensive objects, most of the
time, unless the object has identity, you should be copying it,
and not dealing with pointers at all.
And I should create pointers without new operator only if the
pointer is suposed to be used in his original scope?

In such cases, you probably shouldn't create a pointer at all.
 
J

James Kanze

On 6 Jul 2008 02:14:15 GMT, (e-mail address removed)-berlin.de (Stefan Ram) wrote
in comp.lang.c++:
Just a question. Is there some reason that you can't use plain old
ordinary ASCII quotation marks like just about everyone else in this
group? Their universally understood meaning is to delimit quoted
material, after all.

It's not that universal: in French, you use « ... », and in
German, »...« is common. (Of course, since this is an English
language group, English conventions are to be preferred.)
Your affectation of using non-standard characters as
delimiters can make things difficult on some newsreaders, and
on the character sets used by some people.
Too much to ask that you use "and" instead of »and«?

I've often been tempted to use the French conventions when
quoting code... if the code already contains a "...". The
convention is obvious enough that it should be understandable,
and avoids the ambiguity of which " are part of the code, and
which are being used for quoting in the English text.
(Similarly, you run much less chance of an identifier clashing
with a keyword if the indentifiers are in French or German:).)
Until now, I've not done it, preferring ``...'', even though
that looks ugly on my screen, with my current font.

I'm not sure what the problem is with newsreaders: the standard
"citation" conventions of Usenet don't use quotes. The major
argument against it is, of course, the fact that there are
several widely used character encodings, and it's not the same
in them. But if the poster inserts the proper headers
specifying the encoding, it *should* work. (Whether it does or
not, is another question, and it's certain that sticking to
plain ASCII is a lot surer, at least in English language
groups.)
 
D

Denisson

#include <iostream>
#include <ostream>
int *zeta;
void epsilon( int * eta ){ zeta = eta; }
void gamma(){ int delta = 22; epsilon( &delta ); }
int main(){ gamma(); ::std::cout << *zeta << '\n'; }

I think I understood now. In this piece of code zeta holds the address
of the delta variable that goes out of scope in the end of gamma
function. So when you call cout maybe the '22' will still there, or
this memory could be rewritten by another program.

Thanks
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top