的malloc [英] Malloc
问题描述
此代码是否正确
char * str =(char *)malloc(10 * sizeof(char));
char [0] = ...
char [1] = ...
....
....
...
char [9] =''\''';
malloc()不对<}的连续性做出任何保证br />
内存。在那种情况下,代码是否正确?我们可以假设
字符在内存中是连续的。
Is this code correct
char *str = (char *)malloc(10 * sizeof(char));
char[0] = ...
char [1] = ...
....
....
...
char [9] = ''\0'';
malloc() does not make any guarantees regarding the contiguity of
memory. In that case, is ths code correct? Can we assume that the
characters are contiguous in memory.
推荐答案
pavan< pa ** ******@gmail.comwrote:
pavan <pa********@gmail.comwrote:
malloc()不对
内存的连续性做出任何保证。
malloc() does not make any guarantees regarding the contiguity of
memory.
为什么你认为是这种情况?
-
:wq
^ X ^ Cy ^ K ^ X ^ C ^ C ^ C ^ C
Why do you think this is the case ?
--
:wq
^X^Cy^K^X^C^C^C^C
2月3日上午11:15,Ico<使用... @ zeev.nlwrote:
On Feb 3, 11:15 am, Ico <use...@zeev.nlwrote:
pavan< pavan.m ... @ gmail.comwrote:
pavan <pavan.m...@gmail.comwrote:
malloc()不对
内存的连续性做出任何保证。
malloc() does not make any guarantees regarding the contiguity of
memory.
为什么你认为是这种情况?
-
:wq
^ X ^ Cy ^ K ^ X ^ C ^ C ^ C ^ C
Why do you think this is the case ?
--
:wq
^X^Cy^K^X^C^C^C^C
如果我创建一个包含多个内存的内存块
相同类型的元素,我想知道我是否可以访问ptr,ptr + 1,
ptr + 2等元素...我应该使用vmalloc ()而不是malloc如果我想这样做
这个?
if i create a chunk of memory containing a number of elements of the
same type, i want to know if i can access the elements as ptr, ptr+1,
ptr+2 etc... Should I use vmalloc() instead of malloc if i want to do
this?
pavan写道:
pavan wrote:
2月3日上午11点15分,Ico< use ... @ zeev.nlwrote:
On Feb 3, 11:15 am, Ico <use...@zeev.nlwrote:
pavan< pavan。 m ... @ gmail.comwrote:
pavan <pavan.m...@gmail.comwrote:
malloc()不对
内存的连续性做出任何保证。
malloc() does not make any guarantees regarding the contiguity of
memory.
为什么你认为是这种情况?
Why do you think this is the case ?
如果我创建一个包含
相同类型元素的内存块,我想知道我是否可以访问元素为ptr,ptr + 1,
ptr + 2等...
if i create a chunk of memory containing a number of elements of the
same type, i want to know if i can access the elements as ptr, ptr+1,
ptr+2 etc...
是的,你可以。
Yes, you can.
如果我想这样做,我应该使用vmalloc()而不是malloc吗?
Should I use vmalloc() instead of malloc if i want to do this?
编号vmalloc()是一个非标准函数。 malloc()很好。
No. vmalloc() is a non-standard function. malloc() is fine.
这篇关于的malloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!