You are not logged in. Please login at to post your questions!


Limit on Global memory and heap (dynamic allocation) memory ?

Can I allocate a 2D array of size [20000][20000] (roughly 1.5 GB) on the heap (by dynamic allocation) or as a global variable ?

// heap
int** dp = (int**)malloc(20000*sizeof(int*));
for(int i=0; i<20000; ++i) dp[i] = (int*)malloc(n*sizeof(int));

// global variable outside of main() function
int dp[20000][20000];

Can somebody please explain the bound of the memory avaliable on heap and global area?

asked 12 Oct '14, 00:31

sumanth232's gravatar image

accept rate: 8%

edited 29 May '15, 16:00

please help...

(12 Oct '14, 01:19) sumanth2324★

As you know that every process in Linux environment has these segments. The memory that you are talking about "Heap" comes within a Process Data Segment which consists of uninitialized data, initialized data and heap. So to view/alter the limits of a process, you can use ulimit command.
To Programmatically change the limits you can use getrlimit/setrlimit.

There are two type of limits
The soft limit is the value that the kernel enforces for the corresponding resource. The hard limit acts as a ceiling for the soft limit: an unprivileged process may set only its soft limit to a value in the range from 0 up to the hard limit, and (irreversibly) lower its hard limit.

Ulimit will only change the values for that shell and it's child processes only. It will not change the values system wide and is not permanent.

To set the values permanantly make changes in /etc/security/limits.conf. as root.


answered 12 Oct '14, 16:11

asheshvidyut's gravatar image

accept rate: 0%

nope, if your question is regarding ACM Amrita ICPC Online Problem Lauch Tower. Using just two rows you can calculate the answer, by converting dp[20000,20000] to dp[20000,2], even after that your solution will get TLE verdict, you need another approach of Dynamic Programming. See the editorial by Anudeep,


answered 12 Oct '14, 01:57

asheshvidyut's gravatar image

accept rate: 0%

edited 12 Oct '14, 01:58

I knew that it will get TLE, but that question just made me curious to know about the memory limit of heap and global data segment. Can you please give the approximate memory bounds of both...?

When I allocated [20000,20000] on the heap and also global, it worked fine on my Ubuntu but gave SIGABRT (vector dp(20000,vi(20000))) and sigsegv (int** dp) on codechef

(12 Oct '14, 11:54) sumanth2324★

The maximum memory that you can allocate is 2*10^7, if you declare your array globally. In your question you are allocating 4*10^8 memory space which is not allowed.


answered 12 Oct '14, 13:05

roman28's gravatar image

accept rate: 19%

edited 12 Oct '14, 13:06

@roman8, what about the heap (dynamic allocation) ?

(12 Oct '14, 14:08) sumanth2324★

same as for static. Dynamically you cannot allocate more than 2*10^7 memory space.

(13 Oct '14, 00:32) roman284★ So you mean to say, the heap and the data segment has the same bound on the codechef judge (spoj) ? Why is it so? Any link or reference is appreciated..

(13 Oct '14, 16:34) sumanth2324★

@roman28, what is the emory space limit, it you allocate dynamically ?

(29 May '15, 16:00) sumanth2324★

@sumanth232 If you are running the code on your PC it depends on the RAM available and for OJ problems it's mentioned in the constraints section.

(29 May '15, 19:13) gautam943★
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 12 Oct '14, 00:31

question was seen: 4,577 times

last updated: 29 May '15, 19:13