473,326 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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
Jul 6 '08 #1
9 1304
Denisson <de********@gmail.comwrote:
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.
Jul 6 '08 #2
Sam
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.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkhwJLYACgkQx9p3GYHlUOIubgCdGC8gTgEy5Z GYlkm9pbAk1n0P
2gEAnjo7FuEaKlYpGTvJLfJODlxjvUSG
=l9VT
-----END PGP SIGNATURE-----

Jul 6 '08 #3
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.comwrote:
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
1KDownload
Jul 6 '08 #4
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:
Denisson <denisso...@gmail.comwrites:
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.«)
Jul 6 '08 #5
Sam
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.

Thanks

On 5 jul, 22:49, Sam <s...@email-scan.comwrote:
>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
1KDownload
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkhwM/QACgkQx9p3GYHlUOJyHQCfZFubFTEPDpCByfYojsEQA9yt
qnwAnjpqRCV0L6yJ9DuagXDnIjPzZ8kF
=ujqR
-----END PGP SIGNATURE-----

Jul 6 '08 #6
On 6 Jul 2008 02:14:15 GMT, ra*@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
Jul 6 '08 #7
On Jul 6, 4:28 am, Denisson <denisso...@gmail.comwrote:
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:ja*********@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
Jul 6 '08 #8
On Jul 6, 5:56 am, Jack Klein <jackkl...@spamcop.netwrote:
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:ja*********@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
Jul 6 '08 #9
#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.comwrote:
On Jul 6, 5:56 am, Jack Klein <jackkl...@spamcop.netwrote:
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
Jul 6 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Der Andere | last post by:
I have an array of pointers (to a class) which I want to have sorted. I have implemented the < operator for the class but I guess STL sort will sort the pointers according to _their_ values (the...
4
by: jagguy | last post by:
can you declare variables as pointers without using new or make them point at another variable. this works I thought it shouldn't char *p ; p="xat"; cout << p <<endl;
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
6
by: Martin | last post by:
Hi I need to maintain a <setof pointers to objects, and it must be sorted based on the values of pointed objects, not pointer values. I can achieve this easily by defining my own comparing...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
22
by: sandy | last post by:
I am trying to make a simulated directory structure application for a course. I am using a Vector to store pointers to my Directory objects (as subdirectories of the current object). In my...
3
by: mati-006 | last post by:
Hi, I think the code will be the best way to explain what I mean: #include "arglib/arg_shared.h" class base { public: base() {} virtual ~base() {} };
10
by: =?iso-8859-1?q?Ernesto_Basc=F3n?= | last post by:
I am implementing my custom smart pointer: template <typename T> class MySmartPtr { public: MySmartPtr(T* aPointer) { mPointer = aPointer; }
8
by: Malciah | last post by:
I posted this on another site, but so far I've had no answers. So, I decided to try it here. -------------------------------------------------------- I've been learning C++ for about 6 weeks now,...
11
by: desktop | last post by:
I have made this example: #include<iostream> class Beer { public: Beer() { std::cout << "made a beer\n"; num = 1; }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.