How to use Function Pointer into Memory?

B

Bryan Parkoff

I have written over 4096 functions for my project. I would like to
create one Function Pointer so Function Pointer will contain 4096 functions.
I do not want Function Pointer to be stored in the static memory so I am
trying to think how to tell Function Pointer to be stored in the dyramic
memory. The problem is that C/C++ Compiler can't handle source code more
than 64K limit so it has to create smaller sub-source codes to avoid 64K
limit. After linking, all sub-source codes will be together into one
executable file.
Static memory will not fit in one source code because of 64K limit.
Dyramic memory is only the option. Look at my example below.

unsigned int* pFunc = new unsigned int[4096];

void (*p_Func[4096])(void);

p_Func = pFunc;
p_Func[0] = Func0;
p_Func[1] = Func1;
p_Func[2] = Func2;
...
...
...
p_Func[4094] = Func4094;
p_Func[4095] = Func4095;
p_Func[4096] = Func4096;

You may notice strange because "void (*p_Func[4096])(void) = new void
(*p_Func)(void)" do not work because Function Pointer can't use new keyword.
There may be another way. "unsigned int* pFunc = new unsigned int[4096];"
has to be done first to create dyramic memory. Then "p_Func = pFunc;" is to
copy memory address from pFunc to p_Func before I can be able to add
thousand functions to p_Func that is linked to pFunc.
I do not intend to declare and define "void (*p_Func[...])(void) = {
...., ..., ... } because it is only constant because of source code 64K
limit. I intend to modify function names in the Function Pointer listing.
Do you know what I mean?
Maybe reinterpret_cast may be the option, but it has to find other way
how to declare and define Function Pointer using new keyword. Please
advise. Thanks...

Bryan Parkoff
 
C

clemd

Bryan said:
You may notice strange because "void (*p_Func[4096])(void) = new void
(*p_Func)(void)" do not work because Function Pointer can't use new
keyword.

You *can* allocate function pointers with "new." Here's an example.

void f0() {}
void f1() {}

int main()
{
typedef void (*PtrFunc)();
PtrFunc *pf = new PtrFunc[4096];
pf[0] = f0
pf[1] = f1;
return 0;
}
 
B

Bryan Parkoff

Hello,

It works what I wanted. Thank you very much for the information. Can
you please explain what do typedef keyword mean?
If typedef keyword is not used, how can Function Pointer with new
keyword be used? It looks like that it is unreadable, but please give me a
try.

Bryan Parkoff

Bryan said:
You may notice strange because "void (*p_Func[4096])(void) = new void
(*p_Func)(void)" do not work because Function Pointer can't use new
keyword.

You *can* allocate function pointers with "new." Here's an example.

void f0() {}
void f1() {}

int main()
{
typedef void (*PtrFunc)();
PtrFunc *pf = new PtrFunc[4096];
pf[0] = f0
pf[1] = f1;
return 0;
}
 
J

Jesper Madsen

Bryan Parkoff said:
Hello,

It works what I wanted. Thank you very much for the information. Can
you please explain what do typedef keyword mean?
If typedef keyword is not used, how can Function Pointer with new
keyword be used? It looks like that it is unreadable, but please give me a
try.

Bryan Parkoff

Bryan said:
You may notice strange because "void (*p_Func[4096])(void) = new void
(*p_Func)(void)" do not work because Function Pointer can't use new
keyword.

You *can* allocate function pointers with "new." Here's an example.

void f0() {}
void f1() {}

int main()
{
typedef void (*PtrFunc)();
PtrFunc *pf = new PtrFunc[4096];
pf[0] = f0
pf[1] = f1;
return 0;
}

AFAIK: The typedef keyword can be read/understood as "define type" ,"in
short hand", or "type named as"..
An example:

#include <string>
#include <map>

typedef void (*Function0Param)();

typedef std::map<std::string,Function0Param> funcNameToFunc;

funcNameToFunc& getFuncNameToFunc(){
static funcNameToFunc funcs;
return funcs;
}

void callFunctionByName(const std::string& funcName) {
Function0Param func = getFuncNameToFunc()[funcName];
if (func)
func();
else
throw("Unknown function");
}
 
D

David White

Bryan Parkoff said:
Hello,

It works what I wanted. Thank you very much for the information. Can
you please explain what do typedef keyword mean?

It is used to define a type rather than an object of a type. Take out the
typedef and just pretend that you are defining a variable, e.g.,
void (*VFPtr)(); // a function ptr called 'VFPtr'

Now, if you place 'typedef' in front of this, 'VFPtr' becomes the type
rather than a variable of the type. You can then use this type name instead
of the full type:
type void (*VFPtr)();
VFPtr *p_Func = new VFPtr[4096];

As long you know how to define a variable of a given type, creating a
typedef for the type is just as easy.
If typedef keyword is not used, how can Function Pointer with new
keyword be used? It looks like that it is unreadable, but please give me a
try.

The syntax of 'new' expressions for anything but simple types and pointers
is an area that I've never understood well. I don't know what language or
grammar objections there are to just sticking 'new' in front of any type,
but it doesn't seem to work that way. My guess is that function pointers
cannot be allocated with 'new' without a typedef, but perhaps someone else
will confirm or correct this.

DW
 
J

Jonathan Mcdougall

David White said:
It is used to define a type rather than an object of a type. Take out the
typedef and just pretend that you are defining a variable, e.g.,
void (*VFPtr)(); // a function ptr called 'VFPtr'

Now, if you place 'typedef' in front of this, 'VFPtr' becomes the type
rather than a variable of the type. You can then use this type name
instead
of the full type:
type void (*VFPtr)();

That should be

typedef void (*VFPtr)();
VFPtr *p_Func = new VFPtr[4096];

As long you know how to define a variable of a given type, creating a
typedef for the type is just as easy.
If typedef keyword is not used, how can Function Pointer with new
keyword be used? It looks like that it is unreadable, but please give me a
try.

The syntax of 'new' expressions for anything but simple types and pointers
is an area that I've never understood well. I don't know what language or
grammar objections there are to just sticking 'new' in front of any type,
but it doesn't seem to work that way.

What error do you get?
My guess is that function pointers
cannot be allocated with 'new' without a typedef, but perhaps someone else
will confirm or correct this.

You can allocate functions pointers on the heap without a typedef

void (**pf)() = new (void (*)());

The thing is, I can't seem to make it work for arrays.

void (**pf)() = new (void (*)())[4096];

fails to compile, as if the compiler (Comeau) thinks [4096] is the
operator[] applied to the result of new. That's probably an ambiguity in
the statement, though I don't know exactly why (except that it is quite an
obscure syntax). Using a templated function or a typedef works. imho, it
is not a bad thing that such code is invalid.


Jonathan
 
D

David White

Jonathan Mcdougall said:
David White said:
Now, if you place 'typedef' in front of this, 'VFPtr' becomes the type
rather than a variable of the type. You can then use this type name
instead
of the full type:
type void (*VFPtr)();

That should be

typedef void (*VFPtr)();
Oops.
VFPtr *p_Func = new VFPtr[4096];

As long you know how to define a variable of a given type, creating a
typedef for the type is just as easy.
If typedef keyword is not used, how can Function Pointer with new
keyword be used? It looks like that it is unreadable, but please give
me
a

The syntax of 'new' expressions for anything but simple types and pointers
is an area that I've never understood well. I don't know what language or
grammar objections there are to just sticking 'new' in front of any type,
but it doesn't seem to work that way.

What error do you get?

I get a syntax error for the following:
void (**fptrs)() = new void (*[4096])();
My guess is that function pointers
cannot be allocated with 'new' without a typedef, but perhaps someone else
will confirm or correct this.

You can allocate functions pointers on the heap without a typedef

void (**pf)() = new (void (*)());

The thing is, I can't seem to make it work for arrays.

void (**pf)() = new (void (*)())[4096];

This is what I mean about the strange syntax, if your version is correct.
Why should the square brackets be moved from their correct position? The
equivalent built-in array doesn't look like that.

DW
 
M

Mike Wahler

Jonathan Mcdougall said:
David White said:
It is used to define a type rather than an object of a type. Take out the
typedef and just pretend that you are defining a variable, e.g.,
void (*VFPtr)(); // a function ptr called 'VFPtr'

Now, if you place 'typedef' in front of this, 'VFPtr' becomes the type
rather than a variable of the type. You can then use this type name
instead
of the full type:
type void (*VFPtr)();

That should be

typedef void (*VFPtr)();
VFPtr *p_Func = new VFPtr[4096];

As long you know how to define a variable of a given type, creating a
typedef for the type is just as easy.
If typedef keyword is not used, how can Function Pointer with new
keyword be used? It looks like that it is unreadable, but please give
me
a

The syntax of 'new' expressions for anything but simple types and pointers
is an area that I've never understood well. I don't know what language or
grammar objections there are to just sticking 'new' in front of any type,
but it doesn't seem to work that way.

What error do you get?
My guess is that function pointers
cannot be allocated with 'new' without a typedef, but perhaps someone else
will confirm or correct this.

You can allocate functions pointers on the heap without a typedef

void (**pf)() = new (void (*)());

The thing is, I can't seem to make it work for arrays.

void (**pf)() = new (void (*)())[4096];

fails to compile, as if the compiler (Comeau) thinks [4096] is the
operator[] applied to the result of new. That's probably an ambiguity in
the statement, though I don't know exactly why (except that it is quite an
obscure syntax). Using a templated function or a typedef works. imho, it
is not a bad thing that such code is invalid.

#include <iostream>

int f0() { return 0; }
int f1() { return 1; }
int f2() { return 2; }

int main()
{
int (**pf)() = new (int (*[3])()) ;
pf[0] = f0;
pf[1] = f1;
pf[2] = f2;

int i = 0;

for(i = 0; i < 3; ++i)
std::cout << pf() << '\n';

delete[] pf;
return 0;
}


-Mike
 
J

Jonathan Mcdougall

Mike said:
Jonathan Mcdougall said:
You can allocate functions pointers on the heap without a typedef

void (**pf)() = new (void (*)());

The thing is, I can't seem to make it work for arrays.

void (**pf)() = new (void (*)())[4096];

fails to compile, as if the compiler (Comeau) thinks [4096] is the
operator[] applied to the result of new. That's probably an ambiguity in
the statement, though I don't know exactly why (except that it is quite an
obscure syntax). Using a templated function or a typedef works. imho, it
is not a bad thing that such code is invalid.

#include <iostream>

int f0() { return 0; }
int f1() { return 1; }
int f2() { return 2; }

int main()
{
int (**pf)() = new (int (*[3])()) ;
pf[0] = f0;
pf[1] = f1;
pf[2] = f2;

int i = 0;

for(i = 0; i < 3; ++i)
std::cout << pf() << '\n';

delete[] pf;
return 0;
}


Of course :)


Jonathan
 

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,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top