Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7527303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:04:35+00:00 2026-05-30T04:04:35+00:00

I was trying an experiment with malloc to see if I could allocate all

  • 0

I was trying an experiment with malloc to see if I could allocate all the memory available.

I used the following simple program and have a few questions:

int main(void)
{
    char * ptr;
    int x = 100;

    while(1)
    {
        ptr = (char *) malloc(x++ * sizeof(char) / 2);
        printf("%p\n",ptr);
    }

    return 0;
}

1) Why is it that when using larger data types(int, unsigned long long int, long double) the process would use less memory but with smaller data types (int, char) it would use more?

2) When running the program, it would stop allocating memory after it reached a certain amount (~592mb on Windows 7 64-bit with 8GB RAM swap file set to system managed). The output of the print if showed 0 which means NULL. Why does it stop allocating memory after a reaching this threshold and not exhaust the system memory and swap?

I found someone in the following post trying the same thing as me, but the difference they were not seeing any difference in memory usage, but I am.
Memory Leak Using malloc fails

I’ve tried the code on Linux kernel 2.6.32-5-686 with similar results.

Any help and explanation would be appreciated.

Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:04:36+00:00Added an answer on May 30, 2026 at 4:04 am

    1)Usually memory is allocated in multiples of pages, so if the size you asked for is less than a page malloc will allocate at least one page.

    2)This makes sense, because in a multitasking system, you’re not the only user and your process is not the only process running, there are many other processes that share a limited set of resources, including memory. If the OS allowed one process to allocate all the memory it needs without any limitation, then it’s not really a good OS, right ?

    Finally, in Linux, the kernel doesn’t allocate any physical memory pages until after you actually start using this memory, so just calling malloc doesn’t actually consume any physical memory, other than what is required to keep track of the allocation itself of course. I’m not sure about Windows though.

    Edit:
    The following example allocates 1GB of virtual memory

    #include <stdio.h>
    int main(int agrc, char **argv)
    {
        void *p = malloc(1024*1024*1024);
        getc(stdin);
    }
    

    If you run top you get

    top -p `pgrep test`
    PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
    20   0 1027m  328  252 S    0  0.0   0:00.00 test
    

    If you change malloc to calloc, and run top again you get

    top -p `pgrep test`
    PR   NI VIRT  RES  SHR S %CPU %MEM    TIME+ COMMAND              
    20   0  1027m 1.0g 328 S    0  1.3   0:00.08 test
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying experiment with DabbleDB to see if it's something that I could use
As an experiment i am trying to write the following program which allows me
I'm trying to experiment with cocos2d by building a simple card game. I'm not
I am trying to experiment with the jquery template plugin. I have a single
I'm trying to experiment with malloc and free in assembly code (NASM, 64 bit).
I am trying to experiment using basic shaders in my program, I came across
I'm just trying to experiment some ListBox functionality in the SelectionChanged event. I have
I'm trying to understand the concepts of polymorphism and overloading. I have the following
I'm trying to experiment with xampp. After having some space issues, I wanted to
As an experiment I'm trying to write a generic MVP framework. I started with:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.