question about pointers inside functions

F

Fernando Barsoba

Hi all,

I'm trying to reassign the address of a pointer inside a function and
return it once the function has done its things... However, the address
returned is not the address I wanted to return.. let me explain it
better with the actual example:

caller function:
struct authenHeader * AH;
setAuhenticationH(authenHeader, sizeof(authenHeader), AH);

Memory address for AH here is 0x610fd552

callee function:
struct authenHeader * setAuhenticationH(uint8_t *autH, unsigned short length, struct authenHeader *AH) {

int authenHeader_size;

authenHeader_size = sizeof(struct authenHeader);
bzero(autH, authenHeader_size);
AH = (struct authenHeader *)autH;

Memory address after assignment is 0x22ec30
AH->nexth = 4;
AH->payloadlen = 4; /* for tests, standard case: 3 32-bit word fixed portion + 96-bit authentication
value. In 32-bit words, 3+3-2 = 4 */
AH->resvd = 0;
AH->spi = 0;
AH->seqnum = 0x00;
return AH;
}

The memory address returned is supposedly 0x22ec30, but when going back
to the caller function, the memory address is 0x610fd552.

Clearly, I'm doing something wrong conceptually.. any idea on how to
solve this?

Thanks,

FBM
 
M

mazsx

Hi,
Fernando said:
Hi all,

I'm trying to reassign the address of a pointer inside a function and
return it once the function has done its things... However, the address
returned is not the address I wanted to return.. let me explain it
better with the actual example:

caller function:


Memory address for AH here is 0x610fd552

callee function:


Memory address after assignment is 0x22ec30

The memory address returned is supposedly 0x22ec30, but when going back
to the caller function, the memory address is 0x610fd552.

Clearly, I'm doing something wrong conceptually.. any idea on how to
solve this?

Thanks,

FBM

Your setAuhenticationH function is supposed to return a pointer to
struct, as it indeed does.
You seem to expect that the third argument of your function gets this
value after the function returns. You should better write
AH=setAuhenticationH(....

By the way:
- do you use the value AH in your callee? You passed an uninitilazied
value, that you immediately
modify. So probably you simply do not need to declare the function
with three args.
- bzero is deprecated and non standand, use memset instead
- the first argument of your function is identical to the name of the
sturcture. This is allowed,
since structure tags use a name space other than the variables do.
But be sure that you
are not confused by this.
- I would better use size_t for the value of the sizeof operator

mazsx
 
F

ferbar

mazsx said:
Hi,

Your setAuhenticationH function is supposed to return a pointer to
struct, as it indeed does.
You seem to expect that the third argument of your function gets this
value after the function returns. You should better write
AH=setAuhenticationH(....

Thanks a lot for your response.. I did as you said and it's working.
By the way:
- do you use the value AH in your callee? You passed an uninitilazied
value, that you immediately
modify. So probably you simply do not need to declare the function
with three args.
- bzero is deprecated and non standand, use memset instead
- the first argument of your function is identical to the name of the
sturcture. This is allowed,
since structure tags use a name space other than the variables do.
But be sure that you
are not confused by this.
- I would better use size_t for the value of the sizeof operator

And thanks also for these other points..

FBM
 
J

Jack Klein

On Mon, 14 Nov 2005 19:23:14 GMT, Fernando Barsoba

In addition to what mazsx said, all of which was good advice, I notice
a few things.
Hi all,

I'm trying to reassign the address of a pointer inside a function and
return it once the function has done its things... However, the address
returned is not the address I wanted to return.. let me explain it
better with the actual example:

caller function:

Perhaps you are omitting some of your code, but as the line above is
written, it defines a pointer to a structure, but does not initialize
it to point to an actual structure, or indeed anything useful at all.
If this definition is at file scope, that is outside of any function,
then the pointer is initialized to NULL. If this definition is inside
a function, AH contains uninitialized gibberish, and any use of its
value causes undefined behavior.
Memory address for AH here is 0x610fd552

You statement above is ambiguous. When you are talking about a
pointer, there are two addresses involved. Assuming the pointer
points to a valid object, it contains the address of the object
pointed to. But since a pointer is an object itself, the pointer has
an address, as well as holds an address, and the two addresses are
usually different.
callee function:

Now here's the thing that really got my attention. There is no
guarantee at all, unless the address in 'autH' came from malloc(), or
from taking the address of a real authenHeader structure, that the
address is correctly aligned for the structure.

If it is not properly aligned, all the access to it below, and most
likely in the caller when you return the value properly, cause
undefined behavior. On some hardware architectures that merely means
they take longer to execute. On others, you can generate a trap that
could cause your program to crash, or your operating system to shut it
down.
Memory address after assignment is 0x22ec30

The memory address returned is supposedly 0x22ec30, but when going back
to the caller function, the memory address is 0x610fd552.

Clearly, I'm doing something wrong conceptually.. any idea on how to
solve this?

Thanks,

Why do you need to create a structure out of a pointer to raw memory?
Why can't you create an instance of the structure and use it instead?
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top