什么时候使用 Malloc 是个好主意?

发布于 2024-10-29 18:52:03 字数 293 浏览 1 评论 0原文

我对为变量分配内存有点困惑。例如,如果我正在创建一个大型数据结构(数组、结构体、对象等),并且我在 main() 的范围内初始化它,我应该创建一个局部变量

int chessBoard[ 8][8] = ...

或者我应该在堆上分配内存?

int * chessBoard = new array[8][8];
*chessBoard = ...

这重要吗?是优惠吗?是否更高效/更智能?我个人喜欢为较大的对象分配内存,我认为这些对象对于堆栈帧来说不够小。但我真的不知道。

I'm a bit confused about allocating memory for my variables. If e.g. I'm creating a large data structure (an array, a struct, an object, etc.), and I'm initializing it in main()'s scope, should I create a local variable

int chessBoard[8][8] = ...

or should I allocate memory on the heap?

int * chessBoard = new array[8][8];
*chessBoard = ...

Does it matter? Is it preferential? Is one more efficient/smarter? I personally like allocating memory for larger objects I don't feel are small enough for a stack frame. But I really don't know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

你在我安 2024-11-05 18:52:03

使用 malloc 分配的动态内存的主要原因是当您不知道编译时所需的最大内存量时。对于棋盘来说,大小始终是八乘八,并且数组就可以了。在大多数计算机上,八乘八的 int 数组很小。

对于名称列表,您很少知道可能遇到的最大数量,因此请使用 malloc。

数组要快一些,因为编译器保留了空间。使用 malloc,可以在运行时调用库函数来请求内存。一旦你有了内存,任何一种方式通常都是相同的速度。

使用指针访问 malloc 分配的内存比简单使用数组稍微复杂一些。但如果你想成为一名 C 程序员,你就应该了解两者。

对于非常大量的内存,您必须使用 malloc。

使用 malloc 分配的内存所犯的最大错误是使用完毕后忘记释放它。如果忘记释放它,则可能会发生内存泄漏。

The major reason to use dynamic memory allocated by malloc is when you don't know the maximum amount of memory you need at compile time. For a chessboard, the size is always eight by eight, and an array is fine. On most computers an eight by eight int array is tiny.

For lists of names, you rarely know the maximum number you might encounter, so use malloc.

Arrays are a little faster because the compiler reserves the space. With malloc, there is a run-time call to a library function to request the memory. Once you have the memory, either way is generally the same speed.

Using pointers to access the memory allocated by malloc is a little more complicated than simply using an array. But you should understand both if you are going to be a C programmer.

For very large amounts of memory, you have to use malloc.

The biggest mistake made with memory allocated by malloc is forgetting to free it when you are done with it. If you forget to free it, you can have a memory leak.

对你而言 2024-11-05 18:52:03

当对象所有权需要大量传递时,通常最好使用 malloc 分配内存。如果该对象将在单个引用块中使用并且不会超出范围,那么局部变量肯定更好。这一切都归结为“循环完成后是否需要保留它”。

如果它是在 main() 中定义的,正如您所暗示的,那么它肯定会永远有效(直到应用程序关闭),在这种情况下,它更多地取决于个人喜好。许多人更喜欢局部变量,因为它们更容易阅读,并且取消引用看起来不那么混乱。

It's generally better to allocated memory using malloc when the object ownership needs to be passed around a lot. If the object is going to be used in the single reference block and won't fall out of scope, then a local variable is definitely better. It all comes down to "do you need to hang onto it after the loop is done".

If it's defined in main() as you hint, then it'll certainly be valid forever (till app close), in which case it becomes much more a matter of personal preference. Many people prefer local variables because they're easier to read and the dereferencing doesn't look as messy.

旧竹 2024-11-05 18:52:03

您应该考虑分配的对象的大小,因为这样的代码:

int something[10000000];

会导致内存错误,但是:

int *something = new int[10000000];

会正常工作。

You should consider size of objects you allocate, becouse code like this:

int something[10000000];

will cause memory error, but:

int *something = new int[10000000];

will work fine.

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