第二次使用指针读取结构会出现分段错误
Reading a Structure using pointer second time gives segmentation fault error
当我使用 vector_read() 函数读取第二个向量或通过使用 vector_init_zero() 将其初始化为零时,我遇到了分段错误。我已经搜索了很多仍然没有得到它为什么会发生的答案。如果我只使用一个指针并且不将其初始化为 0,它就可以正常工作。每个其他函数都可以很好地处理结构指针的单个变量。
#include <stdio.h>
#include <stdlib.h>
typedef struct vector_s
{
int dim;
float *data_ptr;
} vector_type;
/* Allocate and initialize to 0 a vector with the given dimension */
void vector_init_zeros(vector_type *v_ptr, int dim)
{
v_ptr->dim = dim;
v_ptr->data_ptr = (float *)calloc(dim, sizeof(float));
#ifdef DEBUG
if (v_ptr->data_ptr == 0)
{
printf("vector_init_zeros error: calloc returned a null ptr\n");
exit(1);
}
#endif
}
/* Free up a vector that was allocated using vector_init_zeros */
void vector_deinit(vector_type *v_ptr)
{
v_ptr->dim = 0;
free(v_ptr->data_ptr);
v_ptr->data_ptr = 0;
}
/* Attempt to read a vector from stdin. */
/* Returns 1 if read successful and 0 otherwise */
int vector_read(vector_type *v_ptr)
{
int i;
float next;
for (i = 0; i < v_ptr->dim; i++)
{
if (scanf("%g\n", &next) != 1)
{
return 0;
}
v_ptr->data_ptr[i] = next;
}
return 1;
}
/* w = u + v */
void vector_add(vector_type *u_ptr, vector_type *v_ptr,
vector_type *w_ptr)
{
int i;
for (i = 0; i < v_ptr->dim; i++)
{
w_ptr->data_ptr[i] = u_ptr->data_ptr[i] + v_ptr->data_ptr[i];
}
}
/* v = cv */
void vector_scalar_mult(vector_type *v_ptr, float c)
{
int i;
for (i = 0; i < v_ptr->dim; i++)
{
v_ptr->data_ptr[i] = v_ptr->data_ptr[i] * c;
}
}
/* print the compondents of the given vector */
void vector_print(vector_type *v_ptr)
{
int i;
for (i = 0; i < v_ptr->dim; i++)
{
printf("%f ", v_ptr->data_ptr[i]);
}
printf("\n");
}
int main()
{
int dim;
int count = 0;
if (scanf("%d", &dim) != 1)
{
printf("Error reading the vector dimension from stdin\n");
exit(1);
}
vector_type *s,*t;
vector_init_zeros(s, dim);
vector_init_zeros(t, dim);
vector_read(s);
vector_read(t);
}
在声明 s
和 t
的 main()
中,您永远不会初始化它们。您可以将它们声明为结构变量并使用 &
'address of' 运算符将它们的地址传递给函数,或者使用 malloc()
和 free()
或等价物分配和释放它们。
int main(){
...
vector_type s, t;
...
vector_init_zeros(&s, dim);
vector_init_zeros(&t, dim);
vector_read(&s);
vector_read(&t);
}
或
int main(){
...
vector_type *s, *t;
s = (vector_type *) malloc(sizeof(vector_type));
t = (vector_type *) malloc(sizeof(vector_type));
...
free(s);
free(t);
}
当我使用 vector_read() 函数读取第二个向量或通过使用 vector_init_zero() 将其初始化为零时,我遇到了分段错误。我已经搜索了很多仍然没有得到它为什么会发生的答案。如果我只使用一个指针并且不将其初始化为 0,它就可以正常工作。每个其他函数都可以很好地处理结构指针的单个变量。
#include <stdio.h>
#include <stdlib.h>
typedef struct vector_s
{
int dim;
float *data_ptr;
} vector_type;
/* Allocate and initialize to 0 a vector with the given dimension */
void vector_init_zeros(vector_type *v_ptr, int dim)
{
v_ptr->dim = dim;
v_ptr->data_ptr = (float *)calloc(dim, sizeof(float));
#ifdef DEBUG
if (v_ptr->data_ptr == 0)
{
printf("vector_init_zeros error: calloc returned a null ptr\n");
exit(1);
}
#endif
}
/* Free up a vector that was allocated using vector_init_zeros */
void vector_deinit(vector_type *v_ptr)
{
v_ptr->dim = 0;
free(v_ptr->data_ptr);
v_ptr->data_ptr = 0;
}
/* Attempt to read a vector from stdin. */
/* Returns 1 if read successful and 0 otherwise */
int vector_read(vector_type *v_ptr)
{
int i;
float next;
for (i = 0; i < v_ptr->dim; i++)
{
if (scanf("%g\n", &next) != 1)
{
return 0;
}
v_ptr->data_ptr[i] = next;
}
return 1;
}
/* w = u + v */
void vector_add(vector_type *u_ptr, vector_type *v_ptr,
vector_type *w_ptr)
{
int i;
for (i = 0; i < v_ptr->dim; i++)
{
w_ptr->data_ptr[i] = u_ptr->data_ptr[i] + v_ptr->data_ptr[i];
}
}
/* v = cv */
void vector_scalar_mult(vector_type *v_ptr, float c)
{
int i;
for (i = 0; i < v_ptr->dim; i++)
{
v_ptr->data_ptr[i] = v_ptr->data_ptr[i] * c;
}
}
/* print the compondents of the given vector */
void vector_print(vector_type *v_ptr)
{
int i;
for (i = 0; i < v_ptr->dim; i++)
{
printf("%f ", v_ptr->data_ptr[i]);
}
printf("\n");
}
int main()
{
int dim;
int count = 0;
if (scanf("%d", &dim) != 1)
{
printf("Error reading the vector dimension from stdin\n");
exit(1);
}
vector_type *s,*t;
vector_init_zeros(s, dim);
vector_init_zeros(t, dim);
vector_read(s);
vector_read(t);
}
在声明 s
和 t
的 main()
中,您永远不会初始化它们。您可以将它们声明为结构变量并使用 &
'address of' 运算符将它们的地址传递给函数,或者使用 malloc()
和 free()
或等价物分配和释放它们。
int main(){
...
vector_type s, t;
...
vector_init_zeros(&s, dim);
vector_init_zeros(&t, dim);
vector_read(&s);
vector_read(&t);
}
或
int main(){
...
vector_type *s, *t;
s = (vector_type *) malloc(sizeof(vector_type));
t = (vector_type *) malloc(sizeof(vector_type));
...
free(s);
free(t);
}