Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to declare an array of objects without "new"

Reply
Thread Tools

how to declare an array of objects without "new"

 
 
b83503104@yahoo.com
Guest
Posts: n/a
 
      07-11-2005
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!

 
Reply With Quote
 
 
 
 
Lionel
Guest
Posts: n/a
 
      07-11-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
David White
Guest
Posts: n/a
 
      07-11-2005
(E-Mail Removed) 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


 
Reply With Quote
 
David White
Guest
Posts: n/a
 
      07-11-2005
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


 
Reply With Quote
 
b83503104@yahoo.com
Guest
Posts: n/a
 
      07-11-2005
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!

 
Reply With Quote
 
b83503104@yahoo.com
Guest
Posts: n/a
 
      07-11-2005
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.

 
Reply With Quote
 
Lionel
Guest
Posts: n/a
 
      07-11-2005
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 . . .
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      07-11-2005
On 10 Jul 2005 22:42:47 -0700, "(E-Mail Removed)"
<(E-Mail Removed)> 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
 
Reply With Quote
 
Lionel
Guest
Posts: n/a
 
      07-11-2005
(E-Mail Removed) 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?
 
Reply With Quote
 
Me
Guest
Posts: n/a
 
      07-11-2005
> 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;

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
Declare array of objects with constructor having arguments b83503104@yahoo.com C++ 7 07-21-2005 06:41 AM
Dynamically declare objects in javascript Secondpost Javascript 2 11-11-2003 10:00 AM
declare array of objects in header file Philipp C++ 5 10-16-2003 06:08 PM



Advertisments