Google Groups Home
Help | Sign in
Pointers and Delete Operator
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Denisson  
View profile
 More options Jul 5, 9:37 pm
Newsgroups: comp.lang.c++
From: Denisson <denisso...@gmail.com>
Date: Sat, 5 Jul 2008 18:37:30 -0700 (PDT)
Local: Sat, Jul 5 2008 9:37 pm
Subject: Pointers and Delete Operator
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

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel T.  
View profile
 More options Jul 5, 9:47 pm
Newsgroups: comp.lang.c++
From: "Daniel T." <danie...@earthlink.net>
Date: Sat, 05 Jul 2008 21:47:43 -0400
Local: Sat, Jul 5 2008 9:47 pm
Subject: Re: Pointers and Delete Operator

Denisson <denisso...@gmail.com> wrote:
> 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?

No. Only memory that was allocated with 'new' should be released with
'delete', only memory that was allocated with 'new[]' should be relased
with 'delete[]', only memory that was allocated with 'malloc', 'calloc'
or 'realloc' should be released with 'free'

 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;

No, the memory that agePtr points to was not newed, so it doesn't need
to be deleted.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sam  
View profile
 More options Jul 5, 9:49 pm
Newsgroups: comp.lang.c++
From: Sam <s...@email-scan.com>
Date: Sat, 05 Jul 2008 20:49:43 -0500
Local: Sat, Jul 5 2008 9:49 pm
Subject: Re: Pointers and Delete Operator

Denisson writes:
> 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;

Nope.

Only stuff that you've obtained from new must be deleted. No exceptions.

  application_pgp-signature_part
< 1K Download

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Denisson  
View profile
 More options Jul 5, 9:55 pm
Newsgroups: comp.lang.c++
From: Denisson <denisso...@gmail.com>
Date: Sat, 5 Jul 2008 18:55:05 -0700 (PDT)
Local: Sat, Jul 5 2008 9:55 pm
Subject: Re: Pointers and Delete Operator
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

On 5 jul, 22:49, Sam <s...@email-scan.com> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Denisson  
View profile
 More options Jul 5, 10:28 pm
Newsgroups: comp.lang.c++
From: Denisson <denisso...@gmail.com>
Date: Sat, 5 Jul 2008 19:28:29 -0700 (PDT)
Local: Sat, Jul 5 2008 10:28 pm
Subject: Re: Pointers and Delete Operator
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

On 5 jul, 23:14, r...@zedat.fu-berlin.de (Stefan Ram) wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sam  
View profile
 More options Jul 5, 10:54 pm
Newsgroups: comp.lang.c++
From: Sam <s...@email-scan.com>
Date: Sat, 05 Jul 2008 21:54:44 -0500
Local: Sat, Jul 5 2008 10:54 pm
Subject: Re: Pointers and Delete Operator

Denisson writes:
> 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.

The code is valid C++ syntax and grammar.

Here's another valid C++ code that will compile just fine:

int zero()
{
   return 0;

}

int one()
{
   return 1;

}

int main()
{
    one()/zero();

}

Just because code is valid C++ syntax, and compiles without an error, does
not mean that it's correct.

  application_pgp-signature_part
< 1K Download

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jack Klein  
View profile
 More options Jul 5, 11:56 pm
Newsgroups: comp.lang.c++
From: Jack Klein <jackkl...@spamcop.net>
Date: Sat, 05 Jul 2008 22:56:20 -0500
Local: Sat, Jul 5 2008 11:56 pm
Subject: Re: Pointers and Delete Operator
On 6 Jul 2008 02:14:15 GMT, r...@zedat.fu-berlin.de (Stefan Ram) wrote
in comp.lang.c++:

>   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.«)

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.

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«?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Kanze  
View profile
 More options Jul 6, 5:46 am
Newsgroups: comp.lang.c++
From: James Kanze <james.ka...@gmail.com>
Date: Sun, 6 Jul 2008 02:46:55 -0700 (PDT)
Local: Sun, Jul 6 2008 5:46 am
Subject: Re: Pointers and Delete Operator
On Jul 6, 4:28 am, Denisson <denisso...@gmail.com> wrote:

> 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?

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.

--
James Kanze (GABI Software)             email:james.ka...@gmail.com
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Kanze  
View profile
 More options Jul 6, 5:54 am
Newsgroups: comp.lang.c++
From: James Kanze <james.ka...@gmail.com>
Date: Sun, 6 Jul 2008 02:54:34 -0700 (PDT)
Local: Sun, Jul 6 2008 5:54 am
Subject: Re: Pointers and Delete Operator
On Jul 6, 5:56 am, Jack Klein <jackkl...@spamcop.net> wrote:

> On 6 Jul 2008 02:14:15 GMT, r...@zedat.fu-berlin.de (Stefan Ram) wrote
> in comp.lang.c++:
> >   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.«)
> 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.)

--
James Kanze (GABI Software)             email:james.ka...@gmail.com
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Denisson  
View profile
 More options Jul 6, 8:33 am
Newsgroups: comp.lang.c++
From: Denisson <denisso...@gmail.com>
Date: Sun, 6 Jul 2008 05:33:38 -0700 (PDT)
Local: Sun, Jul 6 2008 8:33 am
Subject: Re: Pointers and Delete Operator
#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

On Jul 6, 6:54 am, James Kanze <james.ka...@gmail.com> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google