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

without using STL in linux, how to allocate large memory by "new" in C++?

5
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 part of the code:

const int range = 1000000;

int *array = new int(range);
if (array == 0) cout<<"null ptr returned"<<endl;

for (int i=0; i<range; i++)
*(array+i) = rand()%(10*range);

If 'range' is around 10000, the above code works.
But if it is large, then segmentation fault occurs after i=33790.
It happens every time after i reaches 33790. Or, if the array is double,
then seg fault will happen right after i=33790/2 = 16895.

What is wrong in the code?

it seems to me that the heap size available to my program is not
big enough. I used ulimit -a to check and it shows 10240 for stack size.
One more doubt is ulimit -s gives 10240 (man ulimit says in bytes)
but ulimit -a says kbytes. Which one is right?

I've also tried using array:
int array[1000000];
the code works for this particular size, but with the size increasing to
about 3M, seg fault happened too.

In general, how to deal with the need of using large chunk of memory in
c++ without using STL?
Jan 19 '08 #1
1 2124
weaknessforcats
9,208 Expert Mod 8TB
There's something about your Linux that has limited the size of the CRT heap.

I have done what you are trying to do many times and it works fine. I don't think it's your code.

BTW: If new fails, it does not return a 0. Instead it throws a bad_alloc exception.
Jan 20 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: dpr | last post by:
I have come accross a piece of C++ code with the construct: MyClass *c = new class MyClass(); Is there a difference between this and: MyClass *c = new MyClass(); ?
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...
18
by: Leslaw Bieniasz | last post by:
Cracow, 28.10.2004 Hello, I have a program that intensively allocates and deletes lots of relatively small objects, using "new" operator. The objects themselves are composed of smaller...
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...
15
by: b83503104 | last post by:
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.
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...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.