4

I was arguing with one of my friends, if we have defined an array/pointer:

sometype * p;

if we want to know the size of the type, he said one should use:

sizeof(sometype)

I said one could also use:

sizeof(p[0]),

it should be the same. He disagreed, and his point is if p is not initialized, then this usage of p[0] may cause problems.

But to my knowledge, only when we change or depend on the value of p[0], will this be harmful. Since we don't change p[0] nor use p[0]'s value, this is totally sensible.

Since we can't persuade each other, can anyone make this concept clear.

I know this question is of no use, and almost all the developers will use sizeof(sometype), including me :). But this question is fun and I'm really curious to know if there is any case that sizeof(p[0]) will be harmful.

=================================
Let me extend this question a little bit. If I do use some irrelevant value (or considered as kind of random or whatever). Can I always use p[0] to get a value?

To make the question more clear, If I don't change the value of an uninitialized pointer, just use the value it is pointing to, can I always get a value without causing problems?

RainSia
  • 113
  • 1
  • 7
  • 2
    Hamless? That's a shame. I love ham! – ThiefMaster Dec 26 '12 at 15:59
  • 2
    `sizeof p[0]` (or `sizeof *p`) is actually safer, because you might change the type of `p` but forget to change the `sizeof` statement. – Nikos C. Dec 26 '12 at 16:02
  • Lol, did I really say hamless? If I did, then sorry about the typo. Funny question, funny typo, though. – RainSia Dec 26 '12 at 16:04
  • @NikosC Still I'm often talked off when I suggest using `malloc(sizeof *p)` instead of `malloc(sizeof T)`... –  Dec 26 '12 at 16:37

4 Answers4

10

The argument to sizeof is an unevaluated operand, so pointer dereferences and so forth don't have to refer to valid memory. It's purely based on type information.

(As a corollary, sizeof *p is not polymorphic, it uses the static type only.)

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
8

sizeof is evaluated at compile-time so it does not matter whether the memory is initialized or not since that's a runtime thing.

So sizeof p[0] or - what I think is even nicer since it does not look array-ish - sizeof *p is a great way to avoid typing the type again when using sizeof.

Community
  • 1
  • 1
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
7

In C++ language the argument of sizeof is never evaluated, meaning that it is completely irrelevant whether the variables used in the argument expression hold valid values or not. Only the static type of the expression under sizeof matters, and it is determined at compile time.

Moreover, it is actually preferable to use expressions (as opposed to type names) as arguments of sizeof to make the code more type-independent.

In C the argument expression might be evaluated in some cases (variable-length arrays), but not in C++.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
2

sizeof() is evaluated by the compiler, so uninitialized variables are not an issue.

brian beuning
  • 2,836
  • 18
  • 22