2

I was just wondering: since sizeof()'s return type is size_t, why does sizeof(size_t) give me 4? That is, when I malloc(someSize), did I asked for someSize bytes or someSize*4 bytes ?

I've been doing an ASM homework for two days, and I'm getting pretty confused now. Thanks for your help!

VoidStar
  • 35
  • 4
  • 2
    You need to explain the underlying reasoning behind this question better. Why on Earth would you expect `sizeof(size_t)` to be 1??? This is entirely unclear from what you stated in your question. `sizeof` measures sizes in therms of `sizeof(char)`, so `sizeof(char)` is indeed always 1. But why would you expect `sizeof(size_t)` to be 1? – AnT Sep 28 '13 at 16:28
  • Your question doesn't really make sense. The size of a variable is not correlated with the value in it. What you're asking is equivalent to asking why multiplying a number by any `int` isn't always equivalent to multiplying that number by 4. – Crowman Sep 28 '13 at 16:30
  • This is a great question actually, to distinguish between the concepts of a numerical value, and the size of the type used to represent that numerical value. –  Sep 28 '13 at 16:36
  • The only time `sizeof(size_t)` would return 1 would be if `sizeof(int)` returned 1 (conceptually possible, but very unlikely), which in turn would mean `CHAR_BIT` is 16 (bare minimum) or 32. All more hypothetical than real. If CHAR_BIT is 8, then a if `sizeof(size_t)` was 1, you could not allocate arrays with more than 256 elements, etc. – Jonathan Leffler Sep 28 '13 at 16:38

3 Answers3

3

According to the 1999 ISO C standard (C99), size_t is an unsigned integer type of at least 16 bit (see sections 7.17 and 7.18.3).

The size_t is an unsigned data type defined by several C/C++ standards, e.g. the C99 ISO/IEC 9899 standard, that is defined in stddef.h.1 It can be further imported by inclusion of stdlib.h as this file internally sub includes stddef.h.

This type is used to represent the size of an object. Library functions that take or return sizes expect them to be of type or have the return type of size_t. Further, the most frequently used compiler-based operator sizeof should evaluate to a constant value that is compatible with size_t.

This was discussed in this post: What is size_t in C?

And when you do malloc(sizeof(int)) it allocates four bytes of memory dynamically. Don't confuse between size_t and sizeof(size_t).

Community
  • 1
  • 1
Tonmoy
  • 529
  • 2
  • 4
  • 17
2

You're confusing the number of bytes necessary to represent some value and the number of bytes necessary to represent that size. Or, as @Mat put it in a comment, you're confusing the type and the unit. sizeof(T) reports how many bytes (chars) a value of type T occupies. However, because some types can be quite large, occupying thousands or even millions of bytes (think large arrays), this value has to be stored using an integral type that itself occupies several bytes (size_t).

As an analogy, write out the number 10^100 (hypothetically), and then count how many decimal digits this number has. Write up the latter number (hint: it's 100). 100 itself takes several decimal digits to write out, but that doesn't affect how many digits 10^100 occupies.

1

why does sizeof(size_t) give me 4?

Because of size-t is of type unsigned long int, sizeof(size_t) will give the size of unsigned long int on your machine (on my machine--32 bit-- it is giving 4 bytes).

#include <stdio.h> 

 int main ()
{
     printf("%d\n", sizeof(unsigned long int));
     printf("%d\n", sizeof(size_t));
     return 0;
}

did I asked for someSize bytes or someSize*4 bytes ?

Answer is: someSize, because sizeof function determines the size of the data type (in bytes). Return type of sizeof is size_t.
So, you can say sizeof determines size of the data type (in bytes) while returns size_t type data.

haccks
  • 97,141
  • 23
  • 153
  • 244
  • 1
    `size_t` is not `int`. –  Sep 28 '13 at 16:25
  • ... and an int is not necessarily 4 bytes. – Mat Sep 28 '13 at 16:26
  • Alright, but if `X` is the return unit of `sizeof()`, how `sizeof(X)` can be different from 1 ? – VoidStar Sep 28 '13 at 16:26
  • The second part is wrong. `malloc(3)` never allocates 3*4 = 12 bytes, regardless of the `sizeof` any type involved. It always allocates 3 bytes. -1 –  Sep 28 '13 at 16:29
  • @delnan; malloc(3) allocates 3 chars, not 3 bytes. A char could be 4 bytes. – pburka Sep 28 '13 at 18:03
  • @pburka As far as the C standard is concerned, "byte" means `char` by definition. This is consistent with the older, broader definition of byte as smallest addressable memory unit, although as you point out it clashes with the now-ubiquitous "byte = 8 bits". But then again, `char` being 8 bits is equally ubiquitous (I know of one embedded platform where it's not true, but POSIX requires it and many other platforms do it too). –  Sep 28 '13 at 20:28