简体   繁体   中英

debugging segmentation fault using threads

When debugging my c program I get this error message

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 )

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. You are then accessing nouveau->users->nickname , which assumes that nouveau->users is a valid pointer.

The other thing that's happening is overwriting u here:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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