malloc realloc和指针 [英] malloc realloc and pointers

查看:69
本文介绍了malloc realloc和指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我对C相对较新。我对malloc()的查询很少:


1.当我们执行malloc(),动态分配的内存来自

关注进程的堆区域。那么,我们就说堆

已经缩小了。我的查询是:是不是堆物理上没有收缩,但是特定节点被标记为''ALLOCATED''并且对于

后续调用malloc()的内存经理记得他们并且确实没有参考他们吗?


2.使用realloc(),如果某个指针''ptr''最初指向一个

缓冲区中的特定位置(char *缓冲区)然后在这个缓冲区上执行一个

realloc(),'ptr'指向什么?


3.我们可以动态分配的最大内存大小是什么?

调用malloc()?


4.是它在C中有效来强制指针?例如。代码片段...

course int是16 bit,long是32 bit。

int * variable,value;

*(( long *)变量)++ = value;

*((long *)变量)++ = value;

*((long *)变量)++ =价值;

*((长*)变量)++ =价值;


提前预付

Hi all,

I m relatively new to C. I have few queries related to malloc():

1. When we perform malloc(), the memory allocated dynamically comes from
the heap area of the process in concern. Well, we then say that the heap
has shrinked. my query is: Is it that the heap physically does not
shrink but the perticular nodes are marked ''ALLOCATED'' and for
subsequent calls to malloc() the memory manager remembers them and does
not reference them?

2. With realloc(), if some pointer ''ptr'' is pointing initially to a
perticular position in a buffer (char *buffer) then on performing a
realloc() on this buffer, what will be ''ptr'' pointing to?

3. whats the maximum memory size that we can allocate dynamically by
calling malloc() ?

4. Is it valid in C to typecast a pointer? eg. code snippet... of
course int is 16 bit and long is 32 bit.
int *variable, value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;

thanx in advance

推荐答案

ravi< nos ... @nospam.invalidwrote:
ravi <nos...@nospam.invalidwrote:

大家好,


我相对较新C.
Hi all,

I m relatively new to C.



那么为什么要询问它是如何实现的呢?你可以在你爬行之前试图运行

Then why ask questions on how it''s implemented? You''re
trying to run before you can crawl.


我几乎没有与malloc()相关的查询:


1.当我们执行malloc()时,分配的内存动态来自

关注过程的堆区域。
I have few queries related to malloc():

1. When we perform malloc(), the memory allocated
dynamically comes from the heap area of the process in
concern.



如果你这样说的话。语言标准没有规定如何实现malloc的实现,只是它必须如何操作。

If you say so. The language standard does not specify how
malloc is implemented, merely how it must operate.


好​​吧,我们再说堆有收缩。我的查询

是:堆是否物理上没有收缩但

特定节点被标记为''ALLOCATED''和

后续调用malloc()内存管理器

会记住它们并且不引用它们吗?
Well, we then say that the heap has shrinked. my query
is: Is it that the heap physically does not shrink but
the perticular nodes are marked ''ALLOCATED'' and for
subsequent calls to malloc() the memory manager
remembers them and does not reference them?



你应该在一个小组中询问这样的实施细节

是主题。

You should ask in a group where such implementation details
are topical.


2.使用realloc(),如果某个指针''ptr''指向

最初到缓冲区中的某个位置(char

* buffer)那么在这个缓冲区上执行一个realloc(),

''ptr'指向什么?
2. With realloc(), if some pointer ''ptr'' is pointing
initially to a perticular position in a buffer (char
*buffer) then on performing a realloc() on this buffer,
what will be ''ptr'' pointing to?



如果

分配成功,原始指针值将变得不确定。

The original pointer value becomes indeterminate if the
allocation is successful.


3.我们可以通过调用malloc()来动态分配最大内存大小


3. whats the maximum memory size that we can allocate
dynamically by calling malloc() ?



你可以尝试的最多是((size_t)-1)。你实际分配的最多可以实际分配取决于实现。

The most you can try for is ((size_t) -1). The most you
can actually allocate depends on the implementation.


4.在C中是否有效转换指针?
4. Is it valid in C to typecast a pointer?



是的,但需遵守一些规则。转换

不同的非void类型没有定义为

某些程序代码会让你相信。

Yes, but subject to a few rules. Converting between
different non-void types isn''t as well defined as
some program code would have you believe.


例如。代码片段...

course int是16位,long是32位。
eg. code snippet... of
course int is 16 bit and long is 32 bit.



如果这有任何相关性,那么你在学习C的过程中沿着

错误的路径前进。

If that has any relevance, then you''re heading down the
wrong path in your quest to learn C.


int * variable,value;

*((long *)variable)++ = value;
int *variable, value;
*((long*)variable)++ = value;



这可能会破坏指针转换的几条规则。

它也违反了约束。


从int *到long *的强制转换不需要很好

定义。即使它是,它也不会产生适用于postfix ++的可修改的

左值。


-

Peter

This potentially breaks several rules on pointer conversion.
It also violates a constraint.

The cast from int * to long * is not required to be well
defined. Even if it is, it will not produce a modifiable
lvalue suitable for postfix ++.

--
Peter


ravi写道:
ravi wrote:

>

大家好,


我对C相对较新。我有几个与malloc()相关的查询:


1.当我们执行malloc()时,动态分配的内存来自

关注过程的堆区域。
>
Hi all,

I m relatively new to C. I have few queries related to malloc():

1. When we perform malloc(), the memory allocated dynamically comes from
the heap area of the process in concern.



从技术上讲,C没有定义堆。但是,这是一个术语,一般用来表示malloc和朋友从中获取内存的记忆。

Technically, C doesn''t define "heap". However, it is a term in
general use to mean "the memory from which malloc and friends
get their memory".


好​​吧,我们再说堆

已经缩小了。
Well, we then say that the heap
has shrinked.



不一定。也许

堆上可用的内存量已经缩小,但即便如此也不一定如此。你要求的内存在

现有堆中是不可用的,因此可能会增加堆,所以堆成长以适应你的

请求。结果可能是你现在在堆上可用的内存多于调用

malloc()之前的内存。

Not necessarily. Perhaps the amount of memory available on the
heap has shrunk, but even that''s not necessarily true. It is
possible that the memory you request wasn''t available in the
existing heap, and so the heap was grown to accomodate your
request. It is possible that the result is that you now have
more memory available on the heap than before the call to
malloc().


我的查询是:是否物理上没有收缩
收缩但是特定节点被标记为''ALLOCATED''和

后续调用malloc()内存管理器会记住它们并且确实没有引用它们吗?
my query is: Is it that the heap physically does not
shrink but the perticular nodes are marked ''ALLOCATED'' and for
subsequent calls to malloc() the memory manager remembers them and does
not reference them?



malloc / calloc / etc中的内存管理。跟踪

使用的内存和仍然可用的内存。详细信息

如何做到这完全取决于实现。

The memory management within malloc/calloc/etc. keeps track of
what memory is used and that is still available. The details on
How it does this is entirely up to the implemenation.


2.使用realloc(),如果有一些指针'' ptr''最初指向缓冲区(char *缓冲区)中的某个位置,然后在此缓冲区上执行

realloc(),这将是'' ptr''指着?
2. With realloc(), if some pointer ''ptr'' is pointing initially to a
perticular position in a buffer (char *buffer) then on performing a
realloc() on this buffer, what will be ''ptr'' pointing to?



三种可能性:


1)realloc()返回相同的指针,在这种情况下ptr仍然是

有效,指向新大小的缓冲区。


2)realloc()返回不同的指针,在这种情况下ptr是

不再有效,不得解除引用。


3)realloc()返回NULL,在这种情况下,ptr指向

未更改的内存区域为之前。


通常,旧指针的唯一用途是记住当realloc()返回NULL时它指向的地方

。否则,只需使用

新指针。

Three possibilities:

1) realloc() returns the same pointer, in which case ptr is still
valid, pointing to the newly-sized buffer.

2) realloc() returns a different pointer, in which case ptr is
no longer valid, and must not be dereferenced.

3) realloc() returns NULL, in which case ptr is pointing to the
unchanged memory region as before.

Typically, the only use for the old pointer is to remember where
it pointed to when realloc() returns NULL. Otherwise, simply use
the new pointer.


3.我们可以动态分配的最大内存大小是什么? b $ b调用malloc()?
3. whats the maximum memory size that we can allocate dynamically by
calling malloc() ?



无论size_t变量的最大值是多少,尽管你的b $ b更有可能遇到正在运行的平台的限制

on。 (例如,size_t可能允许值高达4GB-1,但

O / S可能只允许2GB。)

Whatever is the maximum value of a size_t variable, though you
are more likely to run into limits of the platform you are running
on. (For example, size_t may allow values up to 4GB-1, but the
O/S may only allow 2GB.)


4在C中使用指针进行类型转换是否有效?例如。代码片段...

course int是16 bit,long是32 bit。

int * variable,value;

*(( long *)变量)++ = value;

*((long *)变量)++ = value;

*((long *)变量)++ =值;

*((long *)变量)++ = value;
4. Is it valid in C to typecast a pointer? eg. code snippet... of
course int is 16 bit and long is 32 bit.
int *variable, value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;



如果变量正确对齐,我相信它是有效的。


-

+ ------------------------- + -------------------- + ----------------------- +

| Kenneth J. Brody | www.hvcomputer.com | #include |

| kenbrody / at\spamcop.net | www.fptech.com | < std_disclaimer.h |

+ ------------------------- + --------- ----------- + ----------------------- +

不要 - 邮寄给我:< mailto:Th ************* @ gmail.com>

If variable is properly aligned, I believe it''s valid.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>


文章< 47***************@spamcop.net>,

Kenneth Brody< ke ****** @ spamcop.netwrote:
In article <47***************@spamcop.net>,
Kenneth Brody <ke******@spamcop.netwrote:

> ravi写道:
>ravi wrote:


> int * variable ,value;
*((long *)变量)++ = value;
*((long *)变量)++ = value;
*((long *)变量)+ + =值;
*((long *)变量)++ = value;
>int *variable, value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;
*((long*)variable)++ = value;


>如果变量正确对齐,我相信它是有效的。
>If variable is properly aligned, I believe it''s valid.



变量尚未初始化,因此它是一个悬空指针。

-

小心上面代码中的错误;我只是证明它是正确的,

没有尝试过。 - 唐纳德克努特

variable has not been initialized, so it is a dangling pointer.
--
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth


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

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