When do you use sizeof ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

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.

3rd Sep 2017, 4:13 PM
Cain Eviatar
Cain Eviatar - avatar
6 Answers
+ 3
well I mostly use sizeof to find array length 😐
3rd Sep 2017, 6:36 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 1
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
3rd Sep 2017, 5:43 PM
Michael Vigato
Michael Vigato - avatar
+ 1
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))
3rd Sep 2017, 5:49 PM
Cain Eviatar
Cain Eviatar - avatar
+ 1
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
3rd Sep 2017, 5:50 PM
Michael Vigato
Michael Vigato - avatar
+ 1
* Java array.length intensifies *
3rd Sep 2017, 6:37 PM
Michael Vigato
Michael Vigato - avatar
+ 1
@very_hard ik that :) is there any other use for it ?
3rd Sep 2017, 6:49 PM
Cain Eviatar
Cain Eviatar - avatar