简体   繁体   English

使用线程调试分段错误

[英]debugging segmentation fault using threads

When debugging my c program I get this error message 调试我的C程序时,出现此错误消息

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff77f6700 (LWP 14945)]
__strcpy_sse2_unaligned ()
at ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:532
532 ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S: Aucun fichier ou dossier de ce type.

I think that it's "useless" to post my code because it doesn't precise the error line. 我认为发布我的代码是“无用的”,因为它无法精确显示错误行。

But I know I think it cames from this function: 但我知道我认为它来自此功能:

void add_user(tlistU *lu, user * u,tuser *new)
{

      tuser *nouveau = malloc(sizeof(tuser));

      u =malloc(sizeof(*u));        
      if (lu == NULL || nouveau == NULL)
      {
        exit(EXIT_FAILURE);
      }
   strcpy(nouveau->users->nickname,u->nickname);
   nouveau->next=lu->first;
   lu->first=nouveau;
}

( nickname is a 9 char string ) (昵称是一个9个字符的字符串)

Your code is a little strange. 您的代码有点奇怪。 The cause of the crash is probably this, however: 但是,崩溃的原因可能是:

strcpy(nouveau->users->nickname,u->nickname);

Note that nouveau is a block of uninitialised memory that you just requested. 请注意, nouveau是您刚刚请求的未初始化内存的块。 You are then accessing nouveau->users->nickname , which assumes that nouveau->users is a valid pointer. 然后,您将访问nouveau->users->nickname ,它假定nouveau->users是有效的指针。

The other thing that's happening is overwriting u here: 这是发生的另一件事是重写u在这里:

u = malloc(sizeof(*u));

I suspect that what you actually meant to do was something like this: 我怀疑您的实际意图是这样的:

nouveau->users = malloc(sizeof(*u));

That would solve both issues. 这将解决两个问题。

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

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