C:通过 void * 返回的函数
来自Java,我对使用 Void 允许返回值感到困惑:
void *emalloc(size_t s) {
void *result = malloc(s);
if (NULL == result) {
fprintf(stderr, "MEMORY ALLOCATION FAILURE\n");
exit( EXIT_FAILURE );
}
return result;
}
这是否返回指向已分配内存的卡盘的指针?
Coming from Java I'm confused by the use of Void allowing a return value in the following:
void *emalloc(size_t s) {
void *result = malloc(s);
if (NULL == result) {
fprintf(stderr, "MEMORY ALLOCATION FAILURE\n");
exit( EXIT_FAILURE );
}
return result;
}
Is this returning a pointer to a chuck of allocated of memory ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的。 void* 指针基本上是指向内存地址的通用指针,通常可以将其类型转换为实际所需的任何类型。
Yes, it is. A void* pointer is basically a generic pointer to a memory address, which could then typically be typecast to whatever type was actually desired.
您的问题表明您误读了函数的返回类型。 之间有很大的区别:
foo
() 不带参数且不返回值,而 bar() 不带参数并返回通用指针。 在C中,关键字void用于指示通用指针,并且void *类型的对象可以转换为任何其他对象指针类型而不会丢失信息。
Your question indicates that you are misreading the function's return type. There is a big difference between:
and
foo() takes no arguments and does not return a value, while bar() takes no arguments and returns a generic pointer. In C, the keyword void is used to indicate a generic pointer, and an object of type void * can be converted to any other object pointer type without loss of information.
是的,这个函数返回一个指向指定大小的已分配内存的指针。 malloc 的不同之处在于它保证返回一个指针。 失败时它将退出应用程序。
Yes, this function is returning a pointer to allocated memory of a specified size. It's different in malloc in the sense that it's guaranteed to return a pointer. On failure it will exit the application.
是的。
void*
表示指向某物的指针,但没有特定类型。Yes.
void*
meaning pointer to something, but of no particular type.void 本质上意味着没有类型,所以如果我们有 void *p; p 是一个指向某个东西的指针,但我们还没有说是什么。
没有指针的 void 就什么都不是,因此 void foo(void) 是一个不带参数且不返回任何内容的函数。
是的,malloc 返回一个指向某个内存块的指针,malloc 不知道也不关心该内存的类型,所以它的返回类型是 void*
void means essentially no type, so if we have void *p; p is a pointer to something, but we haven't said what.
void without a pointer is nothing hence void foo(void) being a function that takes no arguments and returns nothing.
And yes malloc returns a pointer to some chunk of memory, malloc doesn't know or care what type that memory has, so it's return type is void*
Java 中没有指针运算。 它认为这就是你要问的。 例如,假设
malloc
返回一个int
类型的指针,您可能会收到该指针,它基本上是一个指向
数组
的指针。 然后你可以像常规数组一样对其进行索引。问题是
C
没有函数重载。 因此,我们必须找到一种方法来编写通用的malloc
。 否则,您最终会为每种类型得到不同的malloc
。 解决方案是发送您需要的字节数。 然后,您可以按照自己的喜好对其进行索引。 这提供了更大的灵活性和通用的解决方案。因此,整个想法是,我们如何索引从
malloc
获得的内存并不重要。 我们所要做的就是指定新创建的数组的类型以正确索引它。void*
意味着这是一个放置在内存中的指针,我们尚未指定如何索引。In Java there is no pointer arithmetic. It think this is what you are asking about. For example, imagine that
malloc
returns a pointer of typeint
You would possibly receive the pointer, which is basically a pointer to an
array
. Then you would index it like regular arrays.The problem is that
C
doesn't have functions overloading. So, we have to find a way to write a genericmalloc
. Otherwise, you would end up with a differentmalloc
for every type. The solution, is to send the required number of bytes that you need. Then, you can index it however you like. This gives a greater flexibility and one for all solution.So, the whole idea is that it doesn't matter how we index the memory we got from
malloc
. All what we have to do is to specify the type of the newly created array to index it properly.void*
means that this is a pointer to place in memory that we haven't specified how to index.