This question is a duplicate of https://stackoverflow.com/q/300522/773113, but since that question is on stackoverflow, technically it is not a duplicate. (I tried to mark it as a duplicate, but I was prevented, because the other question is not on Programmers SE.)
So, here is what is happening: it is all a matter of convention, and it is all arbitrary. Different languages and environments have their own conventions, (sometimes even self-contradictory,) and you need to learn the conventions of the language you are using, and follow it.
In the old times when C ruled, "size" was the fixed number of bytes allocated for something, while "length" was the smaller, variable number of bytes actually in use. Generally, "size" stood for something fixed, while "length" stood for something variable. But it was still not uncommon for someone to say that "a machine word is 32 bits long" instead of "the size of a machine word is 32 bits", despite the fact that the number of bits in a machine word is, of course, very fixed.
And then comes java, which has arrays of fixed size, but their size is returned via a length property, and strings of fixed size, but their size is returned via a length() method, and collections of variable size, but their length is returned via a size() method. So, java decided to turn things around.
Then came C#, which keeps the term "length" for stuff of fixed size, but for variable size stuff it uses the term "count", which would be perfect, if it was not for the unfortunate fact that besides being a noun it is also a verb, which can be taken to mean that when you get the "count" of a collection, the items in the collection will be counted one by one. (O(N) instead of O(1).)
So, go figure. There is no definitive answer, be sure to carefully study the documentation of the system that you are dealing with, and to understand the precise definition of the terms "length" and "size" within the context of that system, and be even prepared that there may be no precise definition of these terms, and they may be used interchangeably and arbitrarily.
Answer from Mike Nakis on Stack ExchangeI see the terms "length" and "size" used interchangeably in a lot of places, even in the Linux kernel. Example
- "array size" when referring to number of elements in a array.
- "len" or "length" when referring to sizeof(something) i.e. number of chars (which is a byte in 99.9% of the cases).
An example where this causes confusion is with strings, where the size of a string would likely include the terminating nul byte, wheres the length of the string (i.e. strlen) do not.
Both strlen and sizeof have been around for decades, so why do people not follow this "convention"?
Sorry for the rant!
What is the difference between sizeof() and strlen()
c++ - What is the difference between "length" and "size" of a sequence? - Stack Overflow
difference between sizeof and strlen in c - Stack Overflow
c++ - Why does std::string have a length() member function in addition to size()? - Stack Overflow
IMHO, you're supposed to read integers into std::stack<int> in a for (or while or do-while) loop terminating with a negative input. Then you shall print to stdout the length=size of the sequence (the number of elements) as given by std::stack::size() and next the sum, which you may also compute using some std functionality.
I think the assignment is pretty clear, but perhaps your ability of reading & understanding plain English could be improved?
Based on how I understand the assignment, size is used to mean length.
The length of a C++ container is the number of elements, not its storage capacity.
strlen() is used to get the length of a string stored in an array.
sizeof() is used to get the actual size of any type of data in bytes.
Besides, sizeof() is a compile-time expression giving you the size of a type or a variable's type. It doesn't care about the value of the variable.
strlen() is a function that takes a pointer to a character, and walks the memory from this character on, looking for a null character. It counts the number of characters before it finds the null character. In other words, it gives you the length of a C-style null-terminated string.
The two are quite different. In C++, you do not need either very much, strlen() is for C-style strings, which should be replaced by C++-style std::strings, whereas the primary application for sizeof() in C is as an argument to functions like malloc(), memcpy() or memset(), all of which you shouldn't use in C++ (use new, std::copy(), and std::fill() or constructors).
sizeof is not a method. It is a compile-time construct that determines the amount of memory a particular type or a variable occupies. strlen, on the other hand, is a function that counts the number of consecutive non-zero char values starting at the specified location in memory (which happens to be the same as determining the length of a zero-terminated C string).
As per the documentation, these are just synonyms. size() is there to be consistent with other STL containers (like vector, map, etc.) and length() is to be consistent with most peoples' intuitive notion of character strings. People usually talk about a word, sentence or paragraph's length, not its size, so length() is there to make things more readable.
Ruby's just the same, btw, offering both #length and #size as synonyms for the number of items in arrays and hashes (C++ only does it for strings).
Minimalists and people who believe "there ought to be one, and ideally only one, obvious way to do it" (as the Zen of Python recites) will, I guess, mostly agree with your doubts, @Naveen, while fans of Perl's "There's more than one way to do it" (or SQL's syntax with a bazillion optional "noise words" giving umpteen identically equivalent syntactic forms to express one concept) will no doubt be complaining that Ruby, and especially C++, just don't go far enough in offering such synonymical redundancy;-).