使用分配内存功能的原因是什么?在c

发布于 2025-02-06 09:05:49 字数 123 浏览 6 评论 0原文

使用分配内存功能的原因是什么? (mallocexallocatePool,...)

如果每个变量或结构都不会将内存分配给自己,那么我们为什么使用这些功能?

What is the reason for using the functions that are allocating memory? (malloc , ExAllocatePool ,...)

If every variable or structure does not allocate memory to itself, then why do we use these functions?

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

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

发布评论

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

评论(2

叹沉浮 2025-02-13 09:05:49

使用分配内存的函数的原因是什么?

原因很简单。相同的C源代码可以在小型计算机(具有少数兆字节的移动电话)和超级计算机(带有数百万欧元的耗资数百万欧元,带有Terabytes的内存)上使用。

还要想想软件可移植性

一个典型的示例可能是对数字数组进行排序。便宜的计算机(也许是鼠标内的2欧元微处理器)只能对一千个int进行排序。

但是,超级计算机可以对100亿int的数组(不适合您的手机)进行排序。

如有示例,请查看 gnu make )或任何教科书实现 QuickSort

您甚至如何编码简单的C编译器(例如 tinycc ,哪些是开源的)?

如果每个变量或结构都不会将内存分配给本身,那么我们为什么使用这些功能?

每本关于C编程的书都在解释原因和方式。另请参见此 c参考

作为练习,编码C程序,该程序在任何给定的文本文件中输出最长的行。输入文件可能很小或很大(例如, sqlite )或一个巨大的sql dump a href =“ https://postgresql.org/” rel =“ nofollow noreferrer”> postgresql 数据库。

另请参见(有关Linux)我的 nofollow noreferrer“> nordl.c program(program(program)(program(program)(生成任意大的C代码)。

What is the reason for using the functions that are allocating memory?

The reason is simple. The same C source code might be used on small computers (mobile phones with a few megabytes) and on super computers (costing millions of €, with terabytes of memory).

Think also of software portability.

A typical example could be to sort an array of numbers. A cheap computer (perhaps the 2€ microprocessor inside your mouse) can sort just an array of a thousands int.

But a supercomputer could sort an array of ten billions int (that does not fit into your mobile phone).

For examples, look into the source code of GNU sort (or GNU make) or any textbook implementation of quicksort.

How would you code even a simple C compiler (like tinycc, which is open source) without allocating memory?

If every variable or structure does not allocate memory to itself, then why do we use these functions?

Every book on C programming is explaining why and how. See also this C reference.

As an exercise, code the C program which outputs the longest line in any given textual file. Input files could be small or huge (for example, the source code of sqlite) or the SQL dump of a huge PostGreSQL database.

See also (for Linux) my manydl.c program (generating arbitrarily big C code).

夜还是长夜 2025-02-13 09:05:49

使用内存分配函数的原因有很多:

  • 因此,程序可以处理可变量的数据:您可以定义最大尺寸的固定数组,但是一旦知道所需的内存,分配所需的内存就更加灵活运行时给定的问题。

  • 由于某些问题需要可变数量的内存,具体取决于所处理的实际数据,即使对于固定量数据。

  • 因此,一旦完成该任务,就可以将用于特定任务的内存用于其他目的。

There are many reasons to use memory allocation functions:

  • so programs can handle a variable amount of data: you could define fixed arrays of a maximum size, but it is more flexible to allocate the required memory once you know the amount needed for a given problem at runtime.

  • because some problems require a variable amount of memory depending on the actual data handled, even for a fixed amount data.

  • so memory used for a particular task can be reused for other purposes once the task has been completed.

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