[英]C Malloc to a Pointer Through Function Call Causes Bus Error
Due to my feeble understanding of allocating type memory to pointers, the following causes a bus error on the call to barrier_create ("hi" is never printed). 由于我对将类型内存分配给指针的理解不足,因此以下情况会导致对barrier_create的调用出现总线错误(从不打印“ hi”)。
typedef struct barrier barrier_t;
typedef struct barrier *barrier_p;
barrier_p test_barrier_p;
int main(int argc, char *argv[]) {
barrier_create(*test_barrier_p);
}
int barrier_create(barrier_p *barrier_pointer) {
printf("hi\n");
barrier_p old_barrier, new_barrier;
int count;
old_barrier = (barrier_p) *barrier_pointer;
new_barrier = (barrier_p) malloc(sizeof(*new_barrier));
count = pthread_mutex_init(&new_barrier->lock, NULL);
new_barrier->is_valid = VALID_BARRIER;
new_barrier->counter = 0;
new_barrier->release_flag = 0;
*barrier_pointer = new_barrier;
return HAPPY_HAPPY_JOY_JOY;
}
What am I missing or mis-typing? 我想念什么或输入错误?
You're dereferencing a bad pointer in your main function. 您正在取消引用主要功能中的错误指针。 To get the address of the variable, you use the address
&
operator, not the dereferencing *
operator. 要获取变量的地址,请使用地址
&
运算符,而不要使用解引用*
运算符。 Rewrite main as: 将main重写为:
barrier_create(&test_barrier_p);
barrier_create(*test_barrier_p);
Since barrier_create
takes address of a barrier_p
, this should be &test_barrier_p
, not *test_barrier_p
. 由于
barrier_create
的地址为barrier_p
,因此应为&test_barrier_p
,而不是*test_barrier_p
。
printf("hi\n");
Inaccurate test of code reachability because stdout is likely buffered; 代码可达性测试不准确,因为可能会缓冲stdout; I'd recommend
fprintf(stderr, "hi\\n");
我建议
fprintf(stderr, "hi\\n");
instead. 代替。
new_barrier = (barrier_p) malloc(sizeof(*new_barrier));
I'd say sizeof(barrier_t)
. 我会说
sizeof(barrier_t)
。 Again a *
in an odd place, the _p
notation may not be helping your type manipulation clarity. 同样,在一个奇怪的地方加一个
*
, _p
表示法可能无法帮助您清晰地键入类型。
For pedanticism, I would check the return value of malloc. 对于学究主义者,我将检查malloc的返回值。 I see little point in keeping the old value unless to recover in some way from a malloc error.
我认为保留旧值没有什么意义,除非可以通过某种方式从malloc错误中恢复。
What is the purpose of count? 计数的目的是什么?
The test_barrier_p
variable is a pointer to a barrier
structure which is never being initialized, so it's set to NULL (since it's at file scope). test_barrier_p
变量是指向从未初始化的barrier
结构的指针,因此将其设置为NULL(因为它在文件范围内)。
You're de-referencing it at the call from main()
to barrier_create()
. 您正在从
main()
到barrier_create()
的调用中取消引用它。
For help beyond that, you'll need to tell us, in English, what you're trying to achieve. 要获得更多帮助,您需要用英语告诉我们您要实现的目标。
The function int barrier_create(barrier_p *barrier_pointer)
takes a pointer as an argument. 函数
int barrier_create(barrier_p *barrier_pointer)
以指针作为参数。 However, you a passing in an actual barrier_p
in your main since you dereference it - barrier_create(*test_barrier_p)
. 但是,由于您取消引用了主
actual barrier_p
,所以您在actual barrier_p
中传递了一个actual barrier_p
- barrier_create(*test_barrier_p)
。 I think you should be passing the address like barrier_create(&test_barrier_p)
我认为您应该传递诸如
barrier_create(&test_barrier_p)
类的地址
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.