c语言:为什么malloc()函数返回一个指针,而不是价值? [英] C Language: Why does malloc() return a pointer, and not the value?

查看:415
本文介绍了c语言:为什么malloc()函数返回一个指针,而不是价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的C的理解看来,你应该使用malloc(大小),只要你想进行初始化,例如,一个数组,其大小,你不知道,直到运行时。

From my understanding of C it seems that you are supposed to use malloc(size) whenever you are trying to initialize, for instance, an array whose size you do not know of until runtime.

但我想知道为什么malloc()函数返回的指针应用于变量的位置,为什么你甚至需要这一点。

But I was wondering why the function malloc() returns a pointer to the location of the variable and why you even need that.

基本上,为什么不是C只是掩饰这一切从你的,所以,只要你做这样的事情:

Basically, why doesn't C just hide it all from you, so that whenever you do something like this:

    // 'n' gets stdin'ed from the user
    ...
    int someArray[n];

    for(int i = 0; i < n; i++)
        someArray[i] = 5;

您可以做到这一点,而无须调用malloc()或其他一些功能?难道其他语言做这样的(通过完全隐藏内存性能/位置)?我觉得,作为一个初学者这整个处理的你只使用混淆程序员变量的存储位置的过程(并自其他语言不使用它,C似乎做一个简单的初始化过程,如这个过于复杂)...

you can do it without ever having to call malloc() or some other function? Do other languages do it like this (by hiding the memory properties/location altogether)? I feel that as a beginner this whole process of dealing with the memory locations of variables you use just confuse programmers (and since other languages don't use it, C seems to make a simple initialization process such as this overly complicated)...

基本上,我想要问的是,为什么malloc()函数甚至是必需的,因为为什么语言不而不必担心或有看到内存程序员照顾所有你在内部。谢谢

Basically, what I'm trying to ask is why malloc() is even necessary, because why the language doesn't take care of all that for you internally without the programmer having to be concerned about or having to see memory. Thanks

*编辑:好吧,也许是C的某些版本中,我是不知道的,可以让你放弃使用malloc()函数,但让我们试着忽略现在...

*edit: Ok, maybe there are some versions of C that I'm not aware of that allows you to forgo the use of malloc() but let's try to ignore that for now...

推荐答案

ç让您管理您的程序的一点一滴......

C lets you manage every little bit of your program ...

您可以管理的内存分配得到;您可以管理的它被释放;您可以管理如何长出了小拨,...,...,...

You can manage when memory gets allocated; you can manage when it gets deallocated; you can manage how to grow a small allocation, ..., ..., ...

如果您preFER不来管理,让编译器为你做,可使用另一种语言。

If you prefer not to manage that and let the compiler do it for you, use another language.

这篇关于c语言:为什么malloc()函数返回一个指针,而不是价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆