473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

resizable array of pointers

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?

Oct 5 '06 #1
19 2456
"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
Oct 5 '06 #2
"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
Oct 5 '06 #3

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

Oct 5 '06 #4

Mark <mn*******@gmail.comwrote:
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<void*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
Oct 6 '06 #5

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.

Oct 6 '06 #6

David W wrote:
"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
ahh.. i tried a double pointer the first time around, which compiled,
but then i ran into another problem down the road.

i thought i might just be able to just have a pointer to the start of
the list, but i guess what i had was a pointer to a list of voids,
which don't have a size, when i needed a pointer to a list of void*'s..
kinda tough to think about when you have all these asterisks and
brackets laying around.

but thank you, this really helped clear things up. much appreciated.

void ObStash::inflate()
{
cout << "inflating";
length += increment;
void** temp = new void*[length];
for(int i=0; i<count; i++)
{
temp[i] = storage[i];
}
cout << "inflated";
}

now i'm having a problem adding to the list (it crashes after printing
"inflated")

int ObStash::add(void *p)
{
if( count >= length ) inflate();
storage[count] = p;
count++;
}

but, i'll see if i can work this out first before i ask any more
questions.

the reason i was so fixated on size was because i had previously made a
stash where all the data was copied into the list, so it was very
dependant on the size of the object.

i agree that templates would be a much better solution..but..that isn't
an option right now.

the lists actually aren't mixed btw :) it would be crazy to try and
keep track of what each thing was pointing to..

Oct 6 '06 #7

Mark wrote:
David W wrote:
"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

ahh.. i tried a double pointer the first time around, which compiled,
but then i ran into another problem down the road.

i thought i might just be able to just have a pointer to the start of
the list, but i guess what i had was a pointer to a list of voids,
which don't have a size, when i needed a pointer to a list of void*'s..
kinda tough to think about when you have all these asterisks and
brackets laying around.

but thank you, this really helped clear things up. much appreciated.

void ObStash::inflate()
{
cout << "inflating";
length += increment;
void** temp = new void*[length];
for(int i=0; i<count; i++)
{
temp[i] = storage[i];
}
cout << "inflated";
}

now i'm having a problem adding to the list (it crashes after printing
"inflated")

int ObStash::add(void *p)
{
if( count >= length ) inflate();
storage[count] = p;
count++;
}

but, i'll see if i can work this out first before i ask any more
questions.

the reason i was so fixated on size was because i had previously made a
stash where all the data was copied into the list, so it was very
dependant on the size of the object.

i agree that templates would be a much better solution..but..that isn't
an option right now.

the lists actually aren't mixed btw :) it would be crazy to try and
keep track of what each thing was pointing to..
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<count; i++)
{
temp[i] = storage[i];
}
delete [] storage;
storage = temp;
cout << "inflated";
}

(delete, and storage = temp)

i am deleting it properly, right?

Oct 6 '06 #8
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<count; i++)
{
temp[i] = storage[i];
}
delete [] storage;
storage = temp;
cout << "inflated";
}

(delete, and storage = temp)

i am deleting it properly, right?
You forgot the class definition and all the other things. How we are
supposted to know what the class is doing and what could be wrong?

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Oct 6 '06 #9

David W wrote:
"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
okay..maybe you can help me here,

the names are printing out properly, but the numbers (scores) are
garbage.

void Student::print()
{
cout << " name: " << name << endl;
int sum = 0;
for(int i=0; i<scores.getCount(); i++)
{
int score = *(int*)scores.fetch(i);
cout << " score" << i+1 << ": " << score << endl;
sum += score;
}
if( scores.getCount() 0 )
cout << " gpa: " << sum/(float)scores.getCount() << endl;
else
cout << " no scores" << endl;
}

fetched like so

void* ObStash::fetch(int index)
{
return storage[index];
}

Oct 6 '06 #10

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<count; i++)
{
temp[i] = storage[i];
}
delete [] storage;
storage = temp;
cout << "inflated";
}

(delete, and storage = temp)

i am deleting it properly, right?

You forgot the class definition and all the other things. How we are
supposted to know what the class is doing and what could be wrong?

--
Thomas
http://www.netmeister.org/news/learn2quote.html
that part seems to be working (or at least not crashing)
storage is defined as
void** storage;
i'm just asking if that will delete all the pointers in storage,
without leaving anything behind.

but, if you really want to see the class definition

// opstash.h
#ifndef OBSTASH_H
#define OBSTASH_H

const int increment = 100;
const int size = sizeof(void*);

class ObStash
{
int length;
int count;
void** storage;

public:
ObStash();
int add(void* p);
void* fetch(int index);
~ObStash();
int getCount();
void inflate();
};
#endif
// obstash.cpp
#include "ObStash.h"
#include <iostream>
#include "Student.h"
//#include <cassert>
using namespace std;

ObStash::ObStash() : count(0), length(0)
{}

ObStash::~ObStash()
{
// delete all the pointers...
}

void ObStash::inflate()
{
length += increment;
void** temp = new void*[length];
for(int i=0; i<count; i++)
{
temp[i] = storage[i];
}
delete [] storage;
storage = temp;
}

int ObStash::add(void *p)
{
if( count >= length ) inflate();
storage[count] = p;
cout << count << ":" << storage[count] << endl;
count++;
}

void* ObStash::fetch(int index)
{
return storage[index];
}

int ObStash::getCount() {
return count;
}

andddd...since i'll be adding students (as well as integers) to this
stash...

// student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
#include "ObStash.h"
using namespace std;

class Student
{
string name;
ObStash scores;

public:
Student(string name);
void print();
void cleanUp();
};

#endif

// student.cpp
#include "student.h"
#include <iostream>

using namespace std;

Student::Student(string name) : name(name)
{
int score;
while(1)
{
cout << " score" << scores.getCount()+1 << ": ";
cin >score;
if( cin.fail() )
{
cin.clear();
string s;
getline(cin,s);
cout << " * bad input" << endl;
continue;
}
if( score == -1 )
break;
else if( score < 1 || score 5 ) {
cout << " * wrong input" << endl;
continue;
}
scores.add(&score);
};
}

void Student::print()
{
cout << " name: " << name << endl;
int sum = 0;
for(int i=0; i<scores.getCount(); i++)
{
int score = *(int*)scores.fetch(i);
cout << " score" << i+1 << ": " << score << endl;
sum += score;
}
if( scores.getCount() 0 )
cout << " gpa: " << sum/(float)scores.getCount() << endl;
else
cout << " no scores" << endl;
}

Oct 6 '06 #11
"Mark" <mn*******@gmail.comwrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
>

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<count; i++)
{
temp[i] = storage[i];
}
delete [] storage;
storage = temp;
cout << "inflated";
}

(delete, and storage = temp)

i am deleting it properly, right?
Yes. new[] requires delete[].

DW

Oct 6 '06 #12
"Mark" <mn*******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>

okay..maybe you can help me here,

the names are printing out properly, but the numbers (scores) are
garbage.

void Student::print()
{
cout << " name: " << name << endl;
int sum = 0;
for(int i=0; i<scores.getCount(); i++)
{
int score = *(int*)scores.fetch(i);
cout << " score" << i+1 << ": " << score << endl;
sum += score;
}
if( scores.getCount() 0 )
cout << " gpa: " << sum/(float)scores.getCount() << endl;
else
cout << " no scores" << endl;
}

fetched like so

void* ObStash::fetch(int index)
{
return storage[index];
}
My guess is that the numbers you are putting in there no longer exist, but you didn't include that
code. If the ints are ordinary local variables they may have gone out of scope. You may need to
create each int to be stored with 'new' if that's the case (and delete later).

DW
Oct 6 '06 #13

David W wrote:
"Mark" <mn*******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...


okay..maybe you can help me here,

the names are printing out properly, but the numbers (scores) are
garbage.

void Student::print()
{
cout << " name: " << name << endl;
int sum = 0;
for(int i=0; i<scores.getCount(); i++)
{
int score = *(int*)scores.fetch(i);
cout << " score" << i+1 << ": " << score << endl;
sum += score;
}
if( scores.getCount() 0 )
cout << " gpa: " << sum/(float)scores.getCount() << endl;
else
cout << " no scores" << endl;
}

fetched like so

void* ObStash::fetch(int index)
{
return storage[index];
}

My guess is that the numbers you are putting in there no longer exist, but you didn't include that
code. If the ints are ordinary local variables they may have gone out of scope. You may need to
create each int to be stored with 'new' if that's the case (and delete later).

DW
okay, that worked like a charm!

now..last question..i hope.

i've got my destructor

ObStash::~ObStash()
{
delete [] storage;
}

but will that be sufficient to prevent leakage?

since it's an array of pointers. i figure i would have to delete each
pointer first, and then delete the array, since it's declared as void
**storage, however.. i can't delete each pointer because they delete
void* is undefined!

but, from my understanding, it's not necessarily delete integers
anyways..they'll take care of themselves..but the Student class
however, is another story.

class Student
{
string name;
ObStash scores;

public:
Student(string name);
void print();
void cleanUp();
};

the string has it's own destructor i believe, and should clean up after
it's self...

the obstash's destructor should clean up the scores too i think..

but what about when

ObStash students;

goes out of scope?

do i need to call the destructor for each object individually or
anything..? (delete each student manually) or..will they all die
gracefully?

Oct 6 '06 #14

Mark wrote:
David W wrote:
"Mark" <mn*******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>
>
okay..maybe you can help me here,
>
the names are printing out properly, but the numbers (scores) are
garbage.
>
void Student::print()
{
cout << " name: " << name << endl;
int sum = 0;
for(int i=0; i<scores.getCount(); i++)
{
int score = *(int*)scores.fetch(i);
cout << " score" << i+1 << ": " << score << endl;
sum += score;
}
if( scores.getCount() 0 )
cout << " gpa: " << sum/(float)scores.getCount() << endl;
else
cout << " no scores" << endl;
}
>
fetched like so
>
void* ObStash::fetch(int index)
{
return storage[index];
}
My guess is that the numbers you are putting in there no longer exist, but you didn't include that
code. If the ints are ordinary local variables they may have gone out of scope. You may need to
create each int to be stored with 'new' if that's the case (and delete later).

DW

okay, that worked like a charm!

now..last question..i hope.

i've got my destructor

ObStash::~ObStash()
{
delete [] storage;
}

but will that be sufficient to prevent leakage?

since it's an array of pointers. i figure i would have to delete each
pointer first, and then delete the array, since it's declared as void
**storage, however.. i can't delete each pointer because they delete
void* is undefined!

but, from my understanding, it's not necessarily delete integers
anyways..they'll take care of themselves..but the Student class
however, is another story.

class Student
{
string name;
ObStash scores;

public:
Student(string name);
void print();
void cleanUp();
};

the string has it's own destructor i believe, and should clean up after
it's self...

the obstash's destructor should clean up the scores too i think..

but what about when

ObStash students;

goes out of scope?

do i need to call the destructor for each object individually or
anything..? (delete each student manually) or..will they all die
gracefully?
i know i'm spamming, but there's no edit button that i've discovered.

the destructor doesn't seem to be called for each student, when obstash
students goes out of scope

Student::~Student()
{
cout << "killing student " << name << endl;
}

int main(int argc, char *argv[])
{

{
ObStash students;

// snip
}
// destructors ought to be called here?
system("PAUSE");
return EXIT_SUCCESS;
}

in fact.. only the destructor for ObStash students is called, the
ObStash scores are not destructed either! i'm assuming they're not
being destructed because the students aren't being destructed.

modifying it as follows..

int main(int argc, char *argv[])
{

{
ObStash students;
// snip
for(int i=0; i<students.getCount(); i++) {
delete (Student*)students.fetch(i);
}
}

system("PAUSE");
return EXIT_SUCCESS;
}
everything seems to get destructed. i'm not missing anything am i?

thank you all for your help :)

i have one class in java, and one class in c++ this semester.. i'm
really starting to appreciate java..kinda.

to delete a list in java, all you have to do is set the head to null (i
think), and let the garbage collector clean up the huge amount of data
you just tossed into space.

Oct 6 '06 #15
"Mark" <mn*******@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
David W wrote:

My guess is that the numbers you are putting in there no longer exist, but you didn't include
that
code. If the ints are ordinary local variables they may have gone out of scope. You may need to
create each int to be stored with 'new' if that's the case (and delete later).

okay, that worked like a charm!

now..last question..i hope.

i've got my destructor

ObStash::~ObStash()
{
delete [] storage;
}

but will that be sufficient to prevent leakage?
Not if the objects pointed to were created with 'new'.
since it's an array of pointers. i figure i would have to delete each
pointer first,
Yes, and to the correct type of pointer as well (the same type created with 'new').
and then delete the array, since it's declared as void
**storage, however.. i can't delete each pointer because they delete
void* is undefined!

but, from my understanding, it's not necessarily delete integers
anyways..
Yes it is.
they'll take care of themselves..
No they won't. If they were created with 'new' they consume space in the free store like anything
else until they are explicitly deleted.
but the Student class
however, is another story.
A related story.
class Student
{
string name;
ObStash scores;

public:
Student(string name);
void print();
void cleanUp();
};

the string has it's own destructor i believe, and should clean up after
it's self...
Yes, as part of the destruction of the Student.
the obstash's destructor should clean up the scores too i think..
No, it shouldn't. It can't know the types really being pointed to by the array elements, and it
can't know whether those objects were created with 'new'.
but what about when

ObStash students;

goes out of scope?

do i need to call the destructor for each object individually or
anything..? (delete each student manually)
Yes.
or..will they all die
gracefully?
No.

DW

Oct 6 '06 #16

Mark 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
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?
Oct 6 '06 #17

i have a single link list with with 5 nodes .if 5 nodes address part
pointing to 2nd node. then show there is a loop . code it in c.
help me.

Oct 6 '06 #18
neel wrote:
i have a single link list with with 5 nodes .if 5 nodes address part
pointing to 2nd node. then show there is a loop . code it in c.
For C questions please ask in comp.lang.c. Also, read the FAQ before
posting (there and anywhere else). They won't do your homework just
like we won't.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 6 '06 #19

David W wrote:
"Mark" <mn*******@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
David W wrote:
>
My guess is that the numbers you are putting in there no longer exist, but you didn't include
that
code. If the ints are ordinary local variables they may have gone out of scope. You may need to
create each int to be stored with 'new' if that's the case (and delete later).
>
okay, that worked like a charm!

now..last question..i hope.

i've got my destructor

ObStash::~ObStash()
{
delete [] storage;
}

but will that be sufficient to prevent leakage?

Not if the objects pointed to were created with 'new'.
since it's an array of pointers. i figure i would have to delete each
pointer first,

Yes, and to the correct type of pointer as well (the same type created with 'new').
and then delete the array, since it's declared as void
**storage, however.. i can't delete each pointer because they delete
void* is undefined!

but, from my understanding, it's not necessarily delete integers
anyways..

Yes it is.
they'll take care of themselves..

No they won't. If they were created with 'new' they consume space in the free store like anything
else until they are explicitly deleted.
but the Student class
however, is another story.

A related story.
class Student
{
string name;
ObStash scores;

public:
Student(string name);
void print();
void cleanUp();
};

the string has it's own destructor i believe, and should clean up after
it's self...

Yes, as part of the destruction of the Student.
the obstash's destructor should clean up the scores too i think..

No, it shouldn't. It can't know the types really being pointed to by the array elements, and it
can't know whether those objects were created with 'new'.
but what about when

ObStash students;

goes out of scope?

do i need to call the destructor for each object individually or
anything..? (delete each student manually)

Yes.
or..will they all die
gracefully?

No.

DW
hm.. I think I was getting confused with something else. thank you for
your help.

Oct 10 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jonathan Wilson | last post by:
Basicly, I need to store a string plus a data structure for each entry in this array. It needs to be able to get bigger (but I wont be deleting from it). Also, I need to be able to store the...
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
19
by: gaga | last post by:
I can't seem to get this to work: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *names; char **np;
2
by: Simon Morgan | last post by:
I hope this isn't OT, I looked for a newsgroup dealing purely with algorithms but none were to be found and seeing as I'm trying to implement this in C I thought this would be the best place. I...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
1
by: since | last post by:
I figured I would post my solution to the following. Resizable column tables. Search and replace values in a table. (IE only) Scrollable tables. Sortable tables. It is based on a lot...
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.