0

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.

13
  • Your code is too messy, that means that it can easily contain bugs. Commented Dec 1, 2015 at 7:53
  • 1
    You don't check the return value of scanf(), so you don't even know if input is valid! Also, allocFragments = input is wrong because the left-hand side had units of bytes whereas the right-hand side is in megabytes. Commented Dec 1, 2015 at 7:54
  • 1
    You was told before, use the "%p" specifier. Where exactly are you learning c from? Commented Dec 1, 2015 at 7:54
  • I updated my code to look easier to read... I just used a do while loop better. And using the %p is printing 00000000 Commented Dec 1, 2015 at 8:02
  • 2
    Yes, you used the wrong format specifier : %li. Your compiler should have warned you about this. Commented Dec 1, 2015 at 8:17

1 Answer 1

3

You have the declaration:

unsigned long long … allocFragments = 64 * (unsigned long long) megaByte;

and also had the call to printf():

printf("%li Bytes were allocated succesfully in 0x%p\n", allocFragments, pMemory[pCount]);

With this code, your problem is that you're passing an unsigned long long (8 bytes) to printf(), but telling it to print long (4 bytes), so it is using some of the information you gave, but leaving the rest (mostly zeroes) to be interpreted as part of the memory pointer.

Since you're playing with MiB and GiB of memory, and you claim you're getting 00000000 for your null pointer, you're probably on a 32-bit system, and therefore you have 4-byte pointers, and the pointer you're printing is actually the zero bytes in the more significant part of allocFragments.

Update: The printf() has now been updated to:

printf("%lli Bytes were allocated successfully in 0x%p\n", allocFragments, pMemory[pCount]);

In theory, that should fix the problem. …And there's a comment that it did!…

Incidentally, if you're using GCC as your compiler, it should have been warning you about a type mismatch between the format and the value to be printed. If it wasn't, you're not using the warnings it can provide properly. I normally compile using these options — sometimes turning on some extra ones.

gcc -std=c11 -g -O3 -Wall -Wextra -Werror -Wmissing-prototypes \
    -Wstrict-prototypes -Wold-style-definition …
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for replying! You mean I'm on a 32 bit SO? Actually I'm using Windows 8.1 64 Bit, may be I need to compile it for 64 Bit? and I also made that change, %li > %llu
OK; well, it is relevant (it turns out) to mention Windows and 64-bit. Does MSVS compile in 64-bit by default on that platform? I don't use it. I was working from the 8 zeros printed from the null address, plus the fact that you were getting zero printed as the address. It would have accounted for the zeros. There's an outside chance it still would even if you're working with a 64-bit compilation, but it isn't what I'd bet on — whereas with 32-bit mode operations, it was a very reasonable guess. And having a mismatch between format string and type is still undefined behaviour — aka BAD!
Checking all the warnings and making sure I was using the proper parameters in the printf functions made everything work properly! -- i.imgur.com/Xa5Vm7j.jpg
Phew! I'm relieved. Compilers don't emit the warnings for fun. They needn't emit any. They do it when they're reasonably convinced there's a problem that should be fixed. Always, but always, pay attention to the warnings and fix them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.