I'm making a RAM eater program which allocates 64MB inside a while loop until it has filled the requested amount, also print the address of the allocated memory per loop. It works in the memory allocation thing, but is not printing the address
I'm gonna paste all the code so you can track where the variable comes from, and comment the line that isn't working:
void eatRAM()
{
int pCount = 0,
input = 0,
megaByte = 1048576;
unsigned long long toEat = 0,
eaten = 0,
i = 0,
allocFragments = 64 * (unsigned long long) megaByte;
puts("How much RAM do you want to eat? (in Mega Bytes)");
printf("\n>> MB: ");
scanf("%d", &input);
if(input < 64)
{
allocFragments = input;
}
toEat = (unsigned long long)(input * megaByte);
char * pMemory[toEat / allocFragments];
printf("\n\nTotal to eat: %llu Bytes\n", toEat);
do
{
pMemory[pCount] = malloc(allocFragments);
if(pMemory[pCount] != NULL)
{
//NEXT LINE PRINTS:
// < a lot > Bytes were allocated succesfully in 0x00000000
printf("%llu Bytes were allocated succesfully in 0x%p\n", allocFragments, pMemory[pCount]);
for(i = 0; i < allocFragments; i++)
{
pMemory[pCount][i] = 'x';
}
pCount++;
eaten += allocFragments;
}
else
{
puts("\nThere was an error trying to allocate memory. Finishing eating loop\n");
break;
}
}
while (pMemory[pCount] != NULL && eaten < toEat);
puts("----------------------------------");
printf("Total eaten: %llu Bytes\n", eaten);
puts("Check your task manager!\n");
}
In that line, I tried using &, * before the pMemory[pCount] but haven't been able to find a way to print that address.
scanf(), so you don't even know ifinputis valid! Also,allocFragments = inputis wrong because the left-hand side had units of bytes whereas the right-hand side is in megabytes."%p"specifier. Where exactly are you learning c from?%li. Your compiler should have warned you about this.