When do you use sizeof ? | SoloLearn: Learn to code for FREE!


When do you use sizeof ?

i know what sizeof is doing, but i cant understand why someone would ever need a variable type in bytes.


9/3/2017 4:13:25 PM

Cain Eviatar

6 Answers

New Answer


well I mostly use sizeof to find array length 😐


I don't know about C++, so I'll answer on what that is in C. sizeof returns the size in bytes of an object. If you need to allocate space in memory for something, you just go malloc(sizeof(x)) this says "allocate in memory a portion of space that is equal to the portion of space needed to allocate x" for instance, sizeof(char c) returns 1, because you take 1 byte to store a char to memory. sizeof(int x) returns 4, because you'll need 4 bytes to store an integer


you said what is sizeof xD but i mean when do you use it ? ( mind to give an example code ? (hard to me to understand without))


For what I can recall of C, you use it a lot when working with pointers it's been a year since I last wrote something in C, so I could really give you a wrong code and that would be more harmful that anything


* Java array.length intensifies *


@very_hard ik that :) is there any other use for it ?