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

How to use Function Pointer into Memory?

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


P: n/a
Bryan Parkoff wrote:
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;
}

Jul 22 '05 #2

P: n/a
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

<cl***@acm.org> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Bryan Parkoff wrote:
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;
}

Jul 22 '05 #3

P: n/a

"Bryan Parkoff" <no****@nospam.com> wrote in message
news:_a*****************@newssvr30.news.prodigy.co m...
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

<cl***@acm.org> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Bryan Parkoff wrote:
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");
}

Jul 22 '05 #4

P: n/a
"Bryan Parkoff" <no****@nospam.com> wrote in message
news:_a*****************@newssvr30.news.prodigy.co m...
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

Jul 22 '05 #5

P: n/a

"David White" <no@email.provided> a écrit dans le message de news:
CE*****************@nasal.pacific.net.au...
"Bryan Parkoff" <no****@nospam.com> wrote in message
news:_a*****************@newssvr30.news.prodigy.co m...
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)();


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
Jul 22 '05 #6

P: n/a
"Jonathan Mcdougall" <jo***************@DELyahoo.ca> wrote in message
news:fx*********************@wagner.videotron.net. ..

"David White" <no@email.provided> a écrit dans le message de news:
CE*****************@nasal.pacific.net.au...
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
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?


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

Jul 22 '05 #7

P: n/a

"Jonathan Mcdougall" <jo***************@DELyahoo.ca> wrote in message
news:fx*********************@wagner.videotron.net. ..

"David White" <no@email.provided> a écrit dans le message de news:
CE*****************@nasal.pacific.net.au...
"Bryan Parkoff" <no****@nospam.com> wrote in message
news:_a*****************@newssvr30.news.prodigy.co m...
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)();


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.


#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[i]() << '\n';

delete[] pf;
return 0;
}
-Mike
Jul 22 '05 #8

P: n/a
Mike Wahler wrote:
"Jonathan Mcdougall" <jo***************@DELyahoo.ca> wrote in message
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[i]() << '\n';

delete[] pf;
return 0;
}


Of course :)
Jonathan
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.