内存和指针

发布于 2024-09-27 16:32:11 字数 461 浏览 6 评论 0原文

我需要一些帮助来思考一项任务。

我的任务是创建一个内存区域

void *memory = malloc(320);

,然后使用指针将文本存储到这个存储位置:我们想将这个区域划分为32字节的数据块,所以我们可以存储:320/32 = 10个数据块a 32字节。在一个数据块中,我可以存储(1 ASCSII 字符 = 1 字节)32 个字符。

我有一个 10 长的位图,其中每个位都指示数据块是否使用(1)或不使用(0)。

但是如果我想存储 60 个字符长的文本怎么办?然后我需要 2 个数据块(2 x 32 字节)。位图显示数据块2和6是空闲的,1和6不是并排的。我怎样才能实现这个目标?

struct  data {
    char * text;
};

typedef struct data d;

d->text = ???

I have need some some help with some thinking around a task.

My task is to create one memory area

void *memory = malloc(320);

and then use pointers to store texts into this storage place: We want to divide this area into data blocks of 32 bytes, sow we can store: 320/32 = 10 data blocks a 32 bytes. Into one data block I can store (1 ASCSII char = 1 bytes) 32 characters.

I have a bitmap that is 10 long where every bit indicates if data block is used(1) or not(0).

But what if I want to store a text that is 60 characters long? Then i need 2 data blocks (2 x 32 bytes). The bitmap shows that data block 2 and 6 are free, 1 and 6 is not side by side. How can I achieve this?

struct  data {
    char * text;
};

typedef struct data d;

d->text = ???

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

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

发布评论

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

评论(6

听风吹 2024-10-04 16:32:11

这称为内存碎片,是一个严重的问题。即使技术上有足够的内存来支持该块,您也必须报告内存不足。

像 C# 这样不允许指针的托管语言(在正常情况下 - 请不要关注这一点)可以自由地重新排列底层内存并解决这个问题(尽管它在性能方面不是免费的)。

解决 C: 中的问题
您无能为力,因为这些指向内存的指针会阻止您重新排列所有内容。其他人提到了好友系统,还有其他系统,但很少有简单的。很多都是基于预设的“大块”和“小块”,并且只允许小请求、小块等......但这一切都是为了首先停止解决问题,一旦你在那里,你要么否认内存请求或扩展池。

This is called memory fragmentation and is a serious problem. You have to report out of memory even though there is technically enough to support the block.

Managed languages like C# that don't allow pointers (in the normal case - please don't fixate on this) have the freedom to rearrange the underlying memory and fix this problem (though it's not free in terms of performance).

To fix the problem in C:
There's not a lot you can do because those pointers into the memory prevent you from reshuffling everything. Someone else has mentioned the buddy system and there are others but few are simple. A lot are based on having preset 'big chunks' and 'small chunks' and only allowing small requests small chunks etc... but that's all to stop arriving at the problem in the first place, once you're there you either deny the memory request or expand the pool.

素罗衫 2024-10-04 16:32:11

正如其他评论和答案中提到的,这是一个碎片问题。您可以对代码进行碎片整理(这将对系统如何访问内存提出很多要求和限制),或者分配内存。

有一些技术可以最大限度地减少碎片。一种流行的方法是伙伴内存分配:http://en.wikipedia.org/wiki/Buddy_memory_allocation

As mentioned in other comments and answers, this is a fragmentation problem. You could either defragment the code (which will impose lots of requirements and limitations on how systems are allowed to access the memory), or allocate memory.

There are techniques to minimize fragmentation. One popular one is buddy memory allocation: http://en.wikipedia.org/wiki/Buddy_memory_allocation

圈圈圆圆圈圈 2024-10-04 16:32:11

您必须添加一种内存管理器层来跟踪特定条目占用的插槽(在本例中为字符串)以及插槽的使用顺序 - 您的位字段是不够的。

You would have to add a kind of a memory manager layer that keeps track of what slots a particular entry occupies (in this case a string) and in which order the slots are used - your bit field would not be enough.

红玫瑰 2024-10-04 16:32:11

您的字符串数据结构应该与块管理器完全相同,只是它应该跟踪“本地”块而不是内存池中的所有块。

Your string data structure should be exactly like your block manager except it should track "local" blocks rather than all blocks in the memory pool.

渔村楼浪 2024-10-04 16:32:11

我的一些想法:

  • 通过让对存储的所有请求都通过一个控制器/管理器来添加到您的存储架构中,该控制器/管理器抽象对存储的访问(可能是处理位图的同一控制器/管理器)。这将允许您对存储进行碎片整理,同时不必担心应用程序的其他部分在碎片整理后有指向错误位置的指针。

  • 可以重写存储系统的规范,以便每个块的一个特定字节用于存储标识“下一个”块的数字(因此每个块只有 31 个字节的有效存储空间)。

Some thoughts off the top of my head:

  • Add on to your storage architecture by having all requests to your storage go through a controller/manager that abstracts access to the storage (could be the same one that handles the bitmap). This would allow you to defragment your storage while not worrying about other parts of the app having pointers to the wrong place after defragmentation.

  • You could rewrite the spec for your storage system so that one specific byte of each block is used to store a number identifying the "next" block (thus having only 31 bytes of effective storage per block).

夜巴黎 2024-10-04 16:32:11

您可以从 10 32 字节空间中的每一个中提取一个字节,并将该字节用作字符串后续部分的索引。这实际上是一个链表,您可以通过向前和向后索引使其成为双向链表。

You could a byte from the each of the 10 32 byte spaces and used that byte as an index to the continuation of the string. This would actually be a linked list, and you could make it a doubly linked list by having the forward and backward indexes.

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