You serious?stanko51 wrote:delete the function pointer
It doesn't - read again.On the first thought RCU would deny access to the background thread because the function is being read, which i never want to happen
Come on, its not that hard.
You serious?stanko51 wrote:delete the function pointer
It doesn't - read again.On the first thought RCU would deny access to the background thread because the function is being read, which i never want to happen
If the number of functions in this FCT is small, you could link each function into a separate section in your object/executable file (or have it loaded into a separate section in virtual memory), and you could use the length of the section instead. This could solve problems with switch() statements which need additional data to work correctly (just link any additional constants into the same section).stanko51 wrote:but it couldn't delete/modify the function in memory on which the pointers would point to.
So if you just delete the function pointer you have an memory leak with the funtion still in memory and nothing pointing on it anymore.
btw, The table of function pointers already exist, it's the FCT table i talked about earlier.
stanko51 wrote:but it couldn't delete/modify the function in memory on which the pointers would point to.
So if you just delete the function pointer you have an memory leak with the funtion still in memory and nothing pointing on it anymore.
There are more problems:stanko51 wrote:Thank you for your answers.
but then i don't get how are the instructions stored in memory ? Is it not from an adress and then read the following memory?
If for example i have a RunFunction(&example); it will get the function from address &example copy it and execute it.
My problem is basically when i do memcpy(&example, dest, lenght); how can i know what is the value of lenght?
For a small function i just assume the lenght < 1000. So i do memcpy(&example, dest, 1000); and RunFunction work fine. It just copies 1000 bytes from the address &example. But Just setting a random big value is not really good programming method. Does anyone have an idea on how i could estimate this value ?
Thanks
Yep; 6) has been jumping up and down in the back in my head trying to get attention, but somehow I looked the other way. 5) is the "NX flag" thingy most OS designers get so excited about because it's a great way to make life difficult for malware...skyking wrote: 5) Even if it succeeds it's possible that the memory copied to does not permit execution.
6) Even if it does the code may be position dependent.
It seems everything has gotten a little offtopic.. but I do not see why you can not just use a function table for this kind of thing. The only easy way to get the size of a function is to use pure assembly.. but if you really must get it from C then there is this hackish idea I have..stanko51 wrote:Hello all,
This is just a question about general fonctionnality of C language.
How can i know the size that the code of a function use in memory.
For example :Is there something like a sizeof(example) that will give me the memory space needed by the function example.Code: Select all
int example (void){ int i = 0; print(i); return 1; }
Thanks
Code: Select all
int example(void){
int i=0;
printf(i);
return 1;
__asm volatile("_example_end:\n"); //depending on compiler, you may not want the leading _
}
int example_size=example_end-example+MAGIC_NUMBER;
Sorry to burst your bubble, you can't DELETE a function pointer, because when using the 'delete' keyword on a pointer, it will free the memory allocated by the variable it points to. With function pointers, you can't delete the functions stored in them, you CAN redirect the function pointer to another function however, but this can easily be stopped with:stanko51 wrote:but it couldn't delete/modify the function in memory on which the pointers would point to.
So if you just delete the function pointer you have an memory leak with the funtion still in memory and nothing pointing on it anymore.
btw, The table of function pointers already exist, it's the FCT table i talked about earlier.
Code: Select all
void foo() {}
void bar() {}
//...
//Normal function pointer:
void (*p1) () = &foo;
p1 = &bar; //Will compile successfully.
//Function pointer that can't be modified:
void (*const p2) () = &foo;
p2 = &bar; //Will fail to compile, because the pointer can't be modified.
Code: Select all
typedef void (funcPtr*)(int param);
funcPtr array[] = {func1, func2, func3}
for(int i = 0; i < numFuncs; i++)
array[i](param);
// change 3rd function:
array[2] = funcX;
// do more stuff without slow copying and hacky size finding methods...
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
Code: Select all
typedef void (*func)(int param);
...
func foo;
void bar(int p){
}
...
foo=bar;
Don't you meanearlz wrote:Code: Select all
typedef void (*func)(int param); ... func foo; void bar(int p){ } ... foo=bar;
Code: Select all
foo = &bar;
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
func==&func... I mean, how would a compiler handle dereferencing a function pointer?Creature wrote:Don't you meanearlz wrote:Code: Select all
typedef void (*func)(int param); ... func foo; void bar(int p){ } ... foo=bar;
or am I missing something?Code: Select all
foo = &bar;