473,395 Members | 1,386 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,395 software developers and data experts.

how to declare an array of objects without "new"

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
15 2266
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
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
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
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
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
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
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
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
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
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
<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
<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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: garyolsen | last post by:
For a class, MyClass, there are two ways to instantiate an object: 1. MyClass *MC = new MyClass(); 2. MyClass MC; In general, when should you have to use 1 and when 2? What're...
30
by: seesaw | last post by:
Is it right thing to always avoid using "new" to create objects? What if after starting the application, then decide which and how many objects to create? (Seems like under such situation is there...
4
by: Casper | last post by:
Is there features/containers of standard C++ or the STL which will assist in minimizing memmory fragmentation upon creation of a huge ammount of objects on the heap? Currently my application...
24
by: Rv5 | last post by:
Rookie c++ question, but Ive spent the last 5 years doing Java, where everytime I created an object I used new. In c++ I can create my objects without and its confusing me just a little. I have...
1
by: Jean Stax | last post by:
Hi ! A couple of pretty basic questions: Value types: As far as I understand, when I create value type without "new" syntax the object is considered as unutilized. Consequently, I have to...
51
by: Tony Sinclair | last post by:
I'm just learning C#. I'm writing a program (using Visual C# 2005 on WinXP) to combine several files into one (HKSplit is a popular freeware program that does this, but it requires all input and...
1
by: parvtb | last post by:
I know STL vector works. But in case STL is not available, what can one do to allocate large memory size in c++ through operator "new"? For instance, I am writing a sort algorithm, and here's...
350
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.