473,385 Members | 1,930 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,385 software developers and data experts.

Local Pointers

If I do something like:

int* Foo(){

int *aux, *p, *q;

/*
NIFTY CODE HERE
*/

return aux;

}

Does the program destroy the pointers after exiting the function or
should I delete them anyway at the end of the function?

Nov 2 '05 #1
6 1829
Gaijinco wrote:
If I do something like:

int* Foo(){

int *aux, *p, *q;

/*
NIFTY CODE HERE
Supposedly, filling 'aux' with a valid pointer value...
*/

return aux;

}

Does the program destroy the pointers after exiting the function or
should I delete them anyway at the end of the function?


Returning a value of a pointer is orthogonal to any other action, if by
the time you return the value (and after the function exits) the value is
still valid.

The program destroys _every_ object with automatic storage duration upon
leaving the scope in which they are defined. So, yes, the pointers are
destroyed. Destroying a pointer, though, does NOT mean deleting the
object to which that pointer points. The decision whether to delete or
not to delete something is also completely orthogonal to the rest of the
stuff here. If you don't need the object any more, delete it (through
a pointer to it). If you need the object to survive longer, don't delete
it.

V
Nov 2 '05 #2
Gaijinco wrote:
If I do something like:

int* Foo(){

int *aux, *p, *q;

/*
NIFTY CODE HERE
*/

return aux;

}

Does the program destroy the pointers after exiting the function
Yes. aux, p and q have automatic storage duration so are destroyed when
they go out of scope. In your case that's at the end of the function.
But the rest of your question suggests that you may be confusing two
issues.
or should I delete them anyway at the end of the function?


Read this
http://www.parashift.com/c++-faq-lit....html#faq-16.1

So what you really need to know is, should you delete the memory
pointed to the pointers? To learn that, read the rest of section 16 in
the FAQ.

http://www.parashift.com/c++-faq-lit...tore-mgmt.html

After you've read section 16, you should be left with an impression
that managing pointers and dynamic memory is hard. It is. So have a
look at

http://www.parashift.com/c++-faq-lit....html#faq-34.1

and the rest of the FAQ.

Gavin Deane

Nov 2 '05 #3

On 2 Nov 2005 06:33:14 -0800
"Gaijinco" <ga******@gmail.com> wrote:
If I do something like:

int* Foo(){

int *aux, *p, *q;

/*
NIFTY CODE HERE
*/

return aux;

}

Does the program destroy the pointers after exiting the function or
should I delete them anyway at the end of the function?

Think about
char *a = "abc";
char a[] = "abc";

Nov 2 '05 #4
> Think about
char *a = "abc";
char a[] = "abc";


What about it?

Thanks for the replies! I now understand that the pointers are
destroyed after the function exits, but of course the ponitees are not.
For a while I confused

delete ptr;

with

ptr = NULL;

But things are much clear now.

Thanks again!

Nov 2 '05 #5
lpw
> > Think about
char *a = "abc";
char a[] = "abc";


What about it?


#include <iostream>

using namespace std;

char *f1() {
char *a = "abc";
return a; // return pointer to statically allocated string constant
}

char *f2() {
char a[] = "abc";
return a; // return address of a local array
}
int main(void) {
cout << f1() << endl; // ok
cout << f2() << endl; // hell breaks loose
}
Nov 2 '05 #6

"Gaijinco" <ga******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Think about
char *a = "abc";
char a[] = "abc";
What about it?


For clarity, I'll change one name:

char * func()
{
char *a = "abc";
char b[] = "abc";

return a; /* OK */
return b; /* ERROR */
}

Since the string pointed to by 'a' is a literal,
its lifetime is that of the program (it's not
destroyed until termination). The array 'b's
lifetime ends when the function returns (it's
an 'auto' object, so it gets destroyed.)

So returning 'a' to the caller gives the caller
access to the literal. But returning 'b' (the
array's name, which will be converted to a pointer
to its first element), would give the caller the
address of an object that has just been destroyed.

Thanks for the replies! I now understand that the pointers are
destroyed after the function exits, but of course the ponitees are not.
That depends upon where the 'pointees' live.

void f1()
{
int i = 0;
int *p = &i;
return; /* both 'i' (the pointee) and
'p' (the pointer) are destroyed */
}

void f2()
{
int *p = new int;
return; /* 'p' is destroyed, but the object created
by the 'new' operator is not */
/* thus, this function would create a memory leak, since
it does not preserve the value of 'p' or pass it to
anywhere else */
}

} For a while I confused

delete ptr;

with

ptr = NULL;
Not a good thing. :-)

But things are much clear now.


IMO it's actually rather rare to really need to
use 'new' and 'delete' directly. The mechanisms of auto
storage class, and the standard library containers
which handle their own memory management cover
most cases.

-Mike
Nov 2 '05 #7

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

Similar topics

2
by: Kench | last post by:
I was curious and playing with pointers and references to see what's different between them. Other than the obvious ones involving C++ syntax & things like references cannot be modified with...
11
by: Alzane | last post by:
I'm new to C++ programming. I have an exercise that I have written code for but getting warnings. Can I get some help? #include "stdafx.h" #include <string.h> #include <stdio.h> #include...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
2
by: silversurfer | last post by:
Hello, I am a little unsure whether this method really makes sense. The goal is to add an element to a vector. This is the struct and method I am using: std::vector<Entry> models; struct...
8
by: Michael Howes | last post by:
I have some code that manages local user logins. When I create a new user I want to set the password to expire every x days and the number of failed login attempts before the account is...
23
by: deepakvsoni | last post by:
Why does C++ restrict definition of functions with in functions? int f1() { int f2() { ..... } } //This is not supported. Thanks in advance to anybody who replies..
3
by: abhishek | last post by:
Hi group any idea on HOW TO HANDLE POINTERS FROM NON-LOCAL HEAPS?? Thank you
8
by: Samant.Trupti | last post by:
Hi All, I am facing a strange problem.. I am calling a function func2 from func1. Before calling func2 all the local variables in func1 looks fine and has their respective values. When...
28
by: cpluslearn | last post by:
Hi, I have a local class inside a function template. I am able to wrap any type in my local class. Is it legal C++? What type of class is Local? Is it a class template or regular class? Thanks...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.