【发布时间】:2015-12-03 11:52:35
【问题描述】:
我想创建一个结构指针数组并将每个指针设置为空。最终我想让数组中的指针指向我的结构。我相信我有为此编写代码,但我一直遇到段错误。
代码:
//struct in my .h
struct bin{
double cac; // current available capacity
struct node *list //pointer to linked list... bin struct points to linked list struct
}
//main file
void main(){
struct bin *bArray[20];
struct bin *binTemp, *pTemp;
for(i=0;i<20;i++) bArray[i]= NULL;
}
我以为这会创建一个 bin 指针数组,但我在这里遇到了段错误。不管它们是什么类型的指针,我不应该让所有指针都为 NULL 吗?
最终我想让这些都指向 bin 结构,我想我可以做到这一点而不必先将指针设为 NULL,所以我尝试了:
for(i=0;i<20;i++){
binTemp = (struct bin *)malloc(sizeof(struct bin));
binTemp->cac = 1.0;
binTemp->list = NULL;
bArray[i] = binTemp;
}
我又遇到了段错误。我不知道这里发生了什么。我知道段错误意味着我正在尝试写入非法内存位置,这让我认为我必须使用 malloc 设置数组索引的大小。然而,
for(i=0;i<20;i++) bArray[i]= malloc(sizeof(struct bin));
也给了我一个段错误。我不知道我做错了什么。
我运行的实际代码:
//Header File
#ifndef hBin_h
#define hBin_h
#include <stdio.h> // stdio used for file io
#include <stdlib.h> // standard c library
#define MAX_S 20
//bin struc
struct bin {
double cac; //current available capacity
struct node *list; // pointer to linked list
};
//linked list node struct
struct node{
char *name; //name of item
double size; // weight of item
struct node *next; //pointer to next
};
//insert the new item into a node in its appropriate location using alphabetical ordering of item names
struct node *oInsert(char *item, double size, struct node *head);
//print the items of the list out along with the list’s capacity
void traverse(struct node *head);
// deallocate the nodes of the list
void destory(struct node *head);
//input info from file - name of object, weight of object
void input(FILE *inFile, char item[], double *weight);
#endif // hBin_h
#include "hBin.h"
void main(){
FILE *inFile;
char *item;
double *weight;
struct bin *bArray[20];
int i;
struct bin *binTemp, *pTemp;
inFile = fopen("run1.txt", "r"); //open file
printf("HERE1\n");
for(i=0;i<20;i++){
binTemp = (struct bin *)malloc(sizeof(struct bin));
binTemp->cac = 1.0;
binTemp->list = NULL;
bArray[i] = binTemp;
}
/*while(!feof(inFile)){
input(inFile, item, weight);
printf("%s, %.2f\n", item, *weight);
}*/
}
我使用了 gdb(不完全确定我在这里做什么):
(gdb) run
Starting program: /home/Christopher/CSC362/Pass_F/Main
[New Thread 813244.0xc7590]
[New Thread 813244.0xc6ce0]
[New Thread 813244.0xc7320]
[New Thread 813244.0xc5994]
HERE1
0 [main] Main 813244 cygwin_exception::open_stackdumpfile: Dumping stack trace to Main.exe.stackdump
[Thread 813244.0xc7320 exited with code 35584]
[Thread 813244.0xc6ce0 exited with code 35584]
[Inferior 1 (process 813244) exited with code 0105400]
(gdb) where
No stack.
(gdb) for(i=0;i<20;i++){
binTemp->cac = 1.0;
binTemp->list = NULL;
bArray[i] = binTemp;
} /usr/src/debug/cygwin-2.2.1-1/winsup/cygwin/crt0.c: No such file or directory.
(gdb) binTemp = (struct bin *)malloc(sizeof(struct bin));
Undefined command: "binTemp". Try "help".
(gdb) binTemp->cac = 1.0;
Undefined command: "binTemp->cac". Try "help".
(gdb) binTemp->list = NULL;
Undefined command: "binTemp->list". Try "help".
(gdb) bArray[i] = binTemp;
Undefined command: "bArray". Try "help".
(gdb) } for(i=0;i<20;i++){
Undefined command: "". Try "help".
(gdb) binTemp = (struct bin *)malloc(sizeof(struct bin));
Undefined command: "binTemp". Try "help".
(gdb) binTemp->cac = 1.0;
Undefined command: "binTemp->cac". Try "help".
(gdb) binTemp->list = NULL;
Undefined command: "binTemp->list". Try "help".
(gdb) bArray[i] = binTemp;
Undefined command: "bArray". Try "help".
(gdb) } for(i=0;i<20;i++){
binTemp->cac = 1.0;
Undefined command: "". Try "help".
(gdb) binTemp = (struct bin *)malloc(sizeof(struct bin));
Undefined command: "binTemp". Try "help".
(gdb) binTemp->cac = 1.0;
Undefined command: "binTemp->cac". Try "help".
(gdb) binTemp->list = NULL;
Undefined command: "binTemp->list". Try "help".
(gdb) bArray[i] = binTemp;
Undefined command: "bArray". Try "help".
【问题讨论】:
-
请贴出实际崩溃的代码,见MCVE
-
如果
bArray或binTemp如您在第一个代码sn-p 中显示的那样声明,那么您显示的任何代码sn-ps 都不应该给您分段错误。请在调试器中运行崩溃程序以定位崩溃,它发生在您的代码中,并查看涉及的变量以查看是否有任何错误。哦,请尝试创建一个Minimal, Complete, and Verifiable Example 并向我们展示。 -
顺便说一下,如果你没有包含正确的头文件,中间的例子,你转换
malloc的结果,实际上可能会导致未定义的行为和崩溃。请参阅"Do I cast the result of malloc?" 了解更多信息。 -
不是。一方面它不能编译,另一方面没有
#include语句,而且它不会像给定的那样崩溃。问题在于您没有发布的代码。 -
@CinCity 我假设您尝试在评论中添加代码?不要那样做!它不会被格式化并且基本上不可读。而是编辑您的问题以包含重要的相关信息。
标签: c arrays pointers struct malloc