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.

greg-449
  • 109,219
  • 232
  • 102
  • 145
SP3coder
  • 1
  • 1
  • [Turn on your compiler warnings.](https://gcc.godbolt.org/z/7anypR) – chris Jun 06 '20 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?](https://stackoverflow.com/questions/1328223/when-a-function-has-a-specific-size-array-parameter-why-is-it-replaced-with-a-p) – Daniel Langr Jun 06 '20 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. – Some programmer dude Jun 06 '20 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()`. – Thomas Matthews Jun 06 '20 at 16:43
  • Thanks it solves my doubt. – SP3coder Jun 06 '20 at 16:46
  • Change `void show1(string text1[]) {` to `void show1(string (&text1)[3]) {` – Eljay Jun 06 '20 at 17:01
  • you can see it like this, an array is an address where the array starts in memory, when you pass an array to a function you are passing that address to it, not the array itself. – AndersK Jun 07 '20 at 05:58

2 Answers2

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().

TonySalimi
  • 8,257
  • 4
  • 33
  • 62
  • `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. – Ruslan Jun 07 '20 at 06:30
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.

GobeRadJem32
  • 102
  • 3
  • 14
  • `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](https://godbolt.org/z/n8VFgK). Do you post wrong answers on purpose? – Blastfurnace Jun 07 '20 at 11:33
  • 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. – Blastfurnace Jun 07 '20 at 12:34
  • Sorry, I don't know. Tkanks for this noftification. – GobeRadJem32 Jun 07 '20 at 13:03