C++ and malloc

L

Larry Lindsey

Is there a better way of defining a large array other than malloc? I'm
looking to declare an array of bool at a length of up to 250000000. Thanks,

Larry
 
Z

Zeljko Vrba

Is there a better way of defining a large array other than malloc? I'm
looking to declare an array of bool at a length of up to 250000000. Thanks,
bool *arr = new bool[250000000];
bool arr[250000000];

Or you might want to use std::bit_vector which should substantially save
space.
 
U

Unforgiven

Larry said:
Is there a better way of defining a large array other than malloc?
I'm looking to declare an array of bool at a length of up to
250000000. Thanks,

That's a lotta bools...

Anyway:

bool *MyBools = new bool[250000000];
delete[] MyBools;

or even better:
#include <vector>
std::vector<bool> MyBools(250000000);

(Note that std::vector can grow and shrink dynamically, you don't have to
give it it's maximum size initially if you don't want to)
 
J

jeffc

Larry Lindsey said:
Is there a better way of defining a large array other than malloc? I'm
looking to declare an array of bool at a length of up to 250000000.
Thanks,

I would use a standard library type such as vector.
 
S

Steven C.

Is there a better way of defining a large array other than malloc? I'm
looking to declare an array of bool at a length of up to 250000000. Thanks,

Larry

_______________________________________________________

Since a bool hold is only 1 or 0 I use a long to store 32 bools and then you
could malloc (or new) 7812500 longs. It would be trival to wirte a class to
do this.
 
G

Gianni Mariani

Steven said:
Is there a better way of defining a large array other than malloc? I'm
looking to declare an array of bool at a length of up to 250000000. Thanks,

Larry

_______________________________________________________

Since a bool hold is only 1 or 0 I use a long to store 32 bools and then you
could malloc (or new) 7812500 longs. It would be trival to wirte a class to
do this.

I believe vector<bool> is a special case for this.
 
S

Steven C.

Steven said:
Is there a better way of defining a large array other than malloc? I'm
looking to declare an array of bool at a length of up to 250000000. Thanks,

Larry

_______________________________________________________

Since a bool hold is only 1 or 0 I use a long to store 32 bools and then you
could malloc (or new) 7812500 longs. It would be trival to wirte a class to
do this.

I believe vector<bool> is a special case for this.

__________________________________________

Really that would be great.
 
M

Mike Wahler

Steven C. said:
class

I believe vector<bool> is a special case for this.

__________________________________________

Really that would be great.

Not 'would', but 'is' :)

=========================================================
ISO/IEC 14882:1998(E)

23.2.5 Class vector<bool>

1 To optimize space allocation, a specialization of vector for
bool elements is provided:

namespace std {
template <class Allocator> class vector<bool, Allocator> {
public:
// types:
typedef bool const_reference;
typedef (implementation defined) iterator; // See 23.1
typedef (implementation defined) const_iterator; // See 23.1
typedef (implementation defined) size_type; // See 23.1
typedef (implementation defined) difference_type;// See 23.1
typedef bool value_type;
typedef Allocator allocator_type;
typedef (implementation defined) pointer;
typedef (implementation defined) const_pointer
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator>
const_reverse_iterator;

// bit reference:
class reference {
friend class vector;
reference();
public:
~reference();
operator bool() const;
reference& operator=(const bool x);
reference& operator=(const reference& x);
void flip(); // flips the bit
};

[member function descriptions omitted for brevity -- MKW]

2 reference is a class that simulates the behavior of references
of a single bit in vector<bool>.
=========================================================

HTH,
-Mike
 

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
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top