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

Does c++(under linux) overcommit memory?

Hi

I was always under the assumption that linux always overcommits memory
by default - but I'm getting unexpected results
while requesting for a large ammount of memory using new (c++).

In the sense , say I try and allocate dynamically a large array p (int
*p)

p = (int *) malloc(N * sizeof(int)); // ----
1

and replace it by

p = new int[ N * sizeof(int)]; // -- 2

where N = 1000000000000000 //

the second statement always generates a bad_alloc exception ---
Agreed that if you try and access p it'd give a SIGSEGV - but why
should a plain allocation give a bad_alloc - "C" doesn't seem to mind
it - shouldn't C++ too??

I suspect it could be because C++ uses a different memory management
library - could someone please clarify.

(When I do an strace - I find both of the above versions end up
calling mmap().)

ENV -

gcc 3.4.3

linux - 2.4.21-40.EL

I'd really appreciate some info on this,

Regards

Feb 17 '07 #1
5 1920
On 17 Feb., 11:10, "jon wayne" <jon.wayne...@gmail.comwrote:
Hi

I was always under the assumption that linux always overcommits memory
by default - but I'm getting unexpected results
while requesting for a large ammount of memory using new (c++).
That I don't know.
>
In the sense , say I try and allocate dynamically a large array p (int
*p)

p = (int *) malloc(N * sizeof(int)); // ----
1

and replace it by

p = new int[ N * sizeof(int)]; // -- 2
That is not a replacement (unless sizeof(int) happens to be 1 on your
platform). The corresponding expression is
p = new int[ N];
>
where N = 1000000000000000 //

the second statement always generates a bad_alloc exception ---
Agreed that if you try and access p it'd give a SIGSEGV - but why
should a plain allocation give a bad_alloc - "C" doesn't seem to mind
it - shouldn't C++ too??
I would normally recommend that you use std::vector. Here, you'd have:
std::vector<intv(N);

(and use &v[0] whenever you want a pointer to its first element).
In that case you'd have a segment violation because the vector would
be initialised and the overcomitment would kick in right away.
[snip]

/Peter

Feb 17 '07 #2
jon wayne wrote:
Hi

I was always under the assumption that linux always overcommits memory
by default - but I'm getting unexpected results
while requesting for a large ammount of memory using new (c++).

In the sense , say I try and allocate dynamically a large array p (int
*p)

p = (int *) malloc(N * sizeof(int)); // ----
1

and replace it by

p = new int[ N * sizeof(int)]; // -- 2

where N = 1000000000000000 //

the second statement always generates a bad_alloc exception ---
Agreed that if you try and access p it'd give a SIGSEGV - but why
should a plain allocation give a bad_alloc - "C" doesn't seem to mind
it - shouldn't C++ too??

You don't need '*sizeof(int)' in C++, it does that for you. Try this

p = new int[ N ]; // -- 2
>
I suspect it could be because C++ uses a different memory management
library - could someone please clarify.
new is different from malloc in another way too. With new each allocated
object is also default constructed. That makes no difference for int,
but might for a class you have written.

john
Feb 17 '07 #3
jon wayne wrote:
Hi

I was always under the assumption that linux always overcommits memory
by default - but I'm getting unexpected results
while requesting for a large ammount of memory using new (c++).

In the sense , say I try and allocate dynamically a large array p (int
*p)

p = (int *) malloc(N * sizeof(int)); // ----
1

and replace it by

p = new int[ N * sizeof(int)]; // -- 2

where N = 1000000000000000 //
I'd say N is not a long constant, so it most probably
wraps to something negative. I believe this is undefined
behaviour right away, and if not, then new is surprised
by you requesting a negative amount of elements.

HTH,
- J.
Feb 17 '07 #4
On Sat, 17 Feb 2007 14:30:49 +0100, Jacek Dziedzic
<ja************************@gmail.comwrote:
>jon wayne wrote:
>Hi

I was always under the assumption that linux always overcommits memory
by default - but I'm getting unexpected results
while requesting for a large ammount of memory using new (c++).

In the sense , say I try and allocate dynamically a large array p (int
*p)

p = (int *) malloc(N * sizeof(int)); // ----
1

and replace it by

p = new int[ N * sizeof(int)]; // -- 2

where N = 1000000000000000 //

I'd say N is not a long constant, so it most probably
wraps to something negative. I believe this is undefined
behaviour right away, and if not, then new is surprised
by you requesting a negative amount of elements.
Depends on what std::size_t is on your machine. The number may be valid on a
64-bit computer.

-dr
Feb 17 '07 #5
In the sense , say I try and allocate dynamically a large array p (int
*p)

p = (int *) malloc(N * sizeof(int)); // ----
1

and replace it by

p = new int[ N * sizeof(int)]; // -- 2

where N = 1000000000000000 //

the second statement always generates a bad_alloc exception ---
As noted, you're allocating 4 or so times more memory in the second
case.

Also, in the first case, are you checking if p == NULL after the
malloc? It's quite possible that it's failing too, and you're just
not checking it.

Michael

Feb 17 '07 #6

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

Similar topics

5
by: Jon Perez | last post by:
Running the following under Linux creates 3 processes instead of 2. Once the started thread exits, 2 processes still remain. Why? import thread from thread import start_new_thread def...
5
by: Gregor Rot | last post by:
Hi, i have this simple data structure: struct nodeProject { char key; char name; struct linkProject *plink; }; struct linkProject { struct nodeProject *node;
75
by: Beni | last post by:
I have been programming in C for about a year now. It sounds silly, but I never took the time to question why a C(or C++ or Java) program execution begins only at the main(). Is it a convention or...
9
by: Andy B | last post by:
If I bought one of these boxes/OS combos as a postgresql database server, would postgresql be able to make the best use of it with a huge (e.g. 40GB) database? Box: HP ProLiant DL585, with ...
17
by: christophe.chazeau | last post by:
Hi, I have a problem with a really simple chunk of code which should work but does obviously does not. This chunk of code is just a POC aimed at finding a bug in a larger project in which the...
19
by: llothar | last post by:
I must say i didn't expect this. I just did some measures on FreeBSD 6.2 with gcc 3.4.6 and there is absolutely no significant difference between 32 and 64 bit mode - neither in compilation speed,...
7
by: john | last post by:
I am reading TC++PL3 and on page 644 it is mentioned: "Flushing an istream is done using sync(). This cannot always be done right. For some kinds of streams, we would have to reread characters...
4
by: Moshe Goldfarb | last post by:
Strut on over to comp.lang.c to see our Kelsey in action getting his head handed to him. http://groups.google.com/group/comp.lang.c/msg/c3025fff79e1cdde ...
2
by: ravishi | last post by:
Well, this is my first topic at this forum and I'm a newbie on C programming too. I'm coding a little program and I've used some "dynamic arrays" on it. Compiling and running the program on Linux...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.