可调整大小的指针数组 [英] resizable array of pointers

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

问题描述

我需要创建一个可以存储任何东西的类(整数,对象等)

它需要维护一个指向这些对象的指针列表

列表需要要调整大小以容纳更多元素

i不能使用任何特殊课程


i似乎无法弄明白。


我有一个指针,


无效*存储


这将指向一个开始列表


然后会有一个指针,每4个字节(sizeof(void *))


我认为*我可以通过执行访问


存储[index * sizeof(void *)]


我不确定是否需要将其转换为一个无效的指针,因为

'已经被定义为......


然后调整这个野兽的大小,我我完全失去了


i创建一个新的无效指针..


void * temp = new void * [length]


其中长度是新的si ze(它将持有多少指针)


哪个应该分配长度* 4个字节的内存...


然后我试着做


for(int i = 0; I<计数; i ++)

{

temp [i] =存储[i * size];

}


复制指针

其中count是当前藏匿的元素数量,但是我的
编译器抱怨......


`void *''不是指针对象类型

类型为void *的指针用于算术


和......我真的不知道还能做什么。


任何帮助?

解决方案
"马克" < mn ******* @ gmail.comwrote in message

news:11 ********************* @ c28g2000cwb .googlegro ups.com ...


i需要创建一个可以存储任何内容的类(整数,对象等)

它需要什么维护这些对象的指针列表

列表需要调整大小以容纳更多元素

i不能使用任何特殊类

i似乎无法弄明白。


我有一个指针,


void *存储



模板会好得多。然后你可以存储几乎任何类型T的T *数组。如果你使用

void *你需要在读取数组时从void *转换为正确的类型,这是

太可怕了。无论如何,我会继续你的非模板版本。


这将指向列表的开头



它应该是无效的**存储,而不是无效*。请记住,数组的元素本身是void *,

和''storage''是指向第一个的指针,所以它是无效的**。


然后会有一个指针,每4个字节(sizeof(void *))



忘记字节。你不需要考虑''em。


我认为*我可以通过这样做来访问


存储[index * sizeof(void *)]



不,你不能取消引用空白*,因为那样你就会失效,这不会不存在并且没有

定义的大小来进行算术运算。如果'存储''无效**,它无需算术就可以工作,因为

解引用会给出void *,它确实存在并且有一个大小,即存储[index]就是你所有的>
需要。


我不确定是否需要将其转换为无效指针,因为

,它已经被定义为......



但不应该将其定义为。


然后调整这个野兽的大小,我完全迷失了


i创建一个新的无效指针..


void * temp = new void * [length]



No,void ** temp = new void * [length];


其中length是新大小(它将保持多少指针)


应该分配长度* 4个字节的内存...



忘记字节。


然后我尝试为(b i = 0; I<计数; i ++)

{

temp [i] = storage [i * size];



temp [i] = storage [i];


}

将指针复制到

,其中count是当前藏匿的元素数量,但是我的
编译器抱怨......


`void *''不是指针对象类型



这是正确的。再次,void **将解决这个问题。


类型为void *的指针用于算术


和。 ..我真的不知道还能做什么。


有什么帮助吗?



DW


" David W" < no@email.providedwrote在消息新闻中写道:Wd ******************* @ nasal.pacific.net.au ...


" Mark" < mn ******* @ gmail.comwrote in message

news:11 ********************* @ c28g2000cwb .googlegro ups.com ...


i需要创建一个可以存储任何内容的类(整数,对象等)

它需要什么维护这些对象的指针列表

列表需要调整大小以容纳更多元素

i不能使用任何特殊类

i似乎无法弄明白。


我有一个指针,


void *存储



模板会好得多。然后你可以存储几乎任何类型T的T *数组。如果你



使用


void *你需要在读取数组时从void *转换为正确的类型,这是非常可怕的。无论如何,我会继续你的非模板版本。



我在这里假设在给定的数组中指针都是相同的类型。如果相同的数组

有指向不同类型的指针,那么模板将无法正常工作。当然,有了一系列的void *到

a混合类型,你总是要知道哪个void *指向哪种类型并且在你的时候适当地施放它b / b
阅读它。


DW




Mark skrev:


i需要创建一个可以存储任何东西的类(整数,对象等)

它需要维护一个指向这些对象的指针列表

列表需要调整大小以容纳更多元素

i不能使用任何特殊类


i似乎无法弄明白。



[snip]


>

任何帮助?



当然...... std :: vector<>


/ Peter


i need to make a class that can store anything (integers,objects,etc)
it needs to maintain a list of pointers to these objects
the list needs to be resized to accomodate more elements
i can''t use any special classes

i just can''t seem to figure it out.

i''ve got a pointer,

void *storage

which will point to the start of the list

then there will be one pointer, every 4 bytes (sizeof(void*))

which i *think* i can access by doing

storage[index*sizeof(void*)]

i''m not sure if I need to cast that to a void pointer or not, since
that''s already what it''s defined as...

but then to resize this beast, i''m completely lost

i create a new void pointer..

void* temp = new void*[length]

where length is the new size (how many pointers it will hold)

which should allocate length*4 bytes of memory...

and then i tried doing

for(int i=0; i<count; i++)
{
temp[i] = storage[i*size];
}

to copy the pointers over
where count is the number of elements currently in the stash, but my
compiler complains...

`void*'' is not a pointer-to-object type
pointer of type `void *'' used in arithmetic

and...i don''t really know what else to do.

any help?

解决方案

"Mark" <mn*******@gmail.comwrote in message
news:11*********************@c28g2000cwb.googlegro ups.com...

i need to make a class that can store anything (integers,objects,etc)
it needs to maintain a list of pointers to these objects
the list needs to be resized to accomodate more elements
i can''t use any special classes

i just can''t seem to figure it out.

i''ve got a pointer,

void *storage

A template would be much better. Then you can store an array of T* for almost any type T. If you use
void* you''ll need to cast from void* to the correct type whenever you read the array, which is
pretty horrible. Anyway, I''ll press on with your non-template version.

which will point to the start of the list

It should be void **storage, not void*. Remember, the elements of the array are themselves void *,
and ''storage'' is a pointer to the first one, so it''s void **.

then there will be one pointer, every 4 bytes (sizeof(void*))

Forget bytes. You don''t need to think about ''em.

which i *think* i can access by doing

storage[index*sizeof(void*)]

No, you can''t dereference a void *, because then you''d get void, which doesn''t exist and has no
defined size to do arithmetic on. It works without arithmetic if ''storage'' is void**, since
dereferencing that gives void*, which does exist and has a size, i.e., storage[index] is all you
need.

i''m not sure if I need to cast that to a void pointer or not, since
that''s already what it''s defined as...

It shouldn''t be defined as that, though.

but then to resize this beast, i''m completely lost

i create a new void pointer..

void* temp = new void*[length]

No, void **temp = new void*[length];

where length is the new size (how many pointers it will hold)

which should allocate length*4 bytes of memory...

Forget bytes.

and then i tried doing

for(int i=0; i<count; i++)
{
temp[i] = storage[i*size];

temp[i] = storage[i];

}

to copy the pointers over
where count is the number of elements currently in the stash, but my
compiler complains...

`void*'' is not a pointer-to-object type

That''s right. Again, void** would fix that.

pointer of type `void *'' used in arithmetic

and...i don''t really know what else to do.

any help?

DW


"David W" <no@email.providedwrote in message news:Wd*******************@nasal.pacific.net.au...

"Mark" <mn*******@gmail.comwrote in message
news:11*********************@c28g2000cwb.googlegro ups.com...

i need to make a class that can store anything (integers,objects,etc)
it needs to maintain a list of pointers to these objects
the list needs to be resized to accomodate more elements
i can''t use any special classes

i just can''t seem to figure it out.

i''ve got a pointer,

void *storage


A template would be much better. Then you can store an array of T* for almost any type T. If you

use

void* you''ll need to cast from void* to the correct type whenever you read the array, which is
pretty horrible. Anyway, I''ll press on with your non-template version.

I was assuming here that in a given array the pointers are all to the same type. If the same array
has pointers to different types then a template wouldn''t work. Of course, with an array of void * to
a mix of types you''d always have to know which void * pointed to which type and cast it
appropriately when you read it.

DW



Mark skrev:

i need to make a class that can store anything (integers,objects,etc)
it needs to maintain a list of pointers to these objects
the list needs to be resized to accomodate more elements
i can''t use any special classes

i just can''t seem to figure it out.

[snip]

>
any help?

Sure... std::vector<>

/Peter


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

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