473,395 Members | 1,823 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.

C++ and malloc

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
Jul 19 '05 #1
8 6432
On 2003-09-29, Larry Lindsey <gt*****@prism.gatech.edu> wrote:
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.

Jul 19 '05 #2
Larry Lindsey wrote:
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)

--
Unforgiven

"Most people make generalisations"
Freek de Jonge

Jul 19 '05 #3

"Larry Lindsey" <gt*****@prism.gatech.edu> wrote in message
news:bl**********@news-int.gatech.edu...
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.
Jul 19 '05 #4
"Larry Lindsey" <gt*****@prism.gatech.edu> wrote in message
news:bl**********@news-int.gatech.edu...
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.

Jul 19 '05 #5
Steven C. wrote:
"Larry Lindsey" <gt*****@prism.gatech.edu> wrote in message
news:bl**********@news-int.gatech.edu...
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.

Jul 19 '05 #6

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bl********@dispatch.concentric.net...
Steven C. wrote:
"Larry Lindsey" <gt*****@prism.gatech.edu> wrote in message
news:bl**********@news-int.gatech.edu...
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.
Jul 19 '05 #7
"Steven C." <no****@xxx.com> wrote in message
news:mV******************@twister.socal.rr.com...

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bl********@dispatch.concentric.net...
Steven C. wrote:
"Larry Lindsey" <gt*****@prism.gatech.edu> wrote in message
news:bl**********@news-int.gatech.edu...
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.


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

Jul 19 '05 #8
Mike Wahler, in reply, wrote:
I believe vector<bool> is a special case for this.

__________________________________________

Really that would be great.

Not 'would', but 'is' :-)


There is also std::bitset<number> which provides a different interface
to the same sort of structure.

NR

Jul 19 '05 #9

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

Similar topics

19
by: john smith | last post by:
Can someone please explain to me what is happening when I do a malloc(0). This is what I did. int* p = (int*)malloc(0); Then I printed the value of p and of course it was non-null. But...
34
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
20
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
15
by: Martin Jørgensen | last post by:
Hi, I have a (bigger) program with about 15-30 malloc's in it (too big to post it here)... The last thing I tried today was to add yet another malloc **two_dimensional_data. But I found out that...
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
40
by: Why Tea | last post by:
What happens to the pointer below? SomeStruct *p; p = malloc(100*sizeof(SomeStruct)); /* without a cast */ return((void *)(p+1)); /* will the returned pointer point to the 2nd...
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
23
by: raphfrk | last post by:
I am having an issue with malloc and gcc. Is there something wrong with my code or is this a compiler bug ? I am running this program: #include <stdio.h> #include <stdlib.h> typedef...
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
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
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.