sizeof dinamically alocated array .... [英] sizeof dinamically alocated array....
问题描述
我有char *数组,它是dinamically alocated ....
当我把它传递给其他函数时,我需要确定这个数组的大小....
题:!?!?!sizeof操作符不符合dinamically alocated
排列工作
我怎么能确定dinamically alocated数组的大小.. ..?
请建议......
Thx!
Mateo写道:我有char *数组,它是dinamically alocated ....
当我把它传递给其他函数时,我需要确定这个
数组的大小....问题:sizeof运算符无法使用dinamically
分配的数组!?!?!
如何确定dinamically alocated数组的大小....?
你不能。您需要在分配时存储大小
并将其传递到同一个函数中。如果你不能这样做,不要使用一个普通的char数组,使用''std :: string''。
V
-
请在邮寄回复时从我的地址删除资金
Mateo写道:
< blockquote class =post_quotes>我有char *数组,它是dinamically alocated ....
当我把它传递给其他函数时,我需要确定这个
数组的大小...... BR />问题:!?!?!?sizeof操作符不符合dinamically alocated
我/>如何确定dinamically alocated数组的大小.... 块引用> <工作br />
这是一个常见问题,但我不知道它是否在FAQ中。
答案是你应该使用std :: vector<>除非你确实需要一个
原始数组,由new []返回。
如果你有正当理由不使用std :: vector<> ,然后传递大小
与阵列。
没有便携的方法来获取这些信息,因为你的程序是
在新[]调用之后基本上抛弃了信息。不要。
-
Phlip
http://www.greencheese.org/ZeekLand < - 不是博客!!!
Phlip写道:
答案是你应该使用std :: vector<>除非你确实需要一个
原始数组,由new []返回。
更不用说传递给需要
裸T *,std :: vector< T>通过
& v [0]可以轻易转换为T *。
I have char *array and it is dinamically alocated....
When I pass it to other function, I need to determine size of this array....
Problem: sizeof operator does not work with dinamically alocated
arrays!?!?!
How can I determine size of dinamically alocated array.... ?
Please advice...
Thx!
Mateo wrote:I have char *array and it is dinamically alocated....
When I pass it to other function, I need to determine size of this
array.... Problem: sizeof operator does not work with dinamically
alocated arrays!?!?!
How can I determine size of dinamically alocated array.... ?
You cannot. You need to store the size at the time of allocation
and pass it into the same function. If you can''t do that, do not
use a plain char array, use ''std::string''.
V
--
Please remove capital As from my address when replying by mail
Mateo wrote:
I have char *array and it is dinamically alocated....
When I pass it to other function, I need to determine size of this
array....
Problem: sizeof operator does not work with dinamically alocated
arrays!?!?!
How can I determine size of dinamically alocated array.... ?
This is a Frequently Asked Question, but I don''t know if it''s in the FAQ.
The answer is you should use an std::vector<> unless you actually need a
primitive array, returned by new[].
If you have a legitimate reason not to use std::vector<>, then pass the size
with the array.
There''s no portable way to get this information, because your program is
essentially throwing the information away after the new[] call. Don''t.
--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Phlip wrote:
The answer is you should use an std::vector<> unless you actually need a
primitive array, returned by new[].
Not to mention that for passing to library/OS routines that require a
naked T*, a std::vector<T> is trivially convertable to a T* by passing
&v[0].
这篇关于sizeof dinamically alocated array ....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!