
[英]How do I pass and manipulate the arguments of a matrix into the function using pointers?
[英]How to pass arguments to function pointers
我正在使用c中的函数指针,因为我的自定义API库需要回调机制。 总结一个简单的例子:
*userfunction*(SY_msg msg)
{
/* do something */
};
SY_msg的大小为1024个字节。 因此,堆栈中有1024个字节。
指向userfuncion()的指针作为calback_wrapper []中的第一个元素。
here is an example of use:
// (...) some code
SY_msg* msg;
msg = SYmalloc (sizeof(SY_msg)); /* it takes 1024 bytes in heap */
// (...) some code
calback_wrapper[0] (*msg); /* 1204 are passed by value */
/* during userfunction() execution , 1024 unused bytes are present in the heap */
free (msg); /* now finally heap is free */
// (...) some code
但我想拥有以下内容:
// (...) some code
SY_msg* msg;
msg = SYmalloc (sizeof(SY_msg)); /* it takes 1024 bytes in heap */
// (...) some code
memcpy(someplace,msg,sizeof(SY_msg); /* where "someplace" is a point in the stack referred by the argument of userfunction() */
free (msg); /* heap is free */
calback_wrapper[0] (*someplace); /* is starts userfunction() execution */
// (...) some code
有可能找到“某个地方”的地址吗? 我的编译器是gcc。
你干什么
// (...) some code
SY_msg msg, * pmsg;
pmsg = SYmalloc (sizeof(SY_msg)); /* it takes 1024 bytes in heap */
// (...) some code using pmsg instead of msg
memcpy(&msg, pmsg, sizeof(SY_msg)); /* where "someplace" is a point in the stack referred by the argument of userfunction() */
free (pmsg); /* heap is free */
calback_wrapper[0] (msg); /* is starts userfunction() execution */
// (...) some code
在上面的示例中,您可以替换
memcpy(&msg, pmsg, sizeof(SY_msg));
通过
msg = *pmsg;
我的问题中有错误的假设。 用户function()的参数在函数调用之后立即分配到堆栈中。 也许某种“上下文”可以解决这个问题。 例:
但无论如何,都要求提供汇编代码段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.