Pointers and logic trouble

Hello everyone. Let's just move on my problem. Here's the code:
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
int main()
{
  int a[999999];
  int *ptr=new int;
  delete ptr;
   return 0;
}

Let's say we have a maximum of 4Mb memory limit for our program , retrieved
from the memory of system. If we declare an array of integers of exactly 4MB-4B, that is ,
999999 elements, we have left only one cell of mem. remained, used for
declaration of pointer *ptr of type int. Now all the memory allocated for the program is consumed. But this should
not be a problem when we initialize the pointer with new int, because from what I've seen on the internet,
it shoult take up a memory cell not from the memory of program BUT from the system memory, which is MUCH MORE huger.
Though when I initialize it with new int I'm given the overflow error, which is not what I expected, so it
seems like new int doesn't allocate memory cells from the system memory but from program's. So my question is,
what's really going on with new int? And assuming that new int allocate from program memory, I expected to
get rid of error when I delete it using delete keyword( that is, return it back to the system), which is NOT happening. So I'd also
like to know what's wrong with delete keyword too. Thanks in advance!
A program has code too. That has to be loaded somewhere, consuming memory.

Automatic variables (like your array and pointer) are in "stack memory" and dynamically allocated objects elsewhere, but everything within a process is in common address space. What exactly is limited to 4Mb (or is it 4MB -- an 8-fold difference)?

Did you test this:
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
int main()
{
  int a[999999];
  int *ptr = nullptr; // new int;
  // delete ptr;
   return 0;
}


(With aggressive optimization options a compiler would not need to allocate any memory for the array and pointer, because they are not used.)
you can change how much stack you have on a lot of systems/compilers also. Windows allows it, I forget the max.

also, depending on the system, many systems swap ram to hard disk and back, so you can allocate quite a bit (never more than you have ram for, on normal machines) without breaking the machine as long as it has enough ram to run your code, the critical OS functions, it can manage the rest.

I have allocated over 5 GB in a single array before (pointer array, not [] array). Modern machines don't even blink at that ... my home pc has 32 GB, for example, and the stuff at work has 3 times that on the smallest machines.

delete only throws an error if it has an error. Deleting something that was successfully allocated almost never fails, I can't recall ever seeing that. It usually bombs when you try to deallocate something you did not actually allocate successfully, such as

int x;
int * duh = &x;
delete[] duh; //oops!

Try asking again. What exactly do you not understand, or need explained, after reading this far..?

generally if > about 1000, I use a pointer.
so int*a = new int[999999]; will work where
int a[999999] is too big for the stack.

Last edited on
Topic archived. No new replies allowed.