Displaying the amount of memory allocated
Hi I'm writing some test stuff to see the amount of memory that allocated displayed correctly
in Windows TasK manager. Below is the code;
int main(int argc,char* argv[])
{
struct stat st;
char commandXCopy[2开发者_运维知识库00];
char commandDelete[200];
char *fNames[2^16];
int i =0;
char *ptr = (char *)malloc(sizeof(char) * 2^32);
printf("\nTEST");
if(!ptr)
printf("\nCan not allocate");
else
printf("\nMemory allocate");
while(1==1)
{
};
I try to make huge allocations from stack and heap. But all I see on task manager->processes is ~800K.
And I don't see "Can not allocate" message either.
I have Windows 32bit XP Pro and use gcc and application is a dos application.
gcc test.c
Regards
I think you may be suffering under a misapprehension: 2^32
is not 232 (4G, assuming your bytes are eight bits long, which I will for the purposes of this answer) in C.
^
is the bitwise XOR operator. So what you're actually allocating is:
binary hex decimal
--------- ---- -------
0010 0000 0x20 32
xor 0000 0010 0x02 2
=========
0010 0010 0x22 34
or 34 bytes. Similarly, 2^31
would give you 29 bytes so what you may think should be a 2G difference (232 - 231) is really only 5 bytes.
If you want to do powers in C, you should look at the pow()
function but I doubt you'll be able to get 4G of memory (perhaps on a 64-bit OS but even then, that's an awful lot).
And just one other thing: sizeof(char)
is always 1 - there's no need to multiply by it.
精彩评论