【发布时间】:2016-09-05 16:50:19
【问题描述】:
我编写的代码在 netbeans 上运行良好,但在 putty 上出现了段错误。我试过用gdb,但似乎无能为力在哪给我
Program received signal SIGSEGV, Segmentation fault.
0x00000000004006c1 in init_vector ()
(gdb) back trace
#0 0x00000000004006c1 in init_vector ()
#1 0x0000000000400664 in main ()
这是我的代码,希望有人能帮助我
typedef struct v_
{
int* data;
int size;
int capacity;
}vector;
int main()
{
// vector of floats
vector *vec;
init_vector(vec);
insert_element_vector(vec, 5);
int ele = access_element_vector(vec, 0);
printf("%d\n", ele);
return 0;
}
void init_vector( vector* v)
{
v->data = malloc(sizeof(int) * INIT_VECTOR_SIZE);
v->size = 0;
v->capacity = INIT_VECTOR_SIZE;
}
void insert_element_vector( vector* v, int element_to_insert)
{
if(v->capacity == v->size)
{
v->data = realloc(v->data, sizeof(int) * v->capacity * 2);
v->capacity *= 2;
}
v->data[v->size] = element_to_insert;
v->size += 1;
}
【问题讨论】:
标签: c pointers segmentation-fault dereference