memory allocation

E

Emil Huseynli

//there is a test prog
//i declare here an array which size is 32MBytes((8*4194304)/(1024*1024))
//but when i run this prog i see,
//that memory usage doesn't increase by 32MBytes
//shouldn't this prog use 32MBytes when running??
#include <stdio.h>
int main()
{
long long int big_array[4194304];
getchar();
return 0;
}
 
R

Richard Tobin

Emil Huseynli said:
//but when i run this prog i see,
//that memory usage doesn't increase by 32MBytes
//shouldn't this prog use 32MBytes when running??

It's a matter for the operating system to decide whether you can "see"
the memory usage. Quite likely it doesn't really allocate it until
it's used. Try modifying some locations in the array.

-- Richard
 
S

santosh

Emil said:
//there is a test prog
//i declare here an array which size is 32MBytes((8*4194304)/(1024*1024))
//but when i run this prog i see,
//that memory usage doesn't increase by 32MBytes
//shouldn't this prog use 32MBytes when running??
#include <stdio.h>
int main()
{
long long int big_array[4194304];
getchar();
return 0;
}

Standard C makes no restrictions on the properties of your program
during runtime, save that a correct program should behave in the
intended manner.

Under modern operating systems with virtual memory, the memory reserved
by a program may not always be immediatly commited to it. It may be
commited only when you access the memory.

While 32 megabytes of virtual memory may be reserved for your program,
it's usage, at any instant, of actual physical memory, will vary
according to the current demands of your program as well as the rest of
the system.
 
B

bert

Emil said:
//there is a test prog
//i declare here an array which size is 32MBytes((8*4194304)/(1024*1024))
//but when i run this prog i see,
//that memory usage doesn't increase by 32MBytes
//shouldn't this prog use 32MBytes when running??
#include <stdio.h>
int main()
{
long long int big_array[4194304];
getchar();
return 0;
}

An optimising compiler might have entirely discarded your
big_array[ ] from the executable program, as it is unused.
--
 
F

Frank Silvermann

santosh said:
Emil said:
//there is a test prog
//i declare here an array which size is 32MBytes((8*4194304)/(1024*1024))
//but when i run this prog i see,
//that memory usage doesn't increase by 32MBytes
//shouldn't this prog use 32MBytes when running??
#include <stdio.h>
int main()
{
long long int big_array[4194304];
getchar();
return 0;
}

Standard C makes no restrictions on the properties of your program
during runtime, save that a correct program should behave in the
intended manner.

Under modern operating systems with virtual memory, the memory reserved
by a program may not always be immediatly commited to it. It may be
commited only when you access the memory.

While 32 megabytes of virtual memory may be reserved for your program,
it's usage, at any instant, of actual physical memory, will vary
according to the current demands of your program as well as the rest of
the system.
I recently wrote a program that stored *a bunch* of longs and did not
see what I was expecting from the vantage point of my popular OS's
memory usage. I was left perplexed. frank
 
J

Joe Wright

Frank said:
santosh said:
Emil said:
//there is a test prog
//i declare here an array which size is
32MBytes((8*4194304)/(1024*1024))
//but when i run this prog i see,
//that memory usage doesn't increase by 32MBytes
//shouldn't this prog use 32MBytes when running??
#include <stdio.h>
int main()
{
long long int big_array[4194304];
getchar();
return 0;
}

Standard C makes no restrictions on the properties of your program
during runtime, save that a correct program should behave in the
intended manner.

Under modern operating systems with virtual memory, the memory reserved
by a program may not always be immediatly commited to it. It may be
commited only when you access the memory.

While 32 megabytes of virtual memory may be reserved for your program,
it's usage, at any instant, of actual physical memory, will vary
according to the current demands of your program as well as the rest of
the system.
I recently wrote a program that stored *a bunch* of longs and did not
see what I was expecting from the vantage point of my popular OS's
memory usage. I was left perplexed. frank

Thank you for sharing. I hope you've gotten over the perplexity. Clearly
*a bunch* is undefined in C and may contribute to your perplexedness.
 
W

Walter Roberson

Frank Silvermann said:
I recently wrote a program that stored *a bunch* of longs and did not
see what I was expecting from the vantage point of my popular OS's
memory usage. I was left perplexed. frank

Did you try calling sysconf(_SC_BUNCH_MAX) (via unistd.h) to find the
maximum size of a bunch on your system?
 
K

Keith Thompson

bert said:
Emil said:
//there is a test prog
//i declare here an array which size is 32MBytes((8*4194304)/(1024*1024))
//but when i run this prog i see,
//that memory usage doesn't increase by 32MBytes
//shouldn't this prog use 32MBytes when running??
#include <stdio.h>
int main()
{
long long int big_array[4194304];
getchar();
return 0;
}

An optimising compiler might have entirely discarded your
big_array[ ] from the executable program, as it is unused.

In addition, whatever method you used to determine how much memory
your program uses is entirely outside the scope of the C standard.
 
M

Malcolm

Emil Huseynli said:
//there is a test prog
//i declare here an array which size is 32MBytes((8*4194304)/(1024*1024))
//but when i run this prog i see,
//that memory usage doesn't increase by 32MBytes
//shouldn't this prog use 32MBytes when running??
#include <stdio.h>
int main()
{
long long int big_array[4194304];
getchar();
return 0;
}
Try actually using the array - memset it to zero and then iterate over it
adding up the values, then print the result out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top