Valhalla Legends Forums Archive | C/C++ Programming | Address of Function

AuthorMessageTime
HdxBmx27
[code]uint32_t test(){
  return 0;
}

void main()
  uint32_t x = &test;
}[/code]
Is there any reason why x would not equal the address of test()?
I am trying to copy a function to another segment of memory, but & is giving me invalid results.

Yes, that is my entire code at this point, I may jsut be having a major brain lapse. But when I debug this program, x = 0x1002e185, test() = 0x100318c0
May 4, 2009, 2:03 AM
BreW
[quote author=Hdx link=topic=17935.msg182587#msg182587 date=1241402628]
[code]uint32_t test(){
  return 0;
}

void main()
  uint32_t x = &test;
}[/code]
Is there any reason why x would not equal the address of test()?
I am trying to copy a function to another segment of memory, but & is giving me invalid results.

Yes, that is my entire code at this point, I may jsut be having a major brain lapse. But when I debug this program, x = 0x1002e185, test() = 0x100318c0
[/quote]

Yeah, i don't know, but that's just wrong. test is the address of test(), not &test.
Does your code even compile?
May 4, 2009, 2:15 AM
Yegg
What is uint32_t equal to? Is it a typedef struct?
May 4, 2009, 2:15 AM
BreW
[quote author=Yegg link=topic=17935.msg182589#msg182589 date=1241403331]
What is uint32_t equal to? Is it a typedef struct?
[/quote]
Presumably it's an unsigned __int32 type.
May 4, 2009, 2:16 AM
Yegg
[quote author=brew link=topic=17935.msg182590#msg182590 date=1241403365]
[quote author=Yegg link=topic=17935.msg182589#msg182589 date=1241403331]
What is uint32_t equal to? Is it a typedef struct?
[/quote]
Presumably it's an unsigned __int32 type.
[/quote]

I didn't want to make any assumptions. If it's just an unsigned int, why doesn't he use that instead?
May 4, 2009, 2:21 AM
BreW
[quote author=Yegg link=topic=17935.msg182591#msg182591 date=1241403673]
[quote author=brew link=topic=17935.msg182590#msg182590 date=1241403365]
[quote author=Yegg link=topic=17935.msg182589#msg182589 date=1241403331]
What is uint32_t equal to? Is it a typedef struct?
[/quote]
Presumably it's an unsigned __int32 type.
[/quote]

I didn't want to make any assumptions. If it's just an unsigned int, why doesn't he use that instead?
[/quote]

Perhaps he has it jigged up some way in the header files so it's guarenteed to be an unsigned 32 bit integer regardless of chip architecture, word size, compiler, etc.
May 4, 2009, 2:23 AM
HdxBmx27
[quote author=brew link=topic=17935.msg182592#msg182592 date=1241403834]
[quote author=Yegg link=topic=17935.msg182591#msg182591 date=1241403673]
[quote author=brew link=topic=17935.msg182590#msg182590 date=1241403365]
[quote author=Yegg link=topic=17935.msg182589#msg182589 date=1241403331]
What is uint32_t equal to? Is it a typedef struct?
[/quote]
Presumably it's an unsigned __int32 type.
[/quote]

I didn't want to make any assumptions. If it's just an unsigned int, why doesn't he use that instead?
[/quote]

Perhaps he has it jigged up some way in the header files so it's guarenteed to be an unsigned 32 bit integer regardless of chip architecture, word size, compiler, etc.
[/quote]

Indeed, anyways, no shit my code compiles. In theory it should work correctly.
May 4, 2009, 2:27 AM
BreW
[quote author=Hdx link=topic=17935.msg182593#msg182593 date=1241404041]
Indeed, anyways, no shit my code compiles. In theory it should work correctly.
[/quote]
No, it shouldn't compile at all. You're trying to assign a uint32_t the value of a *(*uint32_t)(). What compiler are you using?
May 4, 2009, 2:30 AM
HdxBmx27
VCE9
May 4, 2009, 2:32 AM
Myndfyr
brew is correct.  Your code has a lot of problems.

First, you should be casting to the correct type:

[code]
typedef uint32_t (*PU32FN)();

void main()
{
    PU32FN x = &test;  // x = test; should work as well, but this is more correct.
    cout << x();
}
[/code]

By not using pointer types your code is not 64-bit compatible.  You might say "Well I'm not running 64-bit."  I'll say this, though - at one point it will be common, and you should get into the habits now to not truncate pointer types.

I don't know how your code compiles; I get this error:
[pre]error C2440: 'initializing' : cannot convert from 'uint32_t (__cdecl *)(void)' to 'uint32_t'[/pre]
May 4, 2009, 9:11 PM
HdxBmx27
[quote author=MyndFyre link=topic=17935.msg182624#msg182624 date=1241471487]
brew is correct.  Your code has a lot of problems.

First, you should be casting to the correct type:

[code]
typedef uint32_t (*PU32FN)();

void main()
{
    PU32FN x = &test;  // x = test; should work as well, but this is more correct.
    cout << x();
}
[/code]

By not using pointer types your code is not 64-bit compatible.  You might say "Well I'm not running 64-bit."  I'll say this, though - at one point it will be common, and you should get into the habits now to not truncate pointer types.

I don't know how your code compiles; I get this error:
[pre]error C2440: 'initializing' : cannot convert from 'uint32_t (__cdecl *)(void)' to 'uint32_t'[/pre]
[/quote]Unless you specifically tell your compiler to error on type cast warnings, it will simply display a warning and truncate it for you.

My point being, The code is simplistic, nothing you guys have done yet has changed the functionality of the code at all.

I have had a few other people comment on it. So i'm going to go with them. There blaming it on the debugger. And have offered examples of working code.

And Please for the love of god don't go into 32 vs 64 architectures, yes 64 will eventually become prevalent, and at that time, I will actually give a crap. My code is designed to run on my system, its is merely coincidence that it runs on anyone elses. If I was actually writing code for others to use, for anything consequential I can assure you that I would be more careful, but for the love of god do you guys REALLY think that's final code? seriously?
May 4, 2009, 9:20 PM

Search