0

I am unable to allocate memory using the following code:

int *h_VC = (int *)malloc(sizeof(int)*SIZE); //SIZE is 19200 if(h_VC==NULL) { printf("Memory Not avaialble"); } 

My code uses the above block in a while loop and is run several times. I have 8GB memory. I am monitoring the free memory at the same time when running the code.
The memory allocation is failing although i have arround 3GB of free memory left.
What could be the problem?

9
  • I assume you're using a 64 bit OS? Commented Dec 13, 2011 at 13:02
  • 2
    Just a note: don't cast the returned pointer of malloc. Theres no need to and it is possibly dangerous. Commented Dec 13, 2011 at 13:03
  • Don't know if this is the case, but it might help: stackoverflow.com/questions/605845/… Commented Dec 13, 2011 at 13:04
  • I test it,and it works fairly Commented Dec 13, 2011 at 13:09
  • 3
    If it's a 32-bit program running in a 64-bit OS, you can still have plenty of free memory in the OS, but exhausted address space in the program due to memory fragmentation or leaks or just too many too big allocations. Which one it is it's hard to tell without any additional information about the program and OS. Commented Dec 13, 2011 at 13:31

2 Answers 2

5

Anything could be the problem. Replace the printf with

perror(""); 

to get a hint.

Sign up to request clarification or add additional context in comments.

2 Comments

Except beer... Beer is never the problem... +1
Beer is the cause and the solution to all problems in life. - Homer J. Simpson
0

I am using Visual Studio as a compiler. Compiling the program as x64 solved the issue.
thanks to Alex's comment.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.