Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C memcpy() a function

Is there any method to calculate size of a function? I have a pointer to a function and I have to copy entire function using memcpy. I have to malloc some space and know 3rd parameter of memcpy - size. I know that sizeof(function) doesn't work. Do you have any suggestions?

like image 429
sync Avatar asked Nov 11 '09 17:11

sync


People also ask

Is memcpy a string function?

This C string library function memcpy( ) copies n characters from the block of memory pointed by str1 to str2 . It returns a pointer to the block of memory or object where contents are copied. Note: The pointers are declared as void * so that they can be used for any data type.

What is the difference between memcpy () & Memmove () functions in C?

Answer: memcpy() function is is used to copy a specified number of bytes from one memory to another. memmove() function is used to copy a specified number of bytes from one memory to another or to overlap on same memory.

How is memcpy implemented in C?

Define your memcpy() function in C Programming Language To implement the memcpy() function, you must typecast the source address and the destination address to char*(1 byte). Once the typecasting is performed, now copy the contents from the source array to the destination address.

What are the parameters that need to be passed to memcpy () function?

Parameters. dest − This is pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*. src − This is pointer to the source of data to be copied, type-casted to a pointer of type void*. n − This is the number of bytes to be copied.


2 Answers

Functions are not first class objects in C. Which means they can't be passed to another function, they can't be returned from a function, and they can't be copied into another part of memory.

A function pointer though can satisfy all of this, and is a first class object. A function pointer is just a memory address and it usually has the same size as any other pointer on your machine.

like image 183
Brian R. Bondy Avatar answered Sep 23 '22 17:09

Brian R. Bondy


It doesn't directly answer your question, but you should not implement call-backs from kernel code to user-space.

Injecting code into kernel-space is not a great work-around either.

It's better to represent the user/kernel barrier like a inter-process barrier. Pass data, not code, back and forth between a well defined protocol through a char device. If you really need to pass code, just wrap it up in a kernel module. You can then dynamically load/unload it, just like a .so-based plugin system.

On a side note, at first I misread that you did want to pass memcpy() to the kernel. You have to remind that it is a very special function. It is defined in the C standard, quite simple, and of a quite broad scope, so it is a perfect target to be provided as a built-in by the compiler.

Just like strlen(), strcmp() and others in GCC.

That said, the fact that is a built-in does not impede you ability to take a pointer to it.

like image 35
Steve Schnepp Avatar answered Sep 25 '22 17:09

Steve Schnepp