[NEWBIE] Initialization of an array with a runtime determined size

  • Thread starter Stefano Sabatini
  • Start date
S

Stefano Sabatini

Hi to all C++ posters,
this is my first post here so please be clement.

I want to define inside an object an array with a size which is
determined by the constructor. Here it is the code:

#include <iostream>

using namespace std;

class BufferContainer {
public:
/**
* this buffer is initialized at creation
*/
int buffer[];

/**
* the size of the buffer the object will contain
*/
int bufferSize;

/**
* constructor
*
* @param _bufferSize the size of the buffer to be contained in the
* object
*/
BufferContainer(int _bufferSize);

/**
* destructor
*/
~BufferContainer();
};

BufferContainer::BufferContainer (int _bufferSize) {
bufferSize = _bufferSize;
buffer = new int[bufferSize];
}

BufferContainer::~BufferContainer() {
delete buffer;
}

#if 1
int main(void) {
BufferContainer *b1 = new BufferContainer(128);
exit(0);
}
#endif


I know that the following syntax:
buffer = new int[bufferSize];

is wrong and so I'm not surprised it raises a compilation error. So
which is the correct way to express it?

And which should be the right approach to the problem (maybe should I
use some standard class instead of a raw array)?

Greeter.sayThank(1000);
Greeter.greets();
 
S

Stefano Sabatini

Hi to all C++ posters,
this is my first post here so please be clement.

I want to define inside an object an array with a size which is
determined by the constructor. Here it is the code:

#include <iostream>

using namespace std;

class BufferContainer {
public:
/**
* this buffer is initialized at creation
*/
int buffer[];
^^^^^^^^^^^^
got it, better to put like:
int *buffer;
/**
* the size of the buffer the object will contain
*/
int bufferSize;

/**
* constructor
*
* @param _bufferSize the size of the buffer to be contained in the
* object
*/
BufferContainer(int _bufferSize);

/**
* destructor
*/
~BufferContainer();
};

BufferContainer::BufferContainer (int _bufferSize) {
bufferSize = _bufferSize;
buffer = new int[bufferSize];
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
so now this will work just fine.
}

BufferContainer::~BufferContainer() {
delete buffer;
}

#if 1
int main(void) {
BufferContainer *b1 = new BufferContainer(128);
exit(0);
}
#endif


I know that the following syntax:
buffer = new int[bufferSize];

is wrong and so I'm not surprised it raises a compilation error. So
which is the correct way to express it?

And now another newbie question: how it is supposed to be initialized
the new array?

I mean, I can check that the new array will only contains zero values,
can I rely on this behaviour or is it somehow dependant on the compiler,
so that I should rather initialize it explicitly?
And which should be the right approach to the problem (maybe should I
use some standard class instead of a raw array)?

Greeter.sayThank(1000);
Greeter.greets();

Mmh... better like
Greeter.greet(); // ;-)

Thanks for your attention.
 
I

Ioannis Gyftos

And which should be the right approach to the problem (maybe should I
use some standard class instead of a raw array)?

What do you want to do with that buffer? If you want to use it purely
as an array of ints, you might as well use vector<int>. For example:

#include <vector>

class BufferContainer {
public:
/**
* this buffer is initialized at creation
*/
std::vector<int> buffer;

/**
* the size of the buffer the object will contain
*/
int bufferSize;

/**
* constructor
*
* @param _bufferSize the size of the buffer to be contained in
the
* object
*/
BufferContainer(int _bufferSize);

/**
* destructor
*/
~BufferContainer();

};

BufferContainer::BufferContainer (int _bufferSize) {
bufferSize = _bufferSize;
buffer.reserve(bufferSize);

}

BufferContainer::~BufferContainer() {
// vector takes care of deleting
}

Where std::vector::reserve() just increases the capacity of the
vector. If you want to initialize the ints as well, use
std::vector::resize() instead.
 
P

Paul Sinnett

Stefano said:
And now another newbie question: how it is supposed to be initialized
the new array?

I mean, I can check that the new array will only contains zero values,
can I rely on this behaviour or is it somehow dependant on the compiler,
so that I should rather initialize it explicitly?

Yes. Usually memory will be initialised to zero in debug build
configurations but left uninitialised otherwise.
 
P

Pete Becker

Yes. Usually memory will be initialised to zero in debug build
configurations but left uninitialised otherwise.

That's a truly bad idea. It means that code that checks for null
pointers in allocated data objects may work okay in debug build
configurations but fail mysteriously otherwise. Better to initialize to
some non-zero value, to avoid making uninitialized things look like
valid data.
 
T

terminator

Hi to all C++ posters,
this is my first post here so please be clement.
I want to define inside an object an array with a size which is
determined by the constructor. Here it is the code:
#include <iostream>
using namespace std;
class BufferContainer {
public:
/**
* this buffer is initialized at creation
*/
int buffer[];

^^^^^^^^^^^^
got it, better to put like:
int *buffer;




/**
* the size of the buffer the object will contain
*/
int bufferSize;
/**
* constructor
*
* @param _bufferSize the size of the buffer to be contained in the
* object
*/
BufferContainer(int _bufferSize);
/**
* destructor
*/
~BufferContainer();
};
BufferContainer::BufferContainer (int _bufferSize) {
bufferSize = _bufferSize;
buffer = new int[bufferSize];

^^^^^^^^^^^^^^^^^^^^^^^^^^^^
so now this will work just fine.




BufferContainer::~BufferContainer() {
delete buffer;
//you must use array delete:
delete[] buffer;


but you`d better keep off arrays and use vector.

cheers,
FM.
 
T

terminator

Hi to all C++ posters,
this is my first post here so please be clement.
I want to define inside an object an array with a size which is
determined by the constructor. Here it is the code:
#include <iostream>
using namespace std;
class BufferContainer {
public:
/**
* this buffer is initialized at creation
*/
int buffer[];

^^^^^^^^^^^^
got it, better to put like:
int *buffer;




/**
* the size of the buffer the object will contain
*/
int bufferSize;
/**
* constructor
*
* @param _bufferSize the size of the buffer to be contained in the
* object
*/
BufferContainer(int _bufferSize);
/**
* destructor
*/
~BufferContainer();
};
BufferContainer::BufferContainer (int _bufferSize) {
bufferSize = _bufferSize;
buffer = new int[bufferSize];

^^^^^^^^^^^^^^^^^^^^^^^^^^^^
so now this will work just fine.




BufferContainer::~BufferContainer() {
delete buffer;
}
#if 1
int main(void) {
BufferContainer *b1 = new BufferContainer(128);

Don`t you clean the heap before ending?
You must delete b1:
delete b1;
if you want better code just write this:

BufferContainer b(128);

very bad why 'exit'?
that is a ***crul** way of finishing just do a return:

return 0;

cheers,
FM
 
P

Paul Sinnett

Pete said:
That's a truly bad idea. It means that code that checks for null
pointers in allocated data objects may work okay in debug build
configurations but fail mysteriously otherwise. Better to initialize to
some non-zero value, to avoid making uninitialized things look like
valid data.

Yes. Sorry. I meant non-zero. 0xCC and 0xCD seem to crop up a lot. But
there's no standard for this as far as I'm aware. It's quite handy to
know so you know what to look for in memory dumps.

Anyway that's a side issue. The short answer is: yes, you should
initialise the memory explicitly.
 
S

Stefano Sabatini

Yes. Sorry. I meant non-zero. 0xCC and 0xCD seem to crop up a lot. But
there's no standard for this as far as I'm aware. It's quite handy to
know so you know what to look for in memory dumps.

Anyway that's a side issue. The short answer is: yes, you should
initialise the memory explicitly.

Thank you so much guys for the answers, it's a pleasure to learn from
you.

Cheers.
 
T

terminator

What do you want to do with that buffer? If you want to use it purely
as an array of ints, you might as well use vector<int>. For example:

#include <vector>

class BufferContainer {
public:
/**
* this buffer is initialized at creation
*/
std::vector<int> buffer;

/**
* the size of the buffer the object will contain
*/
int bufferSize;

/**
* constructor
*
* @param _bufferSize the size of the buffer to be contained in
the
* object
*/
BufferContainer(int _bufferSize);

/**
* destructor
*/
~BufferContainer();

};

BufferContainer::BufferContainer (int _bufferSize) {
bufferSize = _bufferSize;
buffer.reserve(bufferSize);

}

had you used an initializer list things would be more straight
forward:

BufferContainer::BufferContainer (int sz):
bufferSize(sz),//initialize buffersize
//initialize buffer with 'sz' elements of default(zero) value:
buffer(sz)
{/*the rest of constructor code*/}
BufferContainer::~BufferContainer() {
// vector takes care of deleting

}

Where std::vector::reserve() just increases the capacity of the
vector. If you want to initialize the ints as well, use
std::vector::resize() instead.

regards,
FM.
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top