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

Is pointer parameter local variable to a function?

ali
Hi,

When I pass a pointer as an argument of a method, is it safe if I
change the data pointed by the argument and return it upon
completion ?

For example:

Object* someFunction(Object* ob)
{
//do some manipulation and return
return ob;
}

Are there things I need to be aware/cautious when doing this? I tried
with int pointer and got the results as expected (no error or garbage
output). I had thought it would give me garbage result because to my
undestanding, ob would be a local variable to someFunction, and should
have been destroyed upon return?

I know that if within the body of the function above, I created a
pointer and assigned memory with new operator, I should not return the
pointer because it will be destroyed upon exit and I might be getting
garbage.

I know i'm missing some simple concept. Will appreciate your help.

Thanks,

Ali

Mar 7 '07 #1
3 2705
ali wrote:
When I pass a pointer as an argument of a method, is it safe if I
change the data pointed by the argument and return it upon
completion ?
Sure. You're receiving a value, you're using the value [to change
the data], you're returning the [same, supposedly] value. What's
the problem?
For example:

Object* someFunction(Object* ob)
{
//do some manipulation and return
return ob;
}

Are there things I need to be aware/cautious when doing this?
The pointer passed in could be NULL. Make sure you check before you
try to manipulate the data pointed to by the pointer.
I tried
with int pointer and got the results as expected (no error or garbage
output). I had thought it would give me garbage result because to my
undestanding, ob would be a local variable to someFunction, and should
have been destroyed upon return?
The variable will be destroyed, but the value you're returning isn't.
What happens if you do

int foo(int a)
{
int b = a + 42;
// do something to b
return a;
}

Nothing special, right. You get the value, you use the value, and then
you return the value. 'a' variable does get destroyed, but not before
the temporary of type 'int' is created and returned.
I know that if within the body of the function above, I created a
pointer and assigned memory with new operator, I should not return the
pointer because it will be destroyed upon exit and I might be getting
garbage.
Sounds wrong.
I know i'm missing some simple concept. Will appreciate your help.
It's hard to explain if you can't grasp the [simple] concept of
passing around values. If you can separate two concepts in your
mind: of variables and values. Variables do have values, but you
don't need a variable to have a value you can pass around. That's
what an 'r-value' is.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 7 '07 #2
ali wrote:
Hi,

When I pass a pointer as an argument of a method, is it safe if I
change the data pointed by the argument and return it upon
completion ?

For example:

Object* someFunction(Object* ob)
{
//do some manipulation and return
return ob;
}

Are there things I need to be aware/cautious when doing this? I tried
with int pointer and got the results as expected (no error or garbage
output). I had thought it would give me garbage result because to my
undestanding, ob would be a local variable to someFunction, and should
have been destroyed upon return?
You need to distinguish the pointer and the pointee. Both are objects:

The pointer ob is an object of type Object*. It is local to someFunction.
The pointee *ob is an object of type Object. It is not local.

The pointer ob is destroyed when it does out of scope. That, however, has no
effect on the pointee. It lives on and needs to be disposed off at some
later time.

I know that if within the body of the function above, I created a
pointer and assigned memory with new operator, I should not return the
pointer because it will be destroyed upon exit and I might be getting
garbage.
You don't know that because you can only know true statements. You might
believe so; but you would be wrong: It is safe, to return a pointer from a
function that has been allocated within the body of the function. There are
even cases where it can be considered good design.
Best

Kai-Uwe Bux
Mar 7 '07 #3
ali
On Mar 6, 10:13 pm, "ali" <aliasger.jaf...@gmail.comwrote:
Hi,

When I pass a pointer as an argument of a method, is it safe if I
change the data pointed by the argument and return it upon
completion ?

For example:

Object* someFunction(Object* ob)
{
//do some manipulation and return
return ob;

}

Are there things I need to be aware/cautious when doing this? I tried
with int pointer and got the results as expected (no error or garbage
output). I had thought it would give me garbage result because to my
undestanding, ob would be a local variable to someFunction, and should
have been destroyed upon return?

I know that if within the body of the function above, I created a
pointer and assigned memory with new operator, I should not return the
pointer because it will be destroyed upon exit and I might be getting
garbage.

I know i'm missing some simple concept. Will appreciate your help.

Thanks,

Ali
Ooh, now I get the idea .. thanks for all your help!

Mar 7 '07 #4

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

Similar topics

110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
34
by: wilson | last post by:
Hi All, I am a novice at C and just have learned pointer for a short period. Today one error had occured while executing my program below. And I cannot find the error out since it's checked "OK"...
7
by: Marcelo | last post by:
Hi everybody, I don't understand why I am having a problem in this code. The problem is that my pointer *phist in main method, it is declared. Then I send the pointer to my method, and this...
7
by: william | last post by:
My question is: Specific memory block where my pointer pointing to changed strangely, seemingly that no statement changed it. Here are two examples I got: ***********1***************** I was...
7
by: lovecreatesbea... | last post by:
Shoud we declare non-pointer function parameters with const keywords? int main(void){ int f(const int i); int i; f(i); return 0; }
16
by: gaga | last post by:
my function should accept a pointer to a string as its argument. and it should convert each charater to an uppercase letter. I know what i need to do, its just my syntax is all out of whack. ...
6
by: raghu | last post by:
look at the code below #include <stdio.h> int * get_number(int i); main() { int *krrk; int i = 0; printf(" testing the address of a variable in the function\n i address in main %u\n", &i);
34
by: Davy | last post by:
Hi all, I am writing a function, which return the pointer of the int. But it seems to be wrong. Any suggestion? int * get_p_t(int t) { return &t; } int main()
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.