关于realloc的更多问题 - 谢谢 [英] More questions on realloc - Thanks

查看:75
本文介绍了关于realloc的更多问题 - 谢谢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢大家的回复。我收集到:


a)如何获得ptr指向的内存区域的大小

realloc(ptr,size)是依赖于实现的。


b)单独使用malloc(),memcpy()和free()

实现realloc(ptr,size),而不知道内存区域的大小ptr指出,

是不可能的。

Thanks everybody for your replies. I gather that:

a) How to obtain the size of the memory region pointed to by ptr in
realloc(ptr, size) is implementation-dependent.

b) Implementing realloc(ptr, size) using malloc(), memcpy() and free()
alone, without knowing the size of the memory region pointed to by ptr, is
just not possible.

推荐答案



James S. Singleton写道:

James S. Singleton wrote:
感谢大家的回复。我收集到:
b)单独使用malloc(),memcpy()和free()
实现realloc(ptr,size),而不知道ptr指向的内存区域的大小,是<只是不可能。
Thanks everybody for your replies. I gather that:
b) Implementing realloc(ptr, size) using malloc(), memcpy() and free()
alone, without knowing the size of the memory region pointed to by ptr, is
just not possible.




当然有可能。效率低下。你可以malloc一个新的

空间,释放旧的(如果malloc成功),并复制所有的电话

到realloc。你失去的是能够重新使用现有空间

,如果新尺寸是<=旧,或者在那里

是额外的可用空间之后当前的块。这可能不是最佳的做事方式,但可能足够好。


-David



Sure it is possible. Just inefficient. You can malloc a new
space, free the old (if malloc succeeds), and copy on all calls
to realloc. What you lose is the ability to reuse the existing space
in cases where the new size is <= the old, or in cases where there
is additional free space after the current block. Which is likely a
less than optimal way to do things, but could be good enough.

-David


David Resnick写道:
David Resnick wrote:
James S. Singleton写道:
James S. Singleton wrote:
感谢大家的回复。我收集到:
b)单独使用malloc(),memcpy()和free()
实现realloc(ptr,size),而不知道ptr指向的内存区域的大小,是<只是不可能。
Thanks everybody for your replies. I gather that:
b) Implementing realloc(ptr, size) using malloc(), memcpy() and free()
alone, without knowing the size of the memory region pointed to by ptr, is
just not possible.



当然有可能。效率低下。你可以malloc一个新的空间,释放旧的(如果malloc成功),并复制所有的呼叫
重新分配。丢失的是在新大小<=旧的情况下,或者在当前块之后有额外可用空间的情况下重用现有空间的能力。这可能不是最佳的做事方式,但可能足够好。



Sure it is possible. Just inefficient. You can malloc a new
space, free the old (if malloc succeeds), and copy on all calls
to realloc. What you lose is the ability to reuse the existing space
in cases where the new size is <= the old, or in cases where there
is additional free space after the current block. Which is likely a
less than optimal way to do things, but could be good enough.



如果你不知道大小的内容,你如何复制内存?旧区?

无法将realloc(ptr,size)作为单独的函数实现,而无需访问内部内存管理结构。如果您不这么认为,请提供代码。
请提供代码。我有一种感觉你在谈论其他东西

完全,但目前还不清楚是什么。


如果你知道这个地区的大小,然后显然复制工作。但OP

明确表示我们不会。


S.


How do you copy memory if you don''t know the size of the old region? There
is no way to implement realloc(ptr, size) as a separate function, without
access to the internal memory management structures. If you think otherwise,
please supply the code. I have a feeling you''re talking about something else
entirely, but it''s unclear what.

If you know the size of the region, then obviously copying works. But the OP
specifically said we don''t.

S.




Skarmander写道:

Skarmander wrote:
David Resnick写道:
David Resnick wrote:
James S. Singleton写道:
James S. Singleton wrote:
感谢大家的回复。我收集到:
b)单独使用malloc(),memcpy()和free()
实现realloc(ptr,size),而不知道ptr指向的内存区域的大小,是<只是不可能。
Thanks everybody for your replies. I gather that:
b) Implementing realloc(ptr, size) using malloc(), memcpy() and free()
alone, without knowing the size of the memory region pointed to by ptr, is
just not possible.



当然有可能。效率低下。你可以malloc一个新的空间,释放旧的(如果malloc成功),并复制所有的呼叫
重新分配。丢失的是在新大小<=旧的情况下,或者在当前块之后有额外可用空间的情况下重用现有空间的能力。这可能不是最佳的做事方式,但可能足够好。



Sure it is possible. Just inefficient. You can malloc a new
space, free the old (if malloc succeeds), and copy on all calls
to realloc. What you lose is the ability to reuse the existing space
in cases where the new size is <= the old, or in cases where there
is additional free space after the current block. Which is likely a
less than optimal way to do things, but could be good enough.


如果你不知道旧区域的大小,你如何复制记忆?有没有办法将realloc(ptr,size)作为一个单独的函数来实现,而无需访问内部内存管理结构。如果您不这么认为,请提供代码。我有一种感觉,你在谈论别的东西
完全,但它不清楚是什么。


How do you copy memory if you don''t know the size of the old region? There
is no way to implement realloc(ptr, size) as a separate function, without
access to the internal memory management structures. If you think otherwise,
please supply the code. I have a feeling you''re talking about something else
entirely, but it''s unclear what.




你是慈善的,假设我''我正在谈论其他事情,

我错了。


-David



You are being charitable assuming I''m talking about something else,
I was just wrong.

-David


这篇关于关于realloc的更多问题 - 谢谢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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