By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,948 Members | 1,215 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,948 IT Pros & Developers. It's quick & easy.

question about pointers inside functions

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,
Fernando Barsoba wrote:
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


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

Nov 15 '05 #2

P: n/a
mazsx wrote:
Hi,
Fernando Barsoba wrote:
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:
... 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
mazsx


Nov 15 '05 #3

P: n/a
On Mon, 14 Nov 2005 19:23:14 GMT, Fernando Barsoba
<fb******@verizon.net> wrote in comp.lang.c:

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:
struct authenHeader * AH;
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.
setAuhenticationH(authenHeader, sizeof(authenHeader), AH);
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:
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;

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

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,


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?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.