473,399 Members | 3,832 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,399 software developers and data experts.

Pointer in functions

look at the code below
#include <stdio.h>
int * get_number(int i);

main()
{
int *krrk[5];
int i = 0;
printf(" testing the address of a variable in the function\n i
address in main %u\n", &i);
krrk[i] = get_number(i);
printf("for = i %d address %u and its contents %d\n", i, krrk[i],
*krrk[i]);
for(i = 1; i < 5; i++)
{

krrk[i] = get_number(i);
printf("for i = %d address %u and its contents %d\n", i, krrk[i],
*krrk[i]);
printf("for i - 1 = %d address %u and its contents %d\n", i - 1,
krrk[i - 1], *krrk[i - 1]);
}
for( i = 0 ; i < 5; i++)
{
printf(" for i = %d address of %d is %u\n", i, *krrk[i], krrk[i]);
}
}
int * get_number(int i)
{
return &i;
}
output of this file (I got) is as follows
testing the address of a variable inthe function
i address in main 2280636
for i = 0 address 2280576 and its contents 0
for i = 1 address 2280576 and its contents 1
for i -1 = 0 addrss 2280576 and its contents 4202616
for i = 2 address 2280576 and its contents 2
for i -1 = 1 addrss 2280576 and its contents 4202616
for i = 3 address 2280576 and its contents 3
for i -1 = 2 addrss 2280576 and its contents 4202616
for i = 4 address 2280576 and its contents 4
for i -1 = 3 addrss 2280576 and its contents 4202616

for i = 0 address of 4202660 is 2280576
for i = 1 address of 4202708 is 2280576
for i = 2 address of 4202708 is 2280576
for i = 3 address of 4202708 is 2280576
for i = 4 address of 4202708 is 2280576
my doubt is here I am returning a local variable of a function
and so Its get corrupted for the next call(if same address is assigned
to that local variable) but here I am getting a garbage value(4202616)
when I pointing to same address again. why?

In the second for loop I am getting different values than before
why?

awaiting for ur reply
- Raghu

Nov 14 '07 #1
6 1443
raghu wrote On 11/14/07 15:04,:
look at the code below
[...]
int * get_number(int i)
{
return &i;
}
output of this file (I got) is as follows
[...]
I don't think the code you posted produced the
output you showed. I think you re-typed one or the
other, possibly both, and introduced discrepancies
along the way. Please don't do that any more; post
*exactly* the code you used and the output you got.
my doubt is here I am returning a local variable of a function
and so Its get corrupted for the next call(if same address is assigned
to that local variable) but here I am getting a garbage value(4202616)
when I pointing to same address again. why?

In the second for loop I am getting different values than before
why?
The answer to both questions is "The program invokes
undefined behavior, so anything at all can happen. No
outcome, however it may startle you, should be in the
least bit surprising."

In this case, it seems likely that the value returned
by get_number() points to a location on a stack, a location
that has been "popped away" by the time get_number()
returns and is no longer in use when the caller regains
control. Since the pointed-to stack location is unused,
other parts of the system use it for their own purposes
and leave various kinds of garbage in it as they go
about their business. Your main() function tries to make
use of the garbage; that's Not Recommended.

You have written your name in the wet sand, and then
let the tide wash in and out several times. Someone now
asks your name, and you tell him to go down to the beach
and read it. He does so, returns, and greets you with
a cheery "Hello, Seaweed!" Why should you be surprised?

--
Er*********@sun.com
Nov 14 '07 #2
raghu wrote:
>
look at the code below
#include <stdio.h>
int * get_number(int i);

main()
{
int *krrk[5];
int i = 0;
printf(" testing the address of a variable in the function\n i
address in main %u\n", &i);
krrk[i] = get_number(i);
printf("for = i %d address %u and its contents %d\n", i, krrk[i],
*krrk[i]);
for(i = 1; i < 5; i++)
{

krrk[i] = get_number(i);
printf("for i = %d address %u and its contents %d\n", i, krrk[i],
*krrk[i]);
printf("for i - 1 = %d address %u and its contents %d\n", i - 1,
krrk[i - 1], *krrk[i - 1]);
}
for( i = 0 ; i < 5; i++)
{
printf(" for i = %d address of %d is %u\n", i, *krrk[i], krrk[i]);
}
}
int * get_number(int i)
{
return &i;
}
output of this file (I got) is as follows
testing the address of a variable inthe function
i address in main 2280636
for i = 0 address 2280576 and its contents 0
for i = 1 address 2280576 and its contents 1
for i -1 = 0 addrss 2280576 and its contents 4202616
for i = 2 address 2280576 and its contents 2
for i -1 = 1 addrss 2280576 and its contents 4202616
for i = 3 address 2280576 and its contents 3
for i -1 = 2 addrss 2280576 and its contents 4202616
for i = 4 address 2280576 and its contents 4
for i -1 = 3 addrss 2280576 and its contents 4202616

for i = 0 address of 4202660 is 2280576
for i = 1 address of 4202708 is 2280576
for i = 2 address of 4202708 is 2280576
for i = 3 address of 4202708 is 2280576
for i = 4 address of 4202708 is 2280576

my doubt is here I am returning a local variable of a function
and so Its get corrupted for the next call(if same address is assigned
to that local variable) but here I am getting a garbage value(4202616)
when I pointing to same address again. why?

In the second for loop I am getting different values than before
why?
The whole program makes no sense.
You're trying to display the addresses and values of rvalues.
rvalues don't have addresses.

--
pete
Nov 14 '07 #3
On Nov 14, 12:31 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
raghu wrote On 11/14/07 15:04,:
look at the code below
[...]
int * get_number(int i)
{
return &i;
}
output of this file (I got) is as follows
[...]

I don't think the code you posted produced the
output you showed. I think you re-typed one or the
other, possibly both, and introduced discrepancies
along the way. Please don't do that any more; post
*exactly* the code you used and the output you got.
The code above is the same one which I compiled and exectued. I just
copied the code only after executing the code and output is the same
one I am got. I am using cygwin in windows machine.
my doubt is here I am returning a local variable of a function
and so Its get corrupted for the next call(if same address is assigned
to that local variable) but here I am getting a garbage value(4202616)
when I pointing to same address again. why?
In the second for loop I am getting different values than before
why?

The answer to both questions is "The program invokes
undefined behavior, so anything at all can happen. No
outcome, however it may startle you, should be in the
least bit surprising."

In this case, it seems likely that the value returned
by get_number() points to a location on a stack, a location
that has been "popped away" by the time get_number()
returns and is no longer in use when the caller regains
control. Since the pointed-to stack location is unused,
other parts of the system use it for their own purposes
and leave various kinds of garbage in it as they go
about their business. Your main() function tries to make
use of the garbage; that's Not Recommended.
I got it. Is it okay to pass structure instance.
look at the following code and the authors claims that its working
mac_ipc_send_ipc_msg ( char *mq_name,
int mq_id,
int options,
IPC_MSG_TYPE msgtype,
int msglen,
char *data,
void (*cb)(void *))
{
ipc_msg_hdr_t msg ;

msg.type = msgtype;
msg.len = msglen ;
msg.data = data;
msg.free_cb = cb;

return vos_mq_send(mq_name,
mq_id,
(char *)&msg,
sizeof(ipc_msg_hdr_t),
(int)MSG_PRI_NORMAL);
}
In the above code vos_mq_send is similar msgQCreate() function.
then what will be the behaviour of 'msg' variable. Will it works?
The testers also saying that it works. I didn't understand why?
Any comments on this.
You have written your name in the wet sand, and then
let the tide wash in and out several times. Someone now
asks your name, and you tell him to go down to the beach
and read it. He does so, returns, and greets you with
a cheery "Hello, Seaweed!" Why should you be surprised?

--
Eric.Sos...@sun.com

Nov 14 '07 #4
raghu wrote:
look at the code below
I'll just make a few comments.
#include <stdio.h>
int * get_number(int i);

main()
This should be "int main(void)".
{
int *krrk[5];
int i = 0;
printf(" testing the address of a variable in the function\n i
address in main %u\n", &i);
"%u" is the wrong format to print an address. To print an address,
use "%p". Since "%p" specifically requires a void*, and since in
this particular case, the compiler doesn't know that it needs to do
an implicit conversion, you need to convert explicitly to void*.

Also, "%p" typically uses a format that makes sense for the platform
(typically hexadecimal).

So:

printf("Address of i is %p\n", (void*)&i);

[...]
int * get_number(int i)
{
return &i;
}
This function doesn't do anything useful. Function arguments are
passed by value in C. In other words, an argument (an expression
between parentheses in a function call) is evaluated, and its value
is *copied* and assigned to the parameter (your "int i", which is a
local variable within the function).

Inside get_number, you take the address of this local variable,
which has nothing to do with the argument you passed to the function
(except that the argument value was copied to it). Once you leave
the function, the local variable (the parameter) no longer exists,
and its address is completely meaningless.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 14 '07 #5
raghu wrote On 11/14/07 17:41,:
On Nov 14, 12:31 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
>>raghu wrote On 11/14/07 15:04,:

>>>look at the code below
[...]
int * get_number(int i)
{
return &i;
}
output of this file (I got) is as follows
[...]

I don't think the code you posted produced the
output you showed. I think you re-typed one or the
other, possibly both, and introduced discrepancies
along the way. Please don't do that any more; post
*exactly* the code you used and the output you got.

The code above is the same one which I compiled and exectued. I just
copied the code only after executing the code and output is the same
one I am got. I am using cygwin in windows machine.
I still don't believe you. Here's the first output
call in your program source:
printf(" testing the address of a variable in the function\n i
address in main %u\n", &i);
.... and here is the first line of output:
testing the address of a variable inthe function
i address in main 2280636
What happened to the space between "in" and "the"? What
happened to the space at the start of the second output line?

The second output call is:
printf("for = i %d address %u and its contents %d\n", i, krrk[i],
*krrk[i]);
.... and the output it supposedly produced is:
for i = 0 address 2280576 and its contents 0
How did the "i" and the "=" get transposed? True, this one
might not be your fault: The program invokes undefined behavior
before attempting this printf() call, and it's conceivable that
the symptom of the undefined behavior is to swap i's and ='s
in all subsequent output. Doesn't seem likely, though.
I got it. Is it okay to pass structure instance.
You still don't quite get it, or don't seem to. The error
in the first program isn't that the returned value is a pointer,
but that it points to something -- a function parameter -- that
ceased to exist when the function returned. The function has
returned the "telephone number" of a "telephone" that was
destroyed in the process of returning.
look at the following code and the authors claims that its working
mac_ipc_send_ipc_msg ( char *mq_name,
int mq_id,
int options,
IPC_MSG_TYPE msgtype,
int msglen,
char *data,
void (*cb)(void *))
{
ipc_msg_hdr_t msg ;

msg.type = msgtype;
msg.len = msglen ;
msg.data = data;
msg.free_cb = cb;

return vos_mq_send(mq_name,
mq_id,
(char *)&msg,
sizeof(ipc_msg_hdr_t),
(int)MSG_PRI_NORMAL);
}
In the above code vos_mq_send is similar msgQCreate() function.
then what will be the behaviour of 'msg' variable. Will it works?
The testers also saying that it works. I didn't understand why?
Any comments on this.
The msg variable is created when mac_ipc_send_ipc_msg()
starts, and exists until it returns. Before it returns,
it calls vos_mq_send(), which runs to completion and returns
a value. After this happens, mac_ipc_send_ipc_msg() itself
returns, passing the same value to its caller.

While vos_mq_send() is running, mac_ipc_send_ipc_msg()
has not yet returned. So msg has not yet been destroyed,
and vos_mq_send() can safely use the pointer to it. The
destruction of msg happens when mac_ipc_send_ipc_msg()
returns, not while it is merely "suspended" to allow a
called function to run.

--
Er*********@sun.com
Nov 14 '07 #6
raghu wrote:
>
look at the code below
[...]
int * get_number(int i)
{
return &i;
}
[...]
my doubt is here I am returning a local variable of a function
and so Its get corrupted for the next call(if same address is assigned
to that local variable) but here I am getting a garbage value(4202616)
when I pointing to same address again. why?
You're not returning a local variable. You're returning the
address of a local variable. That pointer becomes invalid
as soon as the function returns. Derefencing that pointer
invokes undefined behavior. (And, while simply dereferencing
it is unlikely to cause problems on most systems, using the
value stored there will cause problems.)
In the second for loop I am getting different values than before
why?
Because UB can cause anything to happen.

In your case, the address is likely to point to the last paramaeter
passed to the most recent function call, but even that guess is
just a guess.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Nov 15 '07 #7

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

Similar topics

9
by: iceColdFire | last post by:
HI, I have a function as void f(int p) { return p++; } now I have created a function pointer as
4
by: msolem | last post by:
I have some code where there are a set of functions that return pointers to each other. I'm having a bit of a hard time figuring out the correct type to use to do that. The code below works but...
14
by: Bryan Parkoff | last post by:
Do you know that current C++ Compiler limits to 64KB segments in source code? It is good news that Microsoft Visual C++ 2005 has expanded to 4GB segments in source code. 4GB segment is ideal for...
11
by: Felix Kater | last post by:
Hi, I can compile and run this code (see below) which twice calls the function f, first with too less, second with too much arguments. But is it legal and free of memory leaks and other...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
6
by: dtschoepe | last post by:
Hi all, Working on homework again... I've got a weird problem, I've been banging my head against the wall on what is causing it. I have a pointer to a typdef named Person. At one point in the...
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
14
by: Remo D. | last post by:
I know that a variable (void *) is guaranteed to be able to store any pointer and to allow conversion back to the original pointer. I think, then, that I can store a pointer to a function in a...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.