473,397 Members | 2,056 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,397 software developers and data experts.

is a static functions address constant?

is a static functions address constant?
ie..

static void func();
write_to_file(&func);

Restart program...

static void func();
void (*funcPtr) ();
funcPtr = read_from_file();

funcPtr == &func
Jul 22 '05 #1
7 2136
* Nolan Martin:
is a static functions address constant?
ie..

static void func();
write_to_file(&func);

Restart program...

static void func();
void (*funcPtr) ();
funcPtr = read_from_file();

funcPtr == &func


No.

It is constant during _one_ execution program.

Nothing more is guaranteed by the language, although on
particular systems some functions may or will often or always
end up on the same address -- e.g. crackers use knowledge
of such things to exploit buffer overruns.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
* Nolan Martin:
is a static functions address constant?
ie..

static void func();
write_to_file(&func);

Restart program...

static void func();
void (*funcPtr) ();
funcPtr = read_from_file();

funcPtr == &func


No.

It is constant during _one_ execution program.

Nothing more is guaranteed by the language, although on
particular systems some functions may or will often or always
end up on the same address -- e.g. crackers use knowledge
of such things to exploit buffer overruns.


How would I make it so that the example above would work?
I need a way to return to the function across multiple sessions of the
program by reading and writing a "pointer" to the function from a file.
Jul 22 '05 #3
"Nolan Martin" <mo**********@hotmail.com> wrote...
* Nolan Martin:
is a static functions address constant?
ie..

static void func();
write_to_file(&func);

Restart program...

static void func();
void (*funcPtr) ();
funcPtr = read_from_file();

funcPtr == &func


No.

It is constant during _one_ execution program.

Nothing more is guaranteed by the language, although on
particular systems some functions may or will often or always
end up on the same address -- e.g. crackers use knowledge
of such things to exploit buffer overruns.


How would I make it so that the example above would work?
I need a way to return to the function across multiple sessions of the
program by reading and writing a "pointer" to the function from a file.


The "right" way would be to make up some kind of persistent identification
system, like a name that (a) is unique (b) can be associated with a function
and (c) can be written to file and read from it. Such name would have the
association with a function in your code, and you can store the name and
then after reading it do the same thing every time.

A simple way to associate a name with a function is a map that has string
as its key and the function pointer as its value:

typedef std::map<std::string,void(*)()> FuncMap;
FuncMap myfunctionmap;

You will need to have some kind of function to populate the map (and that
is where the string-pointer association will be resolved every time your
program runs):

void populate_map()
{
myfunctionmap["func1"] = func; // 'func' is a function elsewhere
myfunctionmap["more"] = anotherfunc; // and so on
}

Then later if you need to store somthing to a file, store the string part

void write_to_file(void (*f)())
{
// search the map for a value that is the same as 'f', find its key
// store the key
}

and when you read the string from a file, you make the association through
your map:

void (* read_from_file())()
{
std::string s;
// get the string from the file
FuncMap::iterator it = myfunctionmap.find(s);
return (it == myfunctionmap.end()) ? NULL : (*it).second;
}

And so on.

I haven't made sure the code works, it's more to give you the idea of just
one way to do it.

Victor
Jul 22 '05 #4
Nolan Martin wrote:
How would I make it so that the example above would work?
I need a way to return to the function across multiple sessions of the
program by reading and writing a "pointer" to the function from a file.


Why would you need such a thing? Just use the function name in every
session of the program.


Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #5
Nolan Martin wrote:
* Nolan Martin:
is a static functions address constant?
ie..

static void func();
write_to_file(&func);

Restart program...

static void func();
void (*funcPtr) ();
funcPtr = read_from_file();

funcPtr == &func


No.

It is constant during _one_ execution program.

Nothing more is guaranteed by the language, although on
particular systems some functions may or will often or always
end up on the same address -- e.g. crackers use knowledge
of such things to exploit buffer overruns.

How would I make it so that the example above would work?
I need a way to return to the function across multiple sessions of the
program by reading and writing a "pointer" to the function from a file.


Break up the "function" into smaller functions or _states_.
Look up state machines.
You could for example, save the current state information, and any
transition information, if relevant. The program then reads the
state information and set the program to enter that state.

Many game programs allow you to save the state enformation, so
that you can return to the function when the program is executed
again.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 22 '05 #6
> > > * Nolan Martin:
> is a static functions address constant?
> ie..
>
> static void func();
> write_to_file(&func);
>
> Restart program...
>
> static void func();
> void (*funcPtr) ();
> funcPtr = read_from_file();
>
> funcPtr == &func

No.

It is constant during _one_ execution program.

Nothing more is guaranteed by the language, although on
particular systems some functions may or will often or always
end up on the same address -- e.g. crackers use knowledge
of such things to exploit buffer overruns.

How would I make it so that the example above would work?
I need a way to return to the function across multiple sessions of the
program by reading and writing a "pointer" to the function from a file.


The "right" way would be to make up some kind of persistent identification
system, like a name that (a) is unique (b) can be associated with a

function and (c) can be written to file and read from it. Such name would have the
association with a function in your code, and you can store the name and
then after reading it do the same thing every time.

A simple way to associate a name with a function is a map that has string
as its key and the function pointer as its value:

typedef std::map<std::string,void(*)()> FuncMap;
FuncMap myfunctionmap;

You will need to have some kind of function to populate the map (and that
is where the string-pointer association will be resolved every time your
program runs):

void populate_map()
{
myfunctionmap["func1"] = func; // 'func' is a function elsewhere
myfunctionmap["more"] = anotherfunc; // and so on
}

Then later if you need to store somthing to a file, store the string part

void write_to_file(void (*f)())
{
// search the map for a value that is the same as 'f', find its key
// store the key
}

and when you read the string from a file, you make the association through
your map:

void (* read_from_file())()
{
std::string s;
// get the string from the file
FuncMap::iterator it = myfunctionmap.find(s);
return (it == myfunctionmap.end()) ? NULL : (*it).second;
}

And so on.

I haven't made sure the code works, it's more to give you the idea of just
one way to do it.

Victor


If only it were that simple...
I am trying to streamline the process of serialization in my program by
storing the location of constructor helper functions (function that returns
"new someclass"), so that all you need to do to make a class serializeable
is give it a helper function and implement a serialize function. the
serialize function will write a "pointer" to the inheritied helper function
then the remaining data. This allows me to implement new classes without
having too much overhead code preparing the lookup table and dramatically
simplifys things.

Ideally this is how I wanted it:

BOOL isSaving; //global flag

class Foo
{
public:
int data;
void* foobar; //can be any class set up for serialization simmilar to
this one
Foo();
~Foo();
static Foo* createInstance() {return new foo;};
virtual /*note that this function is virtual*/ void serialize(FILE*
file;) {
if (::isSaving) { //serialization
write_to_file(&(foobar->createInstance), file);
foobar->serialize(file); //simmilar to this function
write_to_file(data, file);
} else { //deserialization
void* (*fPtr)() = read_from_file(file);
foobar = fPtr();
foobar->serialize(file);
read_from_file(data, file);
}
};
};

the only thing this relys on is that the functions address is allways the
same and that it uses the vtable to look up the serialize function during
deserialization...I wonder how the functions location is stored for a normal
function call, does the compiler not simply replace the name with the
functions address at compile time making the address constant?

int func(int a) { a++; };
x = func(10); //how does it know where to look for the function?
Jul 22 '05 #7
Nolan Martin wrote:


If only it were that simple...
I am trying to streamline the process of serialization in my program by
storing the location of constructor helper functions (function that returns
"new someclass"), so that all you need to do to make a class serializeable
is give it a helper function and implement a serialize function. the
serialize function will write a "pointer" to the inheritied helper function
then the remaining data. This allows me to implement new classes without
having too much overhead code preparing the lookup table and dramatically
simplifys things.
Bad idea.
At the moment you do some modifications to your program (and you will do this)
all addresses change. Go the way Victor has suggested.
[snip]

the only thing this relys on is that the functions address is allways the
same and that it uses the vtable to look up the serialize function during
deserialization...I wonder how the functions location is stored for a normal
function call, does the compiler not simply replace the name with the
functions address at compile time making the address constant?


Sure it does. But at the moment you insert eg. a variable somewhere or
so some bug fixes all addresses might change. This is exactly why
programming in assembler is much more productive then hacking opcodes
in hex. The translating program keeps track of where things are located
in memory. You pay for this convinience by not interfering or depending on
a particular memory layout.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #8

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

Similar topics

2
by: Rahul Joshi | last post by:
Hi, Is it possible to define static member functions that are 'const', i.e. they just read but do not modify the static data members of a class? Declaring functions like: class SomeClass {...
9
by: cppsks | last post by:
Taking the address of a static const resulted in a unresolved symbol. Why is that? Is the address assigned at load time? Thanks.
3
by: Sehcra | last post by:
Hi, I'm trying to figure out if what I'm doing makes any sense. I created a namespace that contains some functions as well as some constants. Because these variables are constant, I have no...
14
by: John Ratliff | last post by:
I'm trying to find out whether g++ has a bug or not. Wait, don't leave, it's a standard C++ question, I promise. This program will compile and link fine under mingw/g++ 3.4.2, but fails to link...
3
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
9
by: Christian Christmann | last post by:
Hi, I have problems to initialize a static struct. Here is the meaningful part of the code: int main() { int pA = -100; struct globalMixed4 {
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
6
by: asit | last post by:
Static variables can't be initialized with functions, becoz these are load time processes. Why this is so ?? What is a load time process ??
7
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.