0

I am trying to compile the following code:

#include <iostream>
using namespace std;
void show1(string text1[]) {

    cout << "Size of array text1 in show1: " << sizeof(text1) << endl;
}
int main() {
    string text1[] = {"apple","melon","pineapple"};
    cout << "Size of array text1: " << sizeof(text1) << endl;
    cout << "Size of string in the compiler: " << sizeof(string) << endl;
    show1(text1);
    return 0;
}

And the output is shown below:

Size of array text1: 96
Size of string in the compiler: 32
Size of array text1 in show1: 8

I am not able to understand, why is the sizeof operator working on the same array giving two different outputs at two different points? Please explain.

7
  • Turn on your compiler warnings. Commented Jun 6, 2020 at 16:37
  • 4
    Does this answer your question? When a function has a specific-size array parameter, why is it replaced with a pointer? Commented Jun 6, 2020 at 16:39
  • When you declare arguments, arrays are really pointers. So the argument text1 for your show1 function is really declared as string* text1. And getting the sizeof of a pointer is the size of the pointer itself, not what it might point to. Use std::vector or std::array instead. Commented Jun 6, 2020 at 16:39
  • 1
    Reminder: sizeof(string) is the size of the std::string structure, not the size of the text in the string. See also std::string::length(). Commented Jun 6, 2020 at 16:43
  • Thanks it solves my doubt. Commented Jun 6, 2020 at 16:46

2 Answers 2

3

The sizeof() operator returns the compile time size of the objects. It means that if your type allocates a memory chunk at run time from heap, that memory is not taken into account by sizeof().

For your first case, i.e.

 string text1[] = {"apple","melon","pineapple"};

You have an array of 3 strings, so sizeof should return 3*sizeof(std::string). (3*32 = 96 in your case)

For your second case:

sizeof(string)

It should simply print the size of an string. (32 in your case).

Finally for your last case, do not forget that arrays are passed using a pointer in C/C++. So, your parameter is simply a pointer and sizeof() should print the size of a pointer on your machine.

Edit: As @ThomasMatthews has mentioned in the comments, if you are interested in getting the real size of an string (i.e. the number of characters inside it), you can use std::string::length() or std::string::size().

Sign up to request clarification or add additional context in comments.

1 Comment

sizeof is not a function, it's an operator. And the parentheses are only necessary when its operand is a type, which makes it even less of a function.
0

Try with member function 'size'.

Write this code:

#include <iostream>
using namespace std;
void show1(string text1[]) 
{
    cout << "Size of array text1 in show1: " << text1->size() << endl;
}

int main() 
{
    string text1[] = {"apple","melon","pineapple"};
    cout << "Size of array text1: " << text1->size() << endl;
    cout << "Size of string in the compiler: " << sizeof(string) << endl;
    show1(text1);
    return 0;
}

Description:

std::vector has a member function size(). And std::string too. In std::vector return size of vector(all elements). In std::string returns all elements in array.

3 Comments

text1-size() is a syntax error. If you change it to text1->size() it will compile but it doesn't show the size of the array. Do you post wrong answers on purpose?
Once again, text1->size() doesn't show the size of the array. An array passed to a function decays to a pointer. You are printing the size of the first std::string in the array.
Sorry, I don't know. Tkanks for this noftification.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.