Search code examples
cmemoryheap-memorycalloc

Reading and writing dynamically allocated memory


I have some code with following parts:

typedef unsigned long long int ulli_t;
typedef unsigned int obj_t;
// --- SOME UNIMPORTANT PART OF CODE
objects = (obj_t*)malloc(hw*sizeof(obj_t));
objNums = (ulli_t*)calloc(hw, sizeof(ulli_t));
printf("%#x, %#x\n", objects, objNums);
// --- SOME UNIMPORTANT CODE
/// NEXT PART IS for-CYCLED
objNums[k] ++;
if (k>=Nobj) {
   objects[k] = obj;
   Nobj++;
   printf(" %lu - %#x - %#x\n", k, objects+k, objNums+k);
};

And I've got an output of this part of code:

0xaafdf9d8, 0xaad693b0
 0 - 0 - 0xaafdf9d8
 1 - 0 - 0xaafdf9dc
 2 - 0 - 0xaafdf9e0
 3 - 0 - 0xaafdf9e4
 4 - 0 - 0xaafdf9e8
 5 - 0 - 0xaafdf9ec
 6 - 0 - 0xaafdf9f0
 7 - 0 - 0xaafdf9f4
 8 - 0 - 0xaafdf9f8
 9 - 0 - 0xaafdf9fc
 10 - 0 - 0xaafdfa00
 11 - 0 - 0xaafdfa04

So, here is the question - If 'objects' variable is non-zero then why does 'objects+k' is zero? When I make :

printf(" %lu - %#x - %#x\n", k, objects[k], objNums[k]);

I get lines like:

value of k - 0 - hex value of objNums[k]

Why is that? I already got my brain broken thinking about that thing.


Solution

  • You are using %lu to print your long long. You should use %llu.