memcpy() 函数是可重入的吗?
我在信号处理程序中调用一些 C++ 函数,并且我的程序因分段错误而终止。 当我检查 gdb 时,memcpy() 函数是我得到 SIGSEGV 的地方。 我想知道 memcpy() 是否是可重入函数?
I call some C++ functions inside a signal handler and my program is terminated by segmentation fault.
When I check with gdb, memcpy() function is where i get SIGSEGV.
I would like to know if memcpy() is a reentrant function or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在除了高度嵌入式平台之外的所有平台中,它都是可重入的。你提到了 SIGSEGV 所以我认为它不是其中之一。在这种情况下,memcpy() 很可能不是罪魁祸首:这是调用者的错误。如果你要求 memcpy() 复制错误的指针(或错误的长度),那么它就会出错。你可以很容易地做到这一点:
这将导致 SIGSEGV 并且它会告诉你 memcpy() 引起的。当然,这不是 memcpy 的错——它只是按照你告诉它的去做。您的信号处理程序正在用一些奇怪的东西调用它。对调用者站点的回溯(在 gdb 或您拥有的任何工具中)应该显示您调用它的内容。如果做不到这一点,只需 printf 您传递给 memcpy 的参数即可。
In all but the most highly embedded platforms, it'll be reentrant. You mention SIGSEGV so I assume it's not one of those. In this case it's most likely memcpy() isn't the culprit: it's the caller's fault. If you ask memcpy() to copy bad pointers (or bad length) then it'll be the one which faults. You could easily do this:
That'll cause a SIGSEGV and it'll tell you memcpy() caused it. Of course, it's not memcpy's fault - it's just doing what you told it. Your signal handler is calling it with something odd. A backtrace (in gdb or whatever tool you have) to the site of the caller should show what you called it with. Failing that, just printf the arguments you're passing to memcpy.
有关(非)可重入函数和信号处理程序(就 GNU C 库而言)的一些相关信息可以在 http://www.gnu.org/s/libc/manual/html_node/Nonreentrancy.html#Nonreentrancy:
这部分似乎与您的问题特别相关:
“只要值突然变化,在处理程序可能运行的任何时候都不会干扰任何内容,仅仅写入内存对象是安全的。”
Some relevant information concerning (non)reentrant functions and signal handlers (as far as relevant to the GNU C library) can be found at http://www.gnu.org/s/libc/manual/html_node/Nonreentrancy.html#Nonreentrancy:
This part seems especially relevant to your question:
"Merely reading from a memory object is safe provided that you can deal with any of the values that might appear in the object at a time when the signal can be delivered. Keep in mind that assignment to some data types requires more than one instruction, which means that the handler could run “in the middle of” an assignment to the variable if its type is not atomic."
"Merely writing into a memory object is safe as long as a sudden change in the value, at any time when the handler might run, will not disturb anything."
我不明白为什么它不能重入。我不确定,但我认为这很大程度上取决于您使用的库。
I don't see why it could not be reentrant. I'm not sure, but i think its a lot based on the libraries you use.
除非 memcpy 实现得很糟糕,否则它是可重入的。它仅适用于您提供的内容 - 指针和长度值。所有参数均按值传递,因此一旦激活函数,无论信号和/或其他线程如何,这些值都不会在其堆栈帧上更改。
Unless
memcpy
is BADLY implemented, it is reentrant. It will only work with what you give it - the pointers and the length value. All parameters are passed by value, so once the function has been activated, these values will not change on its stack frame, regardless of signals and/or other threads.我认为问题在于您使用无效(或已删除)的指针作为 memcpy 函数的参数,请仔细检查您的代码。
问候。
I think that the problem is that you are using as parameters for the memcpy function a invalid (or deleted) pointer, please check your code carefully.
Regards.