how to declare an array of objects without "new"

B

b83503104

Hi,

class MyClass{
int array_size;
HisClass **hisObject;
};

I want hisObject to point to an array of HisClass objects.

The size of the array is given by array_size.

My question is, if objects of type HisClass must be declared by the
library provided function CreateObject(), and cannot be declared by
using "new", how should I declare hisObject?

If I do this in the constructor, the compiler does not complain, but I
get Segmentation fault:

array_size = 5;
for (int i = 0; i < array_size; i++){
hisObject = CreateObject(...);
}

Thanks in advance!
 
L

Lionel

Hi,

class MyClass{
int array_size;
HisClass **hisObject;
};

All you've done is created a pointer to nothing, it's not allocated any
space.

I want hisObject to point to an array of HisClass objects.

What you want is an array of pointers to HisClass objects:

HisClass *hisObject[array_size];

This will create an array of length array_size, with each element
allowing enough space for the size of a pointer to a HisClass object.

The size of the array is given by array_size.

My question is, if objects of type HisClass must be declared by the
library provided function CreateObject(), and cannot be declared by
using "new", how should I declare hisObject?

If I do this in the constructor, the compiler does not complain, but I
get Segmentation fault:

array_size = 5;
for (int i = 0; i < array_size; i++){
hisObject = CreateObject(...);
}


The compiler won't complain because it assumes you have done the right
thing and allocated the memory. However, at run time your app tries to
access memory that it is not allowed to or isn't a valid address and so
you get the seg fault.


Lionel.
 
D

David White

Hi,

class MyClass{
int array_size;
HisClass **hisObject;
};

I want hisObject to point to an array of HisClass objects.

The size of the array is given by array_size.

My question is, if objects of type HisClass must be declared by the
library provided function CreateObject(), and cannot be declared by
using "new", how should I declare hisObject?

If I do this in the constructor, the compiler does not complain, but I
get Segmentation fault:

Because hisObject could be pointing anywhere.
array_size = 5;

hisObject = new HisClass *[array_size];
for (int i = 0; i < array_size; i++){
hisObject = CreateObject(...);
}


Have you considered using a std::vector<HisClass *> instead?

DW
 
D

David White

Lionel said:
What you want is an array of pointers to HisClass objects:

HisClass *hisObject[array_size];

It looks from the OP's post that array_size is not a compile-time constant.

DW
 
B

b83503104

Thanks. But there is some problem.

"hisObject" is defined in the class definition, and "array_size" is
initialized in the constructor, so I cannot (?!) do this:
HisClass *hisObject[array_size];

Neither can I use "new". I have to use "CreateObject()" to allocate
space. How should I do that?

Thanks!
 
B

b83503104

You suggested
hisObject = new HisClass *[array_size];

Unfortunately, I must not use "new". I have to use the library
provided function "CreateObject(...)" to allocate space.

Still need suggestions. Thanks in advance.
 
L

Lionel

David said:
Lionel said:
What you want is an array of pointers to HisClass objects:

HisClass *hisObject[array_size];


It looks from the OP's post that array_size is not a compile-time constant.

I was a little lazy and assumed the OP was doing something sensible with
array_size . . .
 
J

Jack Klein

You suggested
hisObject = new HisClass *[array_size];

Unfortunately, I must not use "new". I have to use the library
provided function "CreateObject(...)" to allocate space.

Still need suggestions. Thanks in advance.

You are not reading the reply carefully enough.

The allocation David suggested for hisObject does not create any
HisClass objects at all, it creates an array of array_size pointers to
HisClass objects.

Then you iterate through the array:

for (int count = 0; count < array_size; ++count)
{
hisObject [count] = CreateObject();
}

You are not using new to bypass calling the object creation function
at all. You are using new to allocate space for the pointers.
 
L

Lionel

You suggested
hisObject = new HisClass *[array_size];

Unfortunately, I must not use "new". I have to use the library
provided function "CreateObject(...)" to allocate space.

Still need suggestions. Thanks in advance.

This wouldn't happen to be an assignment would it?
 
M

Me

class MyClass{
int array_size;
HisClass **hisObject;
};

If I do this in the constructor, the compiler does not complain, but I
get Segmentation fault:

array_size = 5;

hisObject = new HisClass *[array_size];
for (int i = 0; i < array_size; i++){
hisObject = CreateObject(...);
}


then in your destructor after destroying each hisObject, don't
forget to:

delete [] hisObject;
 
B

b83503104

Great, it works now. I didn't notice it was creating an array of
pointers.

I tried something else, which did not work, and I hope somebody could
point out why:

p = new HisClass *[array_size];
for (int i = 0; i < array_size; i++)
{
p = CreateObject(...);
}

hisObject = p;

where "hisObject" was defined in the class as
HisClass **hisObject;

Shouldn't this be the same as what David or Jack suggested?
 
J

Jakob Bieling

Thanks. But there is some problem.

"hisObject" is defined in the class definition, and "array_size" is
initialized in the constructor, so I cannot (?!) do this:
HisClass *hisObject[array_size];

Neither can I use "new". I have to use "CreateObject()" to allocate
space. How should I do that?


You can use new, because you are allocating pointers, not objects.
When you have your array of pointers, use CreateObject tohave each
pointer point to a valid object.

hth
 
J

Jakob Bieling

Thanks. But there is some problem.

"hisObject" is defined in the class definition, and "array_size" is
initialized in the constructor, so I cannot (?!) do this:
HisClass *hisObject[array_size];

Neither can I use "new". I have to use "CreateObject()" to allocate
space. How should I do that?


You can use new, because you are allocating pointers, not objects.
When you have your array of pointers, use CreateObject tohave each
pointer point to a valid object.

hth
 
D

David White

Great, it works now. I didn't notice it was creating an array of
pointers.

I tried something else, which did not work,

In what way did it not work?
and I hope somebody could
point out why:

p = new HisClass *[array_size];

Where is your definition of 'p'? You need to show all relevant code.
for (int i = 0; i < array_size; i++)
{
p = CreateObject(...);
}

hisObject = p;

where "hisObject" was defined in the class as
HisClass **hisObject;

Shouldn't this be the same as what David or Jack suggested?


On the face of it, yes. You need to show all relevant code and explain why
you believe there's a problem.

DW
 
B

b83503104

Here is the relevant code I should have provided, and I also noticed
some mistake in my previous post. Sorry about that.

I have a class like this:

class MyClass{
int array_size;
HisClass **hisObject;
};

And I want to declare hisObject using the library provided function
"CreateObject(...)".

Here is my question: Aren't these 2 approaches the same? I get seg
fault for Approach 1:

// Approach 1
HisClass *(p[array_size]);
for (int i = 0; i < array_size; i++)
{
p = CreateObject(...);
}
hisObject = p;

// Approach 2
hisObject = new HisClass *[array_size];
for (int i = 0; i < array_size; i++)
{
hisObject = CreateObject(...);
}

Thanks.
 
K

Karl Heinz Buchegger

Here is my question: Aren't these 2 approaches the same?

No they are not.
In 1) the array is allocated 'auto'. Its lifetime is controlled
be the enclosing scope. When that scope ends, so does that array.

In 2) the array is allocated on the free store. You have full control
over when the array gets deleted (and have to do so by yourself) by
actually deleting it.
I get seg
fault for Approach 1:

// Approach 1
HisClass *(p[array_size]);
for (int i = 0; i < array_size; i++)
{
p = CreateObject(...);
}
hisObject = p;

// Approach 2
hisObject = new HisClass *[array_size];
for (int i = 0; i < array_size; i++)
{
hisObject = CreateObject(...);
}

Thanks.
 
M

Mike Smith

Hi,

class MyClass{
int array_size;
HisClass **hisObject;
};

I want hisObject to point to an array of HisClass objects.

The size of the array is given by array_size.

My question is, if objects of type HisClass must be declared by the
library provided function CreateObject(), and cannot be declared by
using "new", how should I declare hisObject?

What is the declaration of CreateObject()?

If it is:

HisClass *CreateObject(...)

Then I suspect that what you want is something like:

MyClass::MyClass(int size) : array_size(size)
{
hisObject = new HisClass *[array_size];
for (int i = 0; i < array_size; ++i)
hisObject = CreateObject(...)
}

But, as indicated elsewhere, perhaps a vector of HisClass *, or even a
vector of smart pointers to HisClass, might be preferable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top