resizable array of pointers
  Home FAQ Contact Sign in
comp.lang.c++ only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.c++ Profile…
 Up
resizable array of pointers         


Author: Mark
Date: Oct 5, 2006 14:44

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
Show full article (1.19Kb)
12 Comments
Re: resizable array of pointers         


Date: Oct 5, 2006 15:59

"Mark" gmail.com> wrote in message
news:1160084668.203213.75030@c28g2000cwb.googlegroups.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*))
Show full article (2.31Kb)
no comments
Re: resizable array of pointers         


Author: peter koch
Date: Oct 5, 2006 16:16

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
no comments
Re: resizable array of pointers         


Author: Ole Nielsby
Date: Oct 5, 2006 17:22

Mark gmail.com> wrote:
> 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

You might want to use a vector of the Standard
Template Library (STL). Vectors are essentially resizeable arrays.

Your online docs - or STL tutorials - will have vector examples
you can work from.

At first glimse, it can seem a bit strange that you have to deal
with iterators instead of pointers, but the iterators are really just
pointers that are dressed up fancy for mating with STL algorithms.

HTH/ON
no comments
Re: resizable array of pointers         


Author: Mark
Date: Oct 5, 2006 18:09

peter koch wrote:
>
> Sure... std::vector<>
>
> /Peter

Mark wrote:
> i can't use any special classes

i was referring specifically to vectors and such.
but thanks for trying.
no comments
Re: resizable array of pointers         


Author: Mark
Date: Oct 5, 2006 18:24

David W wrote:
> "Mark" gmail.com> wrote in message
> news:1160084668.203213.75030@c28g2000cwb.googlegroups.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
> ...
Show full article (3.85Kb)
no comments
Re: resizable array of pointers         


Author: Mark
Date: Oct 5, 2006 18:30

Mark wrote:
> David W wrote:
>> "Mark" gmail.com> wrote in message
>> news:1160084668.203213.75030@c28g2000cwb.googlegroups.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 ...
Show full article (4.50Kb)
1 Comment
Re: resizable array of pointers         


Author: Thomas J. Gritzan
Date: Oct 5, 2006 18:35

Mark schrieb:
> hahaha..
>
> oops.
>
> forgot the two most important lines.
>
> void ObStash::inflate()
> {
> cout << "inflating";
> length += increment;
> void** temp = new void*[length];
> for(int i=0; i > {
> temp[i] = storage[i];
> }
> delete [] storage;
> storage = temp;
> cout << "inflated";
> } ...
Show full article (0.66Kb)
no comments
Re: resizable array of pointers         


Author: Mark
Date: Oct 5, 2006 18:42

David W wrote:
> "Mark" gmail.com> wrote in message
> news:1160084668.203213.75030@c28g2000cwb.googlegroups.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
> ...
Show full article (3.04Kb)
no comments
Re: resizable array of pointers         


Author: Mark
Date: Oct 5, 2006 18:47

Thomas J. Gritzan wrote:
> Mark schrieb:
>> hahaha..
>>
>> oops.
>>
>> forgot the two most important lines.
>>
>> void ObStash::inflate()
>> {
>> cout << "inflating";
>> length += increment;
>> void** temp = new void*[length];
>> for(int i=0; i >> {
>> temp[i] = storage[i];
>> }
>> delete [] storage;
>> storage = temp;
>> cout << "inflated"; ...
Show full article (3.32Kb)
no comments

RELATED THREADS
SubjectArticles qty Group
how to join array into arraycomp.lang.perl.misc ·
1 2