473,468 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to use Function Pointer into Memory?

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
8 2046
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
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

"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
"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

"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
"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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
7
by: Bryan Parkoff | last post by:
C/C++ Compiler encourages to limit 1,000 functions under function pointer, but I am allowed to overlimit 4,096 functions. I am just for fun to test how it works. The problem is that source code...
5
by: wongjoekmeu | last post by:
Hiya all, I am trying to use function pointers. I know that usually if you use a pointer to an object, you have to release the memory at the end by calling the delete keyword. I was wondering if...
57
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
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...
1
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.