内存设置和内存复制

发布于 2024-08-27 04:06:27 字数 559 浏览 8 评论 0原文

我正在编写一个内存分配器,我需要一种方法来将整数存储在内存块中。该整数将表示块的大小,因此我可以在给定指向开头的指针的情况下导航到末尾。

这是我的测试示例:

// 编辑:为 testInt 声明空间 int* testInt = 新 int;

head_ptr = (char*) malloc(4*1024*1024); // Allocate 4MB

// EDIT: Should have used std::fill and std::copy
memset(head_ptr,23,sizeof(int)); // Set Address head_ptr = 12345

memcpy(testInt,head_ptr,sizeof(int)); // Set testInt = head_ptr

printf("testInt = %i",testInt);

这会在倒数第二行引发分段错误。

我想做的事情有意义吗?

如果是这样,正确的做法是什么?

非常感谢大家的帮助!!问题解决了:-)

I'm writing a memory allocator, and I need a way to store an integer inside of a chunk of memory. This integer will represent the size of the block so I can navigate to the end given the pointer to the beginning.

Here's my test example:

// EDIT: Declared space for testInt
int* testInt = new int;

head_ptr = (char*) malloc(4*1024*1024); // Allocate 4MB

// EDIT: Should have used std::fill and std::copy
memset(head_ptr,23,sizeof(int)); // Set Address head_ptr = 12345

memcpy(testInt,head_ptr,sizeof(int)); // Set testInt = head_ptr

printf("testInt = %i",testInt);

This throws a segmentation fault on the second to last line.

Does what I'm trying to do make sense?

If so, what is the correct approach?

Thank you so much everyone for your help!! Problem solved :-)

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

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

发布评论

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

评论(5

只是一片海 2024-09-03 04:06:27

回答原来的问题

memset(head_ptr,12345,sizeof(int)); // Set Address head_ptr = 12345

不,没有。这会将 head_ptr 的前一个 sizeof(int) 字节设置为 12345,这将会溢出(除非您使用的架构中一个字节超过 8 位)。

memcpy(testInt,head_ptr,sizeof(int)); // Set testInt = head_ptr

什么是 testInt?一个 int*?一个整数?在后一种情况下使用 &testInt。

另外,从你的标签来看,你使用的是 C++ 而不是 C。但是你的代码实际上只是 C,你应该真正使用更安全的 C++ 函数和特性:

  • memset -> std::fill
  • memcpy -> std::copy
  • malloc ->新的
  • printf -> cout 或(更好)Boost::Format

答案到您的编辑

int* testInt; 是一个指向整数变量的指针,但它未初始化:它将指向一个随机内存区域(我们可以将其视为“随机”,无论其意图和目的如何,即使它不是)。

然后,memcpy 将尝试写入您很可能无权访问的随机内存区域,因此这会导致分段错误(这意味着“您无法访问此内存区域”) ”)。

Answer to original question

memset(head_ptr,12345,sizeof(int)); // Set Address head_ptr = 12345

No it doesn't. This sets the first sizeof(int) bytes of head_ptr to 12345, which will overflow (unless you are using an architecture where a byte is more than 8 bits).

memcpy(testInt,head_ptr,sizeof(int)); // Set testInt = head_ptr

What is testInt? An int*? An int? In the latter case use &testInt.

Also it appears from your tags that you are using C++ rather than C. But your code is really just C, you should really use the safer C++ functions and features:

  • memset -> std::fill
  • memcpy -> std::copy
  • malloc -> new
  • printf -> cout or (better) Boost::Format

Answer to your edit

int* testInt; is a pointer to an integer variable but it's not initialized: it will point to a random memory area (we can consider it "random" for all intent and purposes even if it isn't).

memcpy will then try to write to this random memory area to which most likely you don't have access to, and therefore this results in a segmentation fault (that means "you can't access this memory area").

秋心╮凉 2024-09-03 04:06:27

其他人评论了 memset(3)memcpy(3) 的不当使用,所以我将回复分配器问题。

如果您确实致力于用 C++ 构建自定义内存分配器 - 请查看 Alexandrescu 的 现代 C++ 设计。它引导您详细实现小对象分配器。该代码作为 洛基图书馆

伙计,有人喜欢独角兽......:)

Others commented on improper usage of memset(3) and memcpy(3), so I'll reply to the allocator issue.

If you are really in the business of building custom memory allocator in C++ - take a look at Chapter 4 in Alexandrescu's Modern C++ Design. It walks you through implementing small object allocator in detail. The code is available as part of the Loki Library.

Man, somebody likes unicorns ... :)

原谅过去的我 2024-09-03 04:06:27

您从未初始化过 testInt,因此您的 memcpy 调用正在写入谁知道在哪里。

试试这个:

int *testInt = malloc(sizeof(int));

You've never initialized testInt, so your memcpy call is writing to who knows where.

Try this:

int *testInt = malloc(sizeof(int));

只是我以为 2024-09-03 04:06:27

如果 testInt 只是一个“int”,可能是因为您按值传递它,并且它没有改变?

If testInt is just an "int", could it be since you're passing it by value, and it's not getting changed?

半枫 2024-09-03 04:06:27

由于无法看到 testInt 是什么,我无法确定,但我的通灵调试能力表明您需要获取 testInt 而不是 的地址testInt 本身作为 memcpy 的参数。

编辑:看到您现在发布的内容后,您需要在使用 testInt 之前为其分配内存。

Without being able to see what testInt is I can't be sure, but my psychic debugging powers are indicating that you need to take the address of testInt rather than testInt itself as the argument to memcpy.

EDIT: Upon seeing what you've posted now, you need to allocate memory for testInt before using it.

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