Jump to content

Trying to allocate a large chunk of memory in one go.

Gat Pelsinger
#include <stdio.h>
#include <stdlib.h>

int main(){
    unsigned long long *a = NULL;
    a = malloc(1024 * 1024 * 1024);
    if (a == NULL){
        printf("a is NULL");
    }
    else {printf("%p", a);}
    getchar();
    return 0;
}

 

I want to allocate a gigabyte of memory in one go. But it is not working. If I print the value of 'a', I get random numbers and letters, likely being the memory address, and also because I do not understand pointers fully (yes, this is a side question), if I print the value of "*a", I get all zeroes. So, this means the value being pointed by 'a' is zero? What is this exactly supposed to mean? Isn't 'a' taking the memory address of the new allocated memory? Anyways, why this code not workyyyyy.

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

Given your description I don't see anything wrong. It's allocating a gigabyte of memory for unsigned long longs. What do you think it's supposed to do that it isn't doing?

 

Some thoughts:

Printing the value of a pointer, 'a' in this case, with %p tells you the location in memory the pointer is pointing at. Those "random numbers and letters" are the memory address.

 

'*a' is the unsigned long long value stored at the memory address pointed to by 'a'. It's zero because you never assign anything to it and the OS is zeroing the memory it gives you from malloc. While this is generally true in modern operating systems, it should not be assumed when using malloc, calloc guarantees zeroed memory.

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

@Aaron_T But I am on Windows. Nothing of the OOM stuff applies to me I think (unless there is an OOM equivalent on Windows). Based on the reply, I think the problem was solved by changing the pointer type from void to char? Mine is not void anyways.

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

@BobVonBob If it allocates a gigabyte, I should see that in task manager.

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

47 minutes ago, Gat Pelsinger said:

@BobVonBob If it allocates a gigabyte, I should see that in task manager.

Task manager won't show the memory as being used until your program actually starts using it. The allocation is only a promise that you can use 1 GB of memory. The OS does this for exactly this scenario, a program allocates an excessive amount of memory but doesn't use it.

 

If you open the more detailed memory monitoring in task manager by going to the "Performance" tab, you can check the "Committed" section, and that will increase by 1 GB when you run your program.

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

@BobVonBob Oh that's right! If it is actually not using all that memory, then how much can I commit? Basically all the memory I have? And to fill the memory I just I treat my pointer as an array and loop through every index filling it?

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, Gat Pelsinger said:

If it is actually not using all that memory, then how much can I commit? Basically all the memory I have?

You can allocate way more memory than your system actually has. This video shows off what happens really well:

 

4 hours ago, Gat Pelsinger said:

And to fill the memory I just I treat my pointer as an array and loop through every index filling it?

Yes, that's one way to fill the memory. If you want to learn different ways to use the memory you allocate, I highly recommend you find a data structures tutorial. On top of that, I think you'd love videos from Jacob Sorber and Low Level Learning. They make a lot of videos related to these questions you're asking.

Computer engineering grad student, cybersecurity researcher, and hobbyist embedded systems developer

 

Daily Driver:

CPU: Ryzen 7 4800H | GPU: RTX 2060 | RAM: 16GB DDR4 3200MHz C16

 

Gaming PC:

CPU: Ryzen 5 5600X | GPU: EVGA RTX 2080Ti | RAM: 32GB DDR4 3200MHz C16

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×