我正在编译这个程序。

char * p;
void * handler(void * arg)
{
  p = "hello"; // initializing p
  puts(p);
  puts("------");
  long tid = (long) arg;
  printf("Hello wrold! it is in thread : %ld\n", tid);
  pthread_exit(NULL); //exiting thread
}

int main() {
  pthread_t t_id[2];
  int rc;
  long t;
  p = malloc(sizeof(100)); // allocating memory tried (p =malloc(sizeof(char)*100);)
  if (p == NULL) {
    perror("malloc");
    exit(EXIT_FAILURE);
  }


  for (t = 0; t < 2; t++) {
    printf("Creating Thread  %ld\n", t);
    rc = pthread_create( & t_id[t], NULL, handler, (void * ) t);
    if (rc) {
      perror("pthread_create");
      exit(EXIT_FAILURE);
    }
  }
  pthread_join(t_id[0], NULL);
  pthread_join(t_id[1], NULL);
  free(p); /// segmentation fault here...
  puts("***");
  //      pthread_exit(NULL);

}

我这里有分割错误,
我用gdb检查过。
令人惊讶的是,p的地址正在改变。为什么?
任何帮助之手将不胜感激!
使用主机libthread_db library“/lib/x86_64-linux-gnu/libthread_db.so.1”。
Temporary breakpoint 1, main () at thread_heap.c:27
27              p=malloc(sizeof(100));
(gdb) p p
*$1 = 0x0*    **/// adress of p**
(gdb) next
28              if(p==NULL)
(gdb) p p
*$2 = 0x602010 ""*  **// why changed here????**
(gdb) next
35              for (t=0;t<2;t++)
(gdb) p p
$3 = 0x602010 ""
(gdb) next
37                      printf("Creating Thread  %ld\n",t);
(gdb)
Creating Thread  0
38                      rc=pthread_create(&t_id[t],NULL,handler,(void *)t);
(gdb)
[New Thread 0x7ffff77fe700 (LWP 21632)]
.
.
.

114     in pthread_join.c
(gdb)
main () at thread_heap.c:49
49      free(p);
(gdb) p p
*$4 = 0x4009e0 "hello"*   **/// again why changed here?**
(gdb)
$5 = 0x4009e0 "hello"
(gdb) step
__GI___libc_free (mem=0x4009e0) at malloc.c:2959
2959    malloc.c: No such file or directory.
(gdb) q

Program received signal SIGSEGV, Segmentation fault.
_int_free (av=0x7ffff7bb6720, p=0x4009d0, have_lock=0) at malloc.c:4098
4098    malloc.c: No such file or directory.   // Why this address changed?
(gdb)

最佳答案

问题出在

// in main
p = malloc(sizeof(100)); // allocating memory tried (p =malloc(sizeof(char)*100);)

// in handler function
p = "hello"; // initializing p

这里p是一个全局变量,您以前在上面使用过malloc。在handler函数中,您将p分配给字符串文本的地址,导致指针悬空。当您尝试释放这个修改过的p时,会得到一个错误。
你应该做的是
const char *hell = "hello";
strcpy(p,hell);

另外,将malloc修复为以前的版本。当前代码只分配4个字节。(sizeof (100)sizeof(int)相同)

关于c - 线程更改堆内存,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41583401/

10-09 05:56