sizeof动态数组 [英] sizeof dynamic array

查看:90
本文介绍了sizeof动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




i想知道如何计算动态数组的大小

使用取消引用声明创建,如int * numbers和

通过malloc或calloc分配:numbers =(int

*)malloc(sizeof(int)* 10);


使用sizeof (数字)将返回指针大小...


有可能吗?


D @ nny

-

生活已经很贵了,所以为什么要花钱买昂贵的

软件。

解决方案

" d @ NNY" < no@thanx.nl>在消息中写道

新闻:fZ **************** @ amsnews03-serv.chello.com ...

hi ,

我想知道如何计算动态数组的大小


确定你需要多大,并分配那么多。 br />
使用取消引用声明创建,如int * numbers


这不是''取消引用声明''。它是一个声明

的指针对象。


通过malloc或calloc分配:numbers =(int
*)malloc(sizeof(int )* 10);


永远不要从''malloc()''转换返回值。有关详细信息,请参阅常见问题解答



使用sizeof(数字)将返回指针大小...


是的。因为''数字''是一个指针。

有可能吗?




是的。分配时你知道它的大小。只需保留

该值(即将其存储在''size_t''对象中)。


int * numbers = 0;

size_t sz = 10 * sizeof *数字;


if(numbers = malloc(sz))

{

printf (%lu bytes allocated.\ n,(unsigned long)sz);

/ * do stuff * /

免费(数字);

}


-Mike


D @ nny写道:



我想知道如何计算动态数组的大小
使用取消引用声明创建,如int * numbers和
通过malloc或calloc分配:
numbers =(int *)malloc(sizeof(int)* 10);


猜猜:数组的大小是sizeof(int)* 10。你觉得怎么样?
它是什么。


顺便说一下,你上面的代码行至少在这里不是惯用的。如果您在发布之前潜伏了
潜伏(预期的usenet行为)或在发布之前检查了常见问题解答

(预期的usenet行为),那么你就没有这个。


您不知道每天有多少人违反这两个标准的

规则,只是为了发布来自

malloc的返回值的代码(更正)这里每天好几次),使用硬编码的魔法

数字,在分配语句中使用硬编码数据类型,要求

每周多次回答问题(原样)你的)并没有

包括一个可编辑的例子。


#include< stdlib.h>

int main( void)

{

const number_elements = 10;

int * numbers;

numbers = malloc(number_elements * sizeof * number);

if(!numbers){/ * handle error * /}

else free(numbers);

return 0 ;

}

使用sizeof(数字)将返回指针大小...

有可能吗?




显然,有。如果你能算出要求malloc

的空间,你已经知道了答案。


D @ nny< no @ thanx .NL>潦草地写道:


i想知道如何计算动态数组的大小
使用取消引用声明创建,比如int * numbers和
分配通过malloc或calloc:numbers =(int
*)malloc(sizeof(int)* 10);
使用sizeof(数字)将返回指针大小...
有可能吗?




不在标准C.你''我必须使用特定于实现的技巧。

但是既然你知道分配内存时的大小,为什么不简单地跟踪它?

跟踪它?
< br $> b $ b -

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰-------- \

\ ------------------------------ --------------------------规则! -------- /

要知道我是爱我。

- JIPsoft

hi,

i would like to know how to calculate the size of a dynamic array
created using a dereference declaration like int *numbers and
allocating via malloc or calloc: numbers=(int
*)malloc(sizeof(int)*10);

using sizeof(numbers) will return the pointers size...

is there a possibility?

D@nny
--
life already is expensive, so why waste money on expensive
software.

解决方案

"D@nny" <no@thanx.nl> wrote in message
news:fZ****************@amsnews03-serv.chello.com...

hi,

i would like to know how to calculate the size of a dynamic array
Determine how big you need it to be, and allocate that much.
created using a dereference declaration like int *numbers
That''s not a ''dereference declaration''. Its a declaration
of a pointer object.
and
allocating via malloc or calloc: numbers=(int
*)malloc(sizeof(int)*10);
Never cast the return value from ''malloc()''. See the FAQ
for details.

using sizeof(numbers) will return the pointers size...
Yes. Because ''numbers'' is a pointer.

is there a possibility?



Yes. You know the size when you allocate it. Just retain
that value (i.e. store it in a ''size_t'' object).

int *numbers = 0;
size_t sz = 10 * sizeof *numbers;

if(numbers = malloc(sz))
{
printf("%lu bytes allocated.\n", (unsigned long)sz);
/* do stuff */
free(numbers);
}

-Mike


D@nny wrote:

hi,

i would like to know how to calculate the size of a dynamic array
created using a dereference declaration like int *numbers and
allocating via malloc or calloc: numbers=(int *)malloc(sizeof(int)*10);
Guess what: the size of the array is sizeof(int)*10. What did you think
it was.

BTW, your code line above is not, at least here, idiomatic. Had you
lurked before posting (expected usenet behavior) or checked the FAQ
before posting (expected usenet behavior), you would have no this.

You have no idea how many people everyday violate those two standard
rules every day just to post code that casts the return value from
malloc (corrected several times a day here), uses hard-coded magic
numbers, uses hard-coded data types in allocation statements, asks
questions answered multiple times a week (as is yours), and fails to
include a compilable example.

#include <stdlib.h>
int main(void)
{
const number_elements = 10;
int *numbers;
numbers = malloc(number_elements * sizeof *numbers);
if (!numbers) { /* handle error */ }
else free(numbers);
return 0;
}
using sizeof(numbers) will return the pointers size...

is there a possibility?



Obviously, there is. If you can figure out how much space to ask malloc
for, you already know the answer.


D@nny <no@thanx.nl> scribbled the following:

hi, i would like to know how to calculate the size of a dynamic array
created using a dereference declaration like int *numbers and
allocating via malloc or calloc: numbers=(int
*)malloc(sizeof(int)*10); using sizeof(numbers) will return the pointers size... is there a possibility?



Not in standard C. You''ll have to use implementation-specific tricks.
But since you know the size when you allocate the memory, why not simply
keep track of it?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"To know me IS to love me."
- JIPsoft


这篇关于sizeof动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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