0

What are the differences between sizeof(), size(), strlen() ? In which cases I should use a certain function?

जलजनक
  • 3,072
  • 2
  • 24
  • 30
  • 8
    Have you read doc? [`sizeof`](https://en.cppreference.com/w/cpp/language/sizeof), [`strlen`](https://en.cppreference.com/w/cpp/string/byte/strlen), [`size`](https://en.cppreference.com/w/cpp/iterator/size). – Jarod42 Jan 23 '21 at 08:28
  • 2
    To begin with, `sizeof` is an operator and a keyword in the language. The `strlen` function is a function inherited from C. `size` can mean different things depending on what it's defined as, and there's no standard global `size` function. – Some programmer dude Jan 23 '21 at 08:34
  • @Someprogrammerdude oh yes. You're right! – ТарасПрогер Jan 23 '21 at 08:52
  • Does this answer your question? [difference between sizeof and strlen in c](https://stackoverflow.com/questions/8590332/difference-between-sizeof-and-strlen-in-c) – DavidW Jan 23 '21 at 13:42
  • The duplicate I've suggest doesn't include `size` but it's definitely something you should have read before posting this – DavidW Jan 23 '21 at 13:43

4 Answers4

2

size() function is used to return the size of the list container or the number of elements in the list container. You can use this to find number of elements in an array.

sizeof() is used with the data types such as int, float, char… etc it simply returns the amount of memory is allocated to that data types.

The strlen() function in C++ returns the length of the given string. It returns the length of the null terminated byte string.

itsDV7
  • 854
  • 5
  • 12
2
  • Sizeof operator: it is a compile-time operator (and not a function) that determines the size, in bytes, of a variable or data type. The sizeof operator can be used to get the size of arrays, classes, structures, unions and any other user defined data type.
  • Size function: It is used to return the size of the set container or the number of elements in a container such as std::set, std::vector, and many others.
  • strlen function: The strlen function takes a null terminated byte string as its argument and returns its length.
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
2

size() is for the Number of elements in X, for example if a={1,2,3,4} , size(a)=4.

sizeof() is for the size(memory) of the element , for example sizeof(int)=4.

strlen() is the size of strings, for example if a = "1234" . strlen(a) = 4.

-1

sizeof() is used to calculate size of your function and is used in dynamic memory allocation like malloc. It takes input as no. of elements * size of 1 element of that type. strlen() returns the length of the string. size() returns number of elements.

  • 2
    _sizeof() is used for dynamic memory allocation like in malloc._ No! This is complete wrong. There is no (standard conform) possibility to determine the size of dynamically allocated memory except remembering (storing) the size of what was allocated. (At best, you can decrement the pointer of by a certain amount of bytes to address the internal book-keeping of heap allocation but only if you know how it is working and only for a specific implementation.) – Scheff's Cat Jan 23 '21 at 09:54