Segmentation Fault with Pointers in C++(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=which' 期间,会产生分段错误.这个项目是在 nachos 平台上为任何熟悉的人编写的,但是 List 的定义与 C++ 中的任何链表大致相同.对于不熟悉玉米片的人来说,'void thread' 是我的 '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++ 中指针的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 中指针的分段错误
基础教程推荐
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
