pointers in functions

R

raghu

look at the following code
#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 = get_number(i);
printf("for i = %d address %u and its contents %d\n", i, krrk,
*krrk);
for(i = 1; i < 5; i++)
{

krrk = get_number(i);
printf("for i = %d address %u and its contents %d\n", i, krrk,
*krrk);
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, krrk);
}
}
int * get_number(int i)
{
return &i;
}

testing the address of a variable in the function
i address i nmain 2280636
for i = 0 address 2280576 and its contents 0
for i = 1 address 2280576 and its contents 1
for i -1 = 0 address 2280576 and its contents 4202572
for i = 2 address 2280576 and its contents 2
for i -1 = 1 address 2280576 and its contents 4202572
for i = 3 address 2280576 and its contents 3
for i -1 = 2 address 2280576 and its contents 4202572
for i = 4 address 2280576 and its contents 4
for i -1 = 3 address 2280576 and its contents 4202572

for i = 0 address of 4202616 is 2280576
for i = 1 address of 4202664 is 2280576
for i = 2 address of 4202664 is 2280576
for i = 3 address of 4202664 is 2280576
for i = 4 address of 4202664 is 2280576

Here I am returning a local variable of a function And capturing its
address in a array of pointers. In the main function for the first
access its giving the correct answer but for the second access its
giving wrong(what ever order either 'for i = 1..' or 'for i - 1 =
0..'). why it behaves like so?

If we use msgQCreate in the same fashion its working fine why?

awaitng for ur reply.

-Raghu
 
G

gw7rib

raghu said:
int * get_number(int i)
{
return &i;
}

The variable i only exists for as long as the function is running.
While the function is running, &i is the address where the current
value of i is stored. Afterwards, the value returned is the address
where i *was* stored at, which is now free for the program to use for
something else if it wants to. This is why you are getting problems.

Don't take or use the address of something unless that something is
still there!

Hope that helps.
Paul.
 
R

raghu

The variable i only exists for as long as the function is running.
While the function is running, &i is the address where the current
value of i is stored. Afterwards, the value returned is the address
where i *was* stored at, which is now free for the program to use for
something else if it wants to. This is why you are getting problems.

Don't take or use the address of something unless that something is
still there!

Hope that helps.
Paul.
Yes I understood thank you. 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.
 
R

Remo D.

raghu ha scritto:
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.

In the first code you sent, you returned the address of a local variable
which is meaningless.

In this example you return the value of a function passing the address
of a local variable as argument.

Do you see the difference?

In the first case you tried to use the address of a variable that no
longer existed. In the second case the local variable msg will be
deallocated only *after* the function vos_mq_send () has been computed.

Remo.D
 
R

raghu

raghu ha scritto:



In the first code you sent, you returned the address of a local variable
which is meaningless.

In this example you return the value of a function passing the address
of a local variable as argument.

Do you see the difference?

In the first case you tried to use the address of a variable that no
longer existed. In the second case the local variable msg will be
deallocated only *after* the function vos_mq_send () has been computed.

Remo.D

you are right. There is a difference in returning. But here we are
passing the variable msg which is local to this function to
vos_mq_send function. As I told earlier it is as similar to that of
msgQCreate() it will store the msg varible in message queue memory.
For the second call same memory might be assigned and the first node
might be corrupted right.
 
K

Keith Thompson

raghu said:
Yes I understood thank you. 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.

It's perfectly ok to pass the address of a local variable to another function in
a call. The call to mac_ipc_send_ipc_msg remains active, and its local
variables continue to exist, while vos_mq_send is executing; vos_mq_send can
safely refer (indirectly) to msg, even though it's local to mac_ipc_send_ipc_msg.

Now if vos_mq_send *returned* the value of &msg as its result, and
mac_ipc_send_ipc_msg then returned that address to its caller, then you'd be in
trouble, but that doesn't seem to be what's happening.
 
R

Remo D.

raghu ha scritto:
On Nov 14, 3:11 pm, "Remo D." <rdentato> wrote:

you are right. There is a difference in returning. But here we are
passing the variable msg which is local to this function to
vos_mq_send function. As I told earlier it is as similar to that of
msgQCreate() it will store the msg varible in message queue memory.
For the second call same memory might be assigned and the first node
might be corrupted right.

It's hard to say without knowing the details around vos_mq_send or
msgQCreate.

For example, one of the functions may create a copy of msg and return
the new address while the other doesn't do it ...

Anyway, if you look deeper into those functions keeping in mind what we
suggested you, I'm sure you'll find the reason of this unexpected behaviour.

Good hunting.

Remo.D
 
P

pete

raghu said:
On Nov 14, 3:11 pm, "Remo D." <rdentato> wrote:

you are right. There is a difference in returning. But here we are
passing the variable msg which is local to this function to
vos_mq_send function.

I don't think you know what "passing" means.
What that really means is that you are initializing
a parameter in vos_mq_send,
with the value that msg has in the calling function.

And like he said, the variable msg,
exists until the calling function completes.
 
C

CBFalconer

raghu said:
look at the following code

Why are you starting two independent threads, with the difference
being the case of the first letter of the subject?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top