Allocate memory in 1GB-chunks and 1kB-chunks.

Hello everyone! I have a assignment where i am supposed to allocate memory in different size-able chunks, or so i think. Something like the size of the blocks should be 1GB and 1kB. I have been looking all over the internet for information regarding the size of the allocated memory i cant really understand it. I have made a program what thought was correct but i see no difference when executing them. All my memory gets used up until bad_alloc occurs. So am i missing something? I really dont understand.

Does this mean the array is a 5-byte chunk or is this 5 chunks of 1 bytes? I am so confused.
 
int *q = new int[5];


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <new>

//using namespace std;

int main() {
	try {
		std::cout << "1. Allokera 1GB-bitar" << std::endl;
		std::cout << "2. Allokera 1KB-bitar" << std::endl;
		
		int menubutton;
		std::cin >> menubutton;

		if (menubutton == 1)
		{
			int test1gb = 1;
			while (test1gb == 1) {
				int *q = new int[1073741824];
			}
		}
		else {
			int test1kb = 1;
			while (test1kb == 1) {
				int *p = new int[1024];
			}
		}
	}catch (std::bad_alloc e) {
		std::cerr << e.what() << std::endl; // Körs denna rad?
	}
	return 0;
}
Last edited on
You ask for memory over and over until it's all gone. What do you think should happen differently?
Well i am not sure to be honest. Based on how my assignment is i assumed that it would be two different outcomes but they are the same(identical) so thats why i am asking here is my program correct and does it really mean that my arrays are of a different size or are they just of a different length? I am still quite uncertain of it all.

I have to answer two different questions. They go like this loosely translated.

"Examine how much memory your program can allocate before your OS says "no". Do this by allocating memory in 1GB-bits(chunks)"

"Examine what happens in your OS when a program is leaking memory. Do this by allocating in memory in 1kB-bits(chunks)"

So this made me think it would be two different experiences.
Topic archived. No new replies allowed.