By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,129 Members | 1,331 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,129 IT Pros & Developers. It's quick & easy.

how to declare an array of objects without "new"

P: n/a
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[i] = CreateObject(...);
}

Thanks in advance!

Jul 23 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
b8*******@yahoo.com wrote:
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[i] = 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.
Jul 23 '05 #2

P: n/a
b8*******@yahoo.com wrote:
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[i] = CreateObject(...);
}


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

DW
Jul 23 '05 #3

P: n/a
Lionel wrote:
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
Jul 23 '05 #4

P: n/a
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!

Jul 23 '05 #5

P: n/a
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.

Jul 23 '05 #6

P: n/a
David White wrote:
Lionel wrote:
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 . . .
Jul 23 '05 #7

P: n/a
On 10 Jul 2005 22:42:47 -0700, "b8*******@yahoo.com"
<b8*******@yahoo.com> wrote in comp.lang.c++:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #8

P: n/a
b8*******@yahoo.com wrote:
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?
Jul 23 '05 #9

P: n/a
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[i] = CreateObject(...);
}


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

delete [] hisObject;

Jul 23 '05 #10

P: n/a
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[i] = 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?

Jul 23 '05 #11

P: n/a
<b8*******@yahoo.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
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
--
jb

(reply address in rot13, unscramble first)
Jul 23 '05 #12

P: n/a
<b8*******@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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[i] = 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

Jul 23 '05 #13

P: n/a
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[i] = CreateObject(...);
}
hisObject = p;

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

Thanks.

Jul 23 '05 #14

P: n/a
"b8*******@yahoo.com" wrote:

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[i] = CreateObject(...);
}
hisObject = p;

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

Thanks.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #15

P: n/a
b8*******@yahoo.com wrote:
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[i] = CreateObject(...)
}

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

--
Mike Smith
Jul 23 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.