hi guys, i have this code

#include <stdio.h>

#include <stdlib.h>

int main(){

    unsigned long long *x;

    x = (unsigned long long*)malloc(600851475143ULL*sizeof(unsigned long long));

    if (x!=NULL){

                 printf("success\n");

                 getchar();

                 }

    else {

         printf("fail\n");
         
         getchar();

         }

printf("%I64u\n",600851475143ULL);

         getchar();

return 0;

}

this is a part of some program im trying to make, i just want to ask you, it compiles fine, the dynamic memory allocation works fine but i get this warning which i cant get rid of

6 C:\..\test.c [Warning] large integer implicitly truncated to unsigned type

any help on how to deal with it?

thanks :)

The only thing that comes to mind is in your malloc call the value is an unsigned long long, but malloc takes a value of type size_t. size_t is probably going to be unsigned long or unsigned int instead of unsigned long long, so your unsigned long long value gets truncated to fit size_t.

The only way to fix it I can think of is to break the big chunk down into an array of smaller chunks and call malloc multiple times. Or use a heap allocator that can handle unsigned long long sizes.

Wait, why do you need to malloc? Wouldent:

unsigned long long  int *x = 600851475143ULL;

Work the same way?

The printf symbol for long long unsigned int is %llu.

In C99 it is. The I64 size is a Microsoft CRT extension, but the Microsoft CRT doesn't support C99, and %llu doesn't work.

http://msdn.microsoft.com/en-us/library/56e442dc(VS.71).aspx
http://msdn.microsoft.com/en-us/library/tcxf1dw6(VS.71).aspx

Wait, why do you need to malloc? Wouldent:

unsigned long long  int *x = 600851475143ULL;

Work the same way?

malloc allocates a number of bytes given by the argument and returns a pointer to heap memory of that size. The original code allocates 600851475143ULL * sizeof(unsigned long long) bytes. Your code points x to the address 600851475143ULL and probably won't work on a protected memory system and certainly not on a system without gobs of memory. Though the malloc approach has a similar problem where allocating that much memory is questionable.

This article has been dead for over six months. Start a new discussion instead.