我是C语言编程的新手,我主要是玩它(我通常使用Java工作)所以请原谅我的无知,如果这是一个明显的错误...我刚刚开始制作一个基于简单文本的Space Sim和我得到两个不同的奇怪错误,这些错误每隔2-3次偶尔会出现,我很难解读(一次只出现1次)。我假设我已经错误地分配了一个变量,但我可以帮忙指出它。
代码分为3个文件:
Physics.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;
}
}
Space.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);
}
Main(Space_In_C.c):
#include <stdio.h>
#include "Space.c" //includes space
//Setup the main variables
void setup(Vector *myShip, Space *space){
myShip -> x = 0;
myShip -> y = 0;
initSpace(space, 10); //initialise the space array
return;
}
int main(int argc, char *argv[]) {
Vector myShip; //create the Vector that stores the ships location
Space space; //create the Space array
setup(&myShip, &space); //set up all key variables
createObjectInSpace(&space, planet, 7, 5);
createObjectInSpace(&space, craft, 7, 5);
Space objects = getObjectsAtLocationInt(&space, 7, 5);
printf("%d", objects.array[1].type);
return 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
第二个错误:
Terminated due to signal: ABORT TRAP (6)
lli(5537,0x7fffc60423c0) malloc: *** error for object 0x7ff8e8603fc0: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
00 lli 0x0000000107f84da9 void std::__1::seed_seq::generate<unsigned int*>(unsigned int*, unsigned int*) + 9993
1 lli 0x0000000107f8583b void std::__1::seed_seq::generate<unsigned int*>(unsigned int*, unsigned int*) + 12699
2 libsystem_platform.dylib 0x00007fffbd52fbba _sigtramp + 26
3 libsystem_platform.dylib 0x00000001109c10d0 _sigtramp + 1397298480
4 libsystem_c.dylib 0x00007fffbd3b6420 abort + 129
5 libsystem_malloc.dylib 0x00007fffbd4b0fb1 szone_error + 626
6 libsystem_malloc.dylib 0x00007fffbd4a6fbf tiny_free_list_remove_ptr + 292
7 libsystem_malloc.dylib 0x00007fffbd4bb932 tiny_free_no_lock + 1484
8 libsystem_malloc.dylib 0x00007fffbd4bc0f3 free_tiny + 671
9 lli 0x0000000107c4ca82 void std::__1::vector<llvm::JITEventListener*, std::__1::allocator<llvm::JITEventListener*> >::__push_back_slow_path<llvm::JITEventListener* const&>(llvm::JITEventListener* const&&&) + 7122
10 lli 0x00000001076af1c5 void std::__1::vector<unsigned long long, std::__1::allocator<unsigned long long> >::__push_back_slow_path<unsigned long long>(unsigned long long&&) + 24085
11 libdyld.dylib 0x00007fffbd322255 start + 1
12 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
为大规模代码转储道歉!任何帮助将不胜感激!
答案 0 :(得分:3)
在initSpace中,看起来你想要为一个对象数组(包含一个枚举和一个Vector)分配内存,但你只为int的数组分配足够的内存
a->array = (Object *)malloc(initialSize * sizeof(int));
应该是
a->array = (Object *)malloc(initialSize * sizeof(Object));
可能还有其他错误,但那个突出了。