简体   繁体   English

将结构指针传递给函数段C FREEBSD

[英]passing a struct pointer to a function segfault C FREEBSD

I am trying to port my C GNU/Linux code to FreeBSD. 我试图将我的C GNU / Linux代码移植到FreeBSD。 At first I thought it wouldn't compile and act abnormally, but it didn't act up on me because it didn't use functions that the other OS does not have native to it. 起初,我认为它不会编译并异常运行,但是它并没有对我起作用,因为它没有使用其他操作系统没有的本机功能。 Although it compiles fine (no errors or warnings using -Wall ) the application keeps seg faulting on a line that works normally as intended on a GNU/Linux install. 尽管它可以正常编译(使用-Wall不会出现错误或警告),但该应用程序会在正常运行GNU / Linux安装的行上保持段错误。

What I am doing is creating a pointer to a struct and then passing the pointer to a function as a void pointer and then recreating it inside the function. 什么我做的是创建一个指向一个结构,然后使指针功能空指针,然后重新创建它里面的功能。

ex: 例如:

typedef struct
{
   int i;
}some_struct;

int main()
{
   some_struct *test = malloc(sizeof(some_struct));
   test->i = -1;
   function(test);

return 0;
}

void *function(void *prarm)
{
   some_struct test = *((some_struct *)param);  //segfaults on this line.
   free(param);


return NULL;
}

On my GNU/Linux install this would allow me to recreate the struct with the passed pointer data locally inside the function and allow me to free the malloc ed memory from main() but on FreeBSD it seg faults and I do not know why. 在我的GNU / Linux安装上,这将允许我在函数内部局部地使用传递的指针数据重新创建结构,并允许我从main()释放malloc内存,但是在FreeBSD上它会出现段错误,我不知道为什么。

If I break at function in gdb and type 如果我在gdb中的function中断并输入

p *(some_struct *)param

It successfully prints out my command struct that is created from the pointer and all its variables from inside the function. 它成功打印了我从指针创建的命令结构以及从函数内部创建的所有变量。

Im at a lost at why this is working GNU/Linux and seg faulting on my FreeBSD test machine. 我不知道为什么它能正常工作GNU / Linux并在FreeBSD测试机上出现段错误。

Thanks for any help towards this problem I am having. 感谢您对我遇到的这个问题的帮助。

It's weird that this would fail. 这将失败是很奇怪的。 Have you tried re-ordering the functions or declaring function before use (before main): 您是否尝试过在使用前(在main之前)重新排序功能或声明function

void *function(void *prarm);

You are creating a very large stack frame: 您正在创建一个非常大的堆栈框架:

char buff[3000600], data[3000000], url[1024], c[1];

That's almost 6MB - perhaps you are exceeding the default process stack size limit on FreeBSD? 将近6MB-也许您超出了FreeBSD上默认的进程堆栈大小限制? FreeBSD uses SIGSEGV to kill a process that exceeds this limit, and it would be detected when you write to a local variable that would cause the stack to be extended beyond the limit. FreeBSD使用SIGSEGV杀死超过此限制的进程,并且在您写入导致堆栈扩展超出该限制的局部变量时将检测到该进程。 You can tweak the stack size limit in login.conf . 您可以在login.conf调整堆栈大小限制。

I don't see any reason for it to segfault, especially if -Wall is silent. 我看不出有任何原因导致段错误,特别是在-Wall保持沉默的情况下。

One thing that bothers me about this is that there is no apparent declaration in effect for function() at the point of the call in main() . 让我感到困扰的一件事是,在main()的调用点上没有明显的声明对function() Without a declaration, C assumes the parameter is passed as an integer, and so should give a warning. 如果没有声明,C会假定参数以整数形式传递,因此应给出警告。 You could fix this in several ways—add a function declaration above main(), move the function definition above main, or put the declaration in a header file included before main(). 您可以通过几种方式解决此问题:在main()上方添加函数声明,将函数定义移至main之上,或将声明放入main()之前的头文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM