PREV INDEX NEXT

Author: Stan Eisenstat
Subject: Re: [Cs223] Malloc allocates wrong amount of memory
Date: Saturday, 18 Apr 2020, 07:34:55


    > Message Posted By: Unknown
    >
    > I'm using malloc to allocate a certain amount of memory, but when I print
    > the sizeof() the allocated memory is different.
    >
    >   int newArraySize = 16;
    >   htbl[tablePos] = malloc(newArraySize);
    >   printf("---- %d, %d\n", sizeof(htbl[tablePos]), newArraySize);
    >
    > which then prints
    >   ---- 8, 16
    >
    > Why would this be happening?

Because sizeof() is printing the size of htbl[tablePos],
which is presumably a char *, not the amount of storage
allocated.

Remember that sizeof() is evaluated at compile-time, not
run-time.

--Stan-
PREV INDEX NEXT