Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
偶发性C错误_C - Fatal编程技术网

偶发性C错误

偶发性C错误,c,C,我是C语言编程新手,我主要是在玩它(我通常在Java中工作),所以如果这是一个明显的错误,请原谅我的无知。。。我刚刚开始制作一个简单的基于文本的空间Sim卡,我会发现两个不同的奇怪错误,它们每隔2-3次就会偶尔出现,我正在努力破译这些错误(一次只出现1个)。我假设我错误地分配了一个变量,但是我可以通过一些帮助指出它 代码分为3个文件: 物理学.c: typedef enum { false, true } bool; //define what a boolean is //define a

我是C语言编程新手,我主要是在玩它(我通常在Java中工作),所以如果这是一个明显的错误,请原谅我的无知。。。我刚刚开始制作一个简单的基于文本的空间Sim卡,我会发现两个不同的奇怪错误,它们每隔2-3次就会偶尔出现,我正在努力破译这些错误(一次只出现1个)。我假设我错误地分配了一个变量,但是我可以通过一些帮助指出它

代码分为3个文件:

物理学.c:

typedef enum { false, true } bool; //define what a boolean is

//define a vector
typedef struct {
    int x;
    int y;
} Vector; 

bool compareVectors(Vector vectorA, Vector vectorB){
    if ((vectorA.x == vectorB.x) && (vectorA.y == vectorB.y)){
        return true;
    } else {
        return false;
    }
}
空间.c:

#include <stdio.h>
#include <stdlib.h>
#include "Physics.c" //includes space

enum Type {
    craft = 0,
    planet,
    star
};


typedef struct {
    enum Type type;
    Vector position;
} Object;

typedef struct {
    Object *array;
    int used;
    int size;

} Space;


void initSpace(Space *a, int initialSize) {
    a->array = (Object *)malloc(initialSize * sizeof(int));
    a->used = 0;
    a->size = initialSize;
}

//inserts a new object in space and returns the location of the new object in the array
int insertToSpace(Space *a, Object element) {
    if (a->used == a->size) {
        a->size *= 2;
        a->array = (Object *)realloc(a->array, a->size * sizeof(int));
    }
    a->array[a->used++] = element;

    return a->used - 1;
}

void freeSpaceArray(Space *a) {
    free(a->array);
    a->array = NULL;
    a->used = a->size = 0;
}

//creates a new object in space and returns the location of the object in the array
int createObjectInSpace(Space *a, int type, int xPos, int yPos){
    Object newObject;
    Vector tempVector;

    newObject.type = type;
    tempVector.x = xPos;
    tempVector.y = yPos;
    newObject.position = tempVector;

    return insertToSpace(a, newObject);
}

//returns the number of objects at that location in space based on Vector location
int objectsAtLocationVector(Space *a, Vector pos){
    int count = 0;
    for (int i = 0; i < a->used; i++){
        if (compareVectors(a->array[i].position, pos)){
            count++;
        }
    }
    return count;
}

//returns the number of objects at that location in space, based on an x/y int location
int objectsAtLocationInt(Space *a, int xPos, int yPos){
    Vector tempVector;
    tempVector.x = xPos;
    tempVector.y = yPos;
    return objectsAtLocationVector(a, tempVector);
}

//returns a list of all objects at the specified location vector
Space getObjectsAtLocationVector(Space *space, Vector pos){
    Space newSpace;
    int noOfObjects = objectsAtLocationVector(space, pos);

    initSpace(&newSpace, noOfObjects);

    if (noOfObjects > 0){
        for (int i = 0; i < space->used; i++){
            if (compareVectors(space->array[i].position, pos)){
                insertToSpace(&newSpace, space->array[i]);
            }
        }
    } 

    return newSpace;
}
//returns a list of all objects at the specified integer location
Space getObjectsAtLocationInt(Space *space, int xPos, int yPos){
    Vector tempVector;
    tempVector.x = xPos;
    tempVector.y = yPos;
    return getObjectsAtLocationVector(space, tempVector);
}
#包括
#包括
#包括“Physics.c”//包括空格
枚举类型{
craft=0,
行星,
明星
};
类型定义结构{
枚举类型;
矢量位置;
}对象;
类型定义结构{
对象*数组;
使用int;
整数大小;
}空间;
void initSpace(空格*a,int initialSize){
a->array=(Object*)malloc(initialSize*sizeof(int));
a->used=0;
a->size=初始大小;
}
//在空间中插入新对象并返回新对象在数组中的位置
int insertToSpace(空格*a,对象元素){
如果(a->used==a->size){
a->size*=2;
a->array=(Object*)realloc(a->array,a->size*sizeof(int));
}
a->array[a->used++]=元素;
返回a->used-1;
}
void freeSpaceArray(空格*a){
自由(a->数组);
a->array=NULL;
a->used=a->size=0;
}
//在空间中创建新对象并返回该对象在数组中的位置
int createObjectInSpace(空格*a,int类型,int xPos,int yPos){
对象新建对象;
向量tempVector;
newObject.type=类型;
tempVector.x=xPos;
tempVector.y=yPos;
newObject.position=tempVector;
返回insertToSpace(a,newObject);
}
//基于向量位置返回空间中该位置的对象数
int objectsAtLocationVector(空格*a,向量位置){
整数计数=0;
对于(int i=0;iused;i++){
if(比较器(a->数组[i].位置,位置)){
计数++;
}
}
返回计数;
}
//基于x/y int位置返回空间中该位置的对象数
int objectsAtLocationInt(空格*a、int xPos、int yPos){
向量tempVector;
tempVector.x=xPos;
tempVector.y=yPos;
返回objectsAtLocationVector(a,tempVector);
}
//返回指定位置向量处所有对象的列表
空间GetObjectsLocationVector(空间*空间,向量位置){
空间新闻空间;
int noOfObjects=对象位置向量(空间,位置);
initSpace(和新闻空间、noOfObjects);
如果(无对象>0){
对于(int i=0;iused;i++){
if(比较器(空间->数组[i].位置,位置)){
insertToSpace(&newSpace,space->array[i]);
}
}
} 
返回新闻空间;
}
//返回指定整数位置处所有对象的列表
Space getObjectsAtLocationInt(Space*Space,intxpos,intypos){
向量tempVector;
tempVector.x=xPos;
tempVector.y=yPos;
返回getObjectsAtLocationVector(空格,tempVector);
}
主(C.C中的空格):

#包括
#包括“Space.c”//包括空格
//设置主要变量
无效设置(向量*myShip,空间*Space){
myShip->x=0;
myShip->y=0;
initSpace(space,10);//初始化空间数组
返回;
}
int main(int argc,char*argv[]){
Vector myShip;//创建存储船舶位置的向量
Space;//创建空间数组
setup(&myShip,&space);//设置所有关键变量
createObjectInSpace(&space,planet,7,5);
createObjectInSpace(空间、工艺、7、5);
spaceobjects=getObjectsAtLocationInt(&Space,7,5);
printf(“%d”,objects.array[1].type);
返回0;
}
这里是第一个错误:

Terminated due to signal: SEGMENTATION FAULT (11)
0  lli                      0x000000010705dda9 void std::__1::seed_seq::generate<unsigned int*>(unsigned int*, unsigned int*) + 9993
1  lli                      0x000000010705e83b void std::__1::seed_seq::generate<unsigned int*>(unsigned int*, unsigned int*) + 12699
2  libsystem_platform.dylib 0x00007fffbd52fbba _sigtramp + 26
3  libsystem_platform.dylib 0x00007fff59489ce0 _sigtramp + 2616566080
4  lli                      0x0000000106f303fe llvm::raw_ostream& llvm::operator<<<llvm::BasicBlock>(llvm::raw_ostream&, llvm::DomTreeNodeBase<llvm::BasicBlock> const*) + 12270
5  lli                      0x0000000106fe21f6 llvm::SmallVectorImpl<std::__1::pair<unsigned int, llvm::TypedTrackingMDRef<llvm::MDNode> > >::operator=(llvm::SmallVectorImpl<std::__1::pair<unsigned int, llvm::TypedTrackingMDRef<llvm::MDNode> > >&&) + 4038
6  lli                      0x0000000106d2376f std::__1::__tree<std::__1::__value_type<llvm::StringRef, llvm::StringRef>, std::__1::__map_value_compare<llvm::StringRef, std::__1::__value_type<llvm::StringRef, llvm::StringRef>, std::__1::less<llvm::StringRef>, true>, std::__1::allocator<std::__1::__value_type<llvm::StringRef, llvm::StringRef> > >::destroy(std::__1::__tree_node<std::__1::__value_type<llvm::StringRef, llvm::StringRef>, void*>*) + 23615
7  lli                      0x0000000106d20b4e std::__1::__tree<std::__1::__value_type<llvm::StringRef, llvm::StringRef>, std::__1::__map_value_compare<llvm::StringRef, std::__1::__value_type<llvm::StringRef, llvm::StringRef>, std::__1::less<llvm::StringRef>, true>, std::__1::allocator<std::__1::__value_type<llvm::StringRef, llvm::StringRef> > >::destroy(std::__1::__tree_node<std::__1::__value_type<llvm::StringRef, llvm::StringRef>, void*>*) + 12318
8  lli                      0x0000000106d20c9e std::__1::__tree<std::__1::__value_type<llvm::StringRef, llvm::StringRef>, std::__1::__map_value_compare<llvm::StringRef, std::__1::__value_type<llvm::StringRef, llvm::StringRef>, std::__1::less<llvm::StringRef>, true>, std::__1::allocator<std::__1::__value_type<llvm::StringRef, llvm::StringRef> > >::destroy(std::__1::__tree_node<std::__1::__value_type<llvm::StringRef, llvm::StringRef>, void*>*) + 12654
9  lli                      0x0000000106788426 void std::__1::vector<unsigned long long, std::__1::allocator<unsigned long long> >::__push_back_slow_path<unsigned long long>(unsigned long long&&) + 24694
10 libsystem_c.dylib        0x00007fffbd3b717f __cxa_finalize_ranges + 339
11 libsystem_c.dylib        0x00007fffbd3b74b2 exit + 55
12 lli                      0x0000000106d22d56 std::__1::__tree<std::__1::__value_type<llvm::StringRef, llvm::StringRef>, std::__1::__map_value_compare<llvm::StringRef, std::__1::__value_type<llvm::StringRef, llvm::StringRef>, std::__1::less<llvm::StringRef>, true>, std::__1::allocator<std::__1::__value_type<llvm::StringRef, llvm::StringRef> > >::destroy(std::__1::__tree_node<std::__1::__value_type<llvm::StringRef, llvm::StringRef>, void*>*) + 21030
13 lli                      0x0000000106788290 void std::__1::vector<unsigned long long, std::__1::allocator<unsigned long long> >::__push_back_slow_path<unsigned long long>(unsigned long long&&) + 24288
14 libdyld.dylib            0x00007fffbd322255 start + 1
15 libdyld.dylib            0x0000000000000002 start + 1120787886
Stack dump:
0.  Program arguments: lli /var/folders/9k/f9_xm4857vq6_fysth4wl5mh0000gn/T/com.coderunnerapp.CodeRunner/CodeRunner/clang/Space_In_C.ll 
由于信号终止:分段故障(11)
0 lli 0x000000010705dda9无效标准::uu 1::种子顺序::生成(无符号整数*,无符号整数*)+9993
1 lli 0x000000010705e83b无效标准::种子顺序::生成(无符号整数*,无符号整数*)+12699
2 libsystem_platform.dylib 0x00007fffbd52fbba_sigtramp+26
3 libsystem_platform.dylib 0x00007fff59489ce0_sigtramp+2616566080

4 lli 0x0000000106f303fe llvm::raw_ostream&llvm::operator在initSpace中,您似乎想要为对象数组(包含枚举和向量)分配内存,但您只为int数组分配了足够的内存

  a->array = (Object *)malloc(initialSize * sizeof(int));
应该是

  a->array = (Object *)malloc(initialSize * sizeof(Object));

很可能还有其他bug,但其中一个很突出。

在initSpace中,看起来您想要为对象数组(包含枚举和向量)分配内存,但您只为int数组分配了足够的内存

  a->array = (Object *)malloc(initialSize * sizeof(int));
应该是

  a->array = (Object *)malloc(initialSize * sizeof(Object));

可能还有其他bug,但其中一个很突出。

这不是一个调试站点。你应该检查一下valgrind,也许它能帮你找到bug。顺便说一句:您确定,
sizeof(int)
等于
sizeof(Object)
?请注意,您通常不会在其他
.c
文件中包含
.c
文件。您可以创建标题并包含它们,分别编译
.c
文件,并将它们链接在一起以构建最终的可执行文件。此外,还有一个
标题,提供类型
bool
(内置类型
\u bool
的替代名称)和值
true
false
。这不是调试站点。你应该检查一下valgrind,也许它能帮你找到bug。顺便说一句:您确定,
sizeof(int)
等于
sizeof(Object)
?请注意,您通常不会在其他
.c
文件中包含
.c
文件。您可以创建标题并包含它们,分别编译
.c
文件,并将它们链接在一起以构建最终的可执行文件。此外,还有一个
标题,提供类型
bool
(内置类型
\u bool
的替代名称)和v