小编Lor*_*tti的帖子

结构的 malloc 上的分段错误(核心转储)

我有一个由线路引起的分段错误错误

*head = malloc(sizeof(struct node)+1);'
Run Code Online (Sandbox Code Playgroud)

我很确定我在其他情况下以相同的方式使用了构造节点和 malloc,一切都工作正常。程序在此处打印1,然后发生核心转储。

这是我的代码:

struct node {
    //int val ;
    struct node * next;
    unsigned char string[];
 } ;

void init_list(struct node ** head) {
    
    printf("here1 \n");
    fflush(stdout);
    *head = malloc(sizeof(struct node)+1);
    printf("here2\n"); 
    fflush(stdout); 
    if(!(*head)){
    
            printf("error malloc \n");
            fflush(stdout);
            return ;
    }
    //(*head) -> val = -1; 
    (*head) -> next = NULL;
    ((*head) -> string)[0]= '\0'; 
    return ;

  }

  int main(void) {
     struct node ** head;
     init_list(head) ;
     printf("hereee\n");
     fflush(stdout) ;
     fini_list(head);
     return 1;
  }
Run Code Online (Sandbox Code Playgroud)

这是 …

c

0
推荐指数
1
解决办法
258
查看次数

你好,我有一个多线程程序,每个线程都必须在 ThreadLocal 变量中存储一个值,但是第二个线程覆盖了 1 的内容

我正在尝试编写这个多线程程序,但出了问题。我有一个 HelloApplication 类,我在那里创建了两个新线程。

run 方法位于另一个名为 ThreadLocalSession 的类中。在那里,我需要存储两个不同的值,每个线程使用 ThreadLocal 变量存储一个值。这种方法失败了,因为第二个线程覆盖了第一个线程写入的值。

由于这个原因,我尝试打印线程 id,我注意到相同的线程 ID 和线程名称被打印了两次......这怎么可能?

这是 HelloApplication 类:

public class HelloApplication extends Application {

@Override
public void start(Stage stage) throws IOException {
    ThreadLocalSession firstUser = new ThreadLocalSession(1);
    ThreadLocalSession secondUser = new ThreadLocalSession(2);
    new Thread(firstUser).start();
    new Thread(secondUser).start();
}

public static void main(String[] args) {
    launch();
}
}
Run Code Online (Sandbox Code Playgroud)

这是 ThreadLocal 会话类:

public class ThreadLocalSession implements Runnable{

private static final ThreadLocal<Session> usersession= new ThreadLocal<Session>();
private Integer tr_id;

public ThreadLocalSession(int i) {
    this.tr_id =i;
}


@Override …
Run Code Online (Sandbox Code Playgroud)

java multithreading javafx

-3
推荐指数
1
解决办法
86
查看次数

标签 统计

c ×1

java ×1

javafx ×1

multithreading ×1