C ++中带有指针的分段错误 [英] Segmentation Fault with Pointers in C++

查看:82
本文介绍了C ++中带有指针的分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建结构的对象,但是在分配值时遇到了分段错误.构建对象后,它将通过指针传递到列表中.这是我的实现:

I am trying to build an object of a struct but am getting a segmentation fault while assigning the values. After I build the object it will be passed by pointer into a list. Here is my implementation:

struct clientInfo {
  int client, a, b;
};
List *info=new List();

void thread(int which) {
  clientInfo *cI;
  cI->client=which;
  cI->a=4;
  cI->b=5;
  info->Append(cI);
}

在执行'cI-> client =哪个'期间,产生了分段错误.该项目正在nachos平台上为任何熟悉的人编写,但是List的定义与C ++中的任何链接列表非常相似.对于任何不熟悉玉米片的人,空线程"是我的"int main".

During the execution of 'cI->client=which' a segmentation fault is produced. This project is being writting on the nachos platform for anyone that is familiar, however the definition of List is much the same as any linked list in C++. For anyone not familiar with nachos, 'void thread' is my 'int main'.

谢谢.

推荐答案

您必须创建clientInfo *cI,例如

clientInfo *cI = new clientInfo();

这篇关于C ++中带有指针的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆