C:通过 void * 返回的函数

发布于 2024-07-30 11:34:44 字数 295 浏览 3 评论 0原文

来自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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

无法言说的痛 2024-08-06 11:34:44

是的。 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.

紫轩蝶泪 2024-08-06 11:34:44

您的问题表明您误读了函数的返回类型。 之间有很大的区别:

void foo( void ) {}

foo

void *bar( void ) {}

() 不带参数且不返回值,而 bar() 不带参数并返回通用指针。 在C中,关键字void用于指示通用指针,并且void *类型的对象可以转换为任何其他对象指针类型而不会丢失信息。

Your question indicates that you are misreading the function's return type. There is a big difference between:

void foo( void ) {}

and

void *bar( void ) {}

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.

七禾 2024-08-06 11:34:44

是的,这个函数返回一个指向指定大小的已分配内存的指针。 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.

绝不放开 2024-08-06 11:34:44

是的。 void* 表示指向某物的指针,但没有特定类型。

Yes. void* meaning pointer to something, but of no particular type.

合久必婚 2024-08-06 11:34:44

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*

梦回梦里 2024-08-06 11:34:44

Java 中没有指针运算。 它认为这就是你要问的。 例如,假设malloc返回一个int类型的指针,

int* malloc(size_t size)
{
    //
}

您可能会收到该指针,它基本上是一个指向数组的指针。 然后你可以像常规数组一样对其进行索引。

int* arr = malloc(10 * sizeof(int)); // 10 contiguous int(s).

问题是 C 没有函数重载。 因此,我们必须找到一种方法来编写通用的malloc。 否则,您最终会为每种类型得到不同的 malloc。 解决方案是发送您需要的字节数。 然后,您可以按照自己的喜好对其进行索引。 这提供了更大的灵活性和通用的解决方案。

int*  i = (int*)malloc(10 * sizeof(int)); // 40 bytes if int = 4 bytes
char* c = (char*)malloc(10 * sizeof(char)); // 10 bytes if char = 1 byte

int  thirdElement = i[3]; // third element. 12 bytes away from (*i)
char secondElement = c[2]; // second element. 2 bytes away from (*c)

因此,整个想法是,我们如何索引从 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 type int

int* malloc(size_t size)
{
    //
}

You would possibly receive the pointer, which is basically a pointer to an array. Then you would index it like regular arrays.

int* arr = malloc(10 * sizeof(int)); // 10 contiguous int(s).

The problem is that C doesn't have functions overloading. So, we have to find a way to write a generic malloc. Otherwise, you would end up with a different malloc 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.

int*  i = (int*)malloc(10 * sizeof(int)); // 40 bytes if int = 4 bytes
char* c = (char*)malloc(10 * sizeof(char)); // 10 bytes if char = 1 byte

int  thirdElement = i[3]; // third element. 12 bytes away from (*i)
char secondElement = c[2]; // second element. 2 bytes away from (*c)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文