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

Dynamic array of objects - initialization

Hello everyone,

I want to create an array of objects at run-time.

AFAIU, operator new[] will call the default constructor for each object
in the array. In other words, the following program will print INSIDE
DEFAULT CTOR five times.

#include <vector>
#include <cstdio>

struct Foo
{
Foo(int u) {
puts("INSIDE CTOR"); p = new char[u];
}
Foo() {
puts("INSIDE DEFAULT CTOR"); p = new char[666];
}
~Foo() {
puts("INSIDE DTOR"); delete[] p;
}
char *p;
};

int main()
{
Foo *ww = new Foo[5];
return 0;
}

$ g++ -Wall -g3 vectest.cxx
vectest.cxx: In function `int main()':
vectest.cxx:20: warning: unused variable 'ww'
$ ./a.out
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR

What if I want to use a different constructor?

For example, how can I get the program to create an array of 5 objects
that hold a 123-byte buffer?

Foo *ww = new Foo(123)[5];

is a syntax error. Am I missing something obvious?

I suppose I could add a static variable to class Foo and have the
default constructor use the value of that variable...

struct Foo
{
Foo(int u) {
puts("INSIDE CTOR"); p = new char[u];
}
Foo() {
puts("INSIDE DEFAULT CTOR"); p = new char[666];
}
~Foo() {
puts("INSIDE DTOR"); delete[] p;
}
char *p;
static int defaultsize;
};

int Foo::defaultsize = 0;

int main()
{
Foo::defaultsize = 123;
Foo *ww = new Foo[5];
return 0;
}

But that feels like a kludge. Is there a better solution?

On a related note, would a vector help in this situation?

I could write something along the lines of

std::vector < Foo v;
v.reserve(N);
for (int i=0; i < N; ++i)
{
Foo *curr = new Foo(size)
v.push_back(*curr);
}

But that feels somewhat like a kludge too.

Regards.
Mar 19 '07 #1
3 3180
Spoon wrote:
I want to create an array of objects at run-time.

AFAIU, operator new[] will call the default constructor for each
object in the array. In other words, the following program will print
INSIDE DEFAULT CTOR five times.

#include <vector>
#include <cstdio>

struct Foo
{
Foo(int u) {
puts("INSIDE CTOR"); p = new char[u];
}
Foo() {
puts("INSIDE DEFAULT CTOR"); p = new char[666];
}
~Foo() {
puts("INSIDE DTOR"); delete[] p;
}
char *p;
};

int main()
{
Foo *ww = new Foo[5];
return 0;
}

$ g++ -Wall -g3 vectest.cxx
vectest.cxx: In function `int main()':
vectest.cxx:20: warning: unused variable 'ww'
$ ./a.out
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR

What if I want to use a different constructor?
You're SOL.
For example, how can I get the program to create an array of 5 objects
that hold a 123-byte buffer?

Foo *ww = new Foo(123)[5];

is a syntax error. Am I missing something obvious?
Not really. There is no way to do what you want in a single statement.
I suppose I could add a static variable to class Foo and have the
default constructor use the value of that variable...

struct Foo
{
Foo(int u) {
puts("INSIDE CTOR"); p = new char[u];
}
Foo() {
puts("INSIDE DEFAULT CTOR"); p = new char[666];
}
~Foo() {
puts("INSIDE DTOR"); delete[] p;
}
char *p;
static int defaultsize;
};

int Foo::defaultsize = 0;

int main()
{
Foo::defaultsize = 123;
Foo *ww = new Foo[5];
return 0;
}

But that feels like a kludge. Is there a better solution?
Use std::vector.
On a related note, would a vector help in this situation?
Yep.
I could write something along the lines of

std::vector < Foo v;
v.reserve(N);
for (int i=0; i < N; ++i)
{
Foo *curr = new Foo(size)
v.push_back(*curr);
}

But that feels somewhat like a kludge too.
Whatever you choose to call it. I call it "a work-around".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 19 '07 #2
On 19 Mar, 15:07, Spoon <devn...@localhost.comwrote:
Hello everyone,

I want to create an array of objects at run-time.

AFAIU, operator new[] will call the default constructor for each object
in the array. In other words, the following program will print INSIDE
DEFAULT CTOR five times.

#include <vector>
#include <cstdio>

struct Foo
{
Foo(int u) {
puts("INSIDE CTOR"); p = new char[u];
}
Foo() {
puts("INSIDE DEFAULT CTOR"); p = new char[666];
}
~Foo() {
puts("INSIDE DTOR"); delete[] p;
}
char *p;

};

int main()
{
Foo *ww = new Foo[5];
return 0;

}

$ g++ -Wall -g3 vectest.cxx
vectest.cxx: In function `int main()':
vectest.cxx:20: warning: unused variable 'ww'
$ ./a.out
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR

What if I want to use a different constructor?

For example, how can I get the program to create an array of 5 objects
that hold a 123-byte buffer?

Foo *ww = new Foo(123)[5];

is a syntax error. Am I missing something obvious?

I suppose I could add a static variable to class Foo and have the
default constructor use the value of that variable...

struct Foo
{
Foo(int u) {
puts("INSIDE CTOR"); p = new char[u];
}
Foo() {
puts("INSIDE DEFAULT CTOR"); p = new char[666];
}
~Foo() {
puts("INSIDE DTOR"); delete[] p;
}
char *p;
static int defaultsize;

};

int Foo::defaultsize = 0;

int main()
{
Foo::defaultsize = 123;
Foo *ww = new Foo[5];
return 0;

}

But that feels like a kludge. Is there a better solution?

On a related note, would a vector help in this situation?

I could write something along the lines of

std::vector < Foo v;
v.reserve(N);
for (int i=0; i < N; ++i)
{
Foo *curr = new Foo(size)
v.push_back(*curr);

}
std::vector<Foov(5, Foo(4));

--
Erik Wikström

Mar 19 '07 #3
Erik Wikström wrote:
On 19 Mar, 15:07, Spoon <devn...@localhost.comwrote:
>[..]

std::vector<Foov(5, Foo(4));
... along with a proper implementation of the copy c-tor.

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

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

Similar topics

5
by: Nils | last post by:
Hi, I want to create a dynamic array with pointer, without allocation of the memory. I tried it so: objekt **ob= new objekt; It is not working, because he will parameter for the Konstructor...
6
by: Vasileios Zografos | last post by:
Hello, I have a function that generates some values (e.g. vertices in 2d space) the number of which I dont know. So, it could generate 20 vertices, 100 vertices, or even 1 vertex. void...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
1
by: lemonade | last post by:
Hello! Can someone explain to me the difference between dynamic array of pointers vs dynamic array of objects by giving a real life example. Following is the code that I am using for dynamic...
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
3
by: sk.rasheedfarhan | last post by:
Hi , Here I am new user to C#, my problem is I have to use dynamic Array of objects. But I heard C# don't support ptrs (using managed code C# support). In short i initialized objects of 1000...
2
by: him1979 | last post by:
If I have these classes: class student { private: string * name; int faculty number; date datebirth; public: ... };
4
by: Steph | last post by:
Hello, I have filled a dynamic array of strings (realloc() + malloc()) char **sMyArray; At the end, I get correctly sMyArray = "STRING_0", sMyArray = "STRING_1", etc... But I'm unable to...
4
by: David d'Angers | last post by:
hi all i need a function that returns all the file names contained in a directory since the number of files is not known in advance how should i go about to use "dynamic array" to hold the file...
0
by: leon70 | last post by:
Hi Group, I built a gSoap 2.7.11 Web services client in C++ on AIX and have got it talking to the remote service. The service is sending back an array of objects to my client but the client is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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
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...
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.