sizeof([分配的记忆]) [英] sizeof([ALLOCATED MEMORY])

查看:48
本文介绍了sizeof([分配的记忆])的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个指向malloc()的指针,想要读取它就好像它是一个数组,我需要知道我不会读到最后一个索引。


如果这是一个指向指针的指针,一个常见的技术似乎是

设置一个指向列表末尾的NULL指针,这里我们知道

分配的内存已经用完了。好的。


当这是指向另一个类型的指针时,比如int,我可以有一个

变量来记录分配的内存和使用量那个

来跟踪''数组''的大小。

或者,我们可以将''数组'的结尾设置为某种

错误代码,例如99或MAX_INT。

我不喜欢这些技巧。


那么,什么是一个很好的方法来停止循环读取或写入分配给指针的

内存?

或者如果可能的话,确定内存大小的好方法是什么? br />
分配给指针?


干杯,

马特

If I have malloc()''ed a pointer and want to read from it as if it were
an array, I need to know that I won''t be reading past the last index.

If this is a pointer to a pointer, a common technique seems to be
setting a NULL pointer to the end of the list, and here we know that
the allocated memory has been exhausted. All good.

When this is a pointer to another type, say int, I could have a
variable that records how much memory is being allocated and use that
to track the size of the ''array''.
Alternatively, we could set the end of the ''array'' to some kind of
error-code, such as 99 or MAX_INT.
I don''t like either of these techniques.

So, what is a good way to stop a loop reading or writing past the
memory allocated to a pointer?
Or if possible, what is a good way of determining the size of memory
allocated to a pointer?

Cheers,
Matt

推荐答案

ballpointpenthief说:
ballpointpenthief said:
如果我有一个指向malloc()''的指针,想要从中读取它就好像它是
一个数组,我需要知道我不会读过最后一个索引。
If I have malloc()''ed a pointer and want to read from it as if it were
an array, I need to know that I won''t be reading past the last index.



当你分配内存时,你确切地知道你有多少内存请求



Don''忘记(tm)。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



When you allocate the memory, you know precisely how much memory you are
requesting.

Don''t Forget (tm).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


" ballpointpenthief" <马************* @ gmail.com>写道:
"ballpointpenthief" <Ma*************@gmail.com> writes:
那么,什么是阻止循环读取或写入分配给指针的内存的好方法?
或者如果可能的话,什么是确定分配给指针的内存大小的好方法?
So, what is a good way to stop a loop reading or writing past the
memory allocated to a pointer?
Or if possible, what is a good way of determining the size of memory
allocated to a pointer?




你的代码分配了内存。

你的代码应该记住分配了多少。


-

克里斯。



Your code allocated the memory.
Your code should remember how much it allocated.

--
Chris.




" Chris McDonald" < CH *** @ csse.uwa.edu.au>写了

"Chris McDonald" <ch***@csse.uwa.edu.au> wrote
" ballpointpenthief" <马************* @ gmail.com>写道:
"ballpointpenthief" <Ma*************@gmail.com> writes:
那么,什么是阻止循环读取或写入分配给指针的内存的好方法?
或者如果可能的话,什么是确定分配给指针的内存大小的好方法?
So, what is a good way to stop a loop reading or writing past the
memory allocated to a pointer?
Or if possible, what is a good way of determining the size of memory
allocated to a pointer?



你的代码分配了内存。
你的代码应该记住分配了多少。



Your code allocated the memory.
Your code should remember how much it allocated.




如果我理解我今天学到的东西,如果有一个不同的模块,那么

比其中的那个

p = malloc(某个数字);

被声明,其中内存是一个燃烧的问题,然后一个来源

需要声明并通过。 Joe

---------

如果华盛顿没有穿越特拉华州,那么一个人昨天对我说,

___,然后我们将向女王纳税。

但我喜欢伊丽莎白。



If I understand what I''ve learned today, if one has a differing module other
than the one in which
p = malloc(some number);
is declared and in which memory is a burning question, then ones source
needs to declare and pass. Joe
---------
A guy says to me yesterday, if Washington hadn''t crossed the Delaware in
___, then we would be paying taxes to the Queen.
But I like Elizabeth.


这篇关于sizeof([分配的记忆])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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