1

To access any random index in array, we access the following memory location :

a[i] = base address + (size of data_type) * i

This is the reason why arrays have only same type of data.

Now this works perfectly fine when we're working with primitive data types like int, char etc.

However, let's say I have an array of strings where size of each string is different :

string a[] = {"xyz", "abcde", "qwerty"}

How does this work inside memory? Since each string is taking different memory, how does this work out internally?

Edit :

Got the answer for strings. Strings have a constant size internally so it works out.

What happens in case of let's say array of vectors:

vector<int> v[5];

Do they also have a constant size internally?

2
  • 1
    As std::string instances?!? I Don't get your question? If you want to determine the string size, you have to use std::string::size(), sizeof(std::string) is constant. Commented May 8, 2021 at 9:06
  • 1
    @Varum I'd say string memory layout is implementation dependent but you should generally have something like 15 bytes (for short string implementation) plus a pointer (for larger strings). So an array of std::string would be a fixed-size, contiguous array of strings (each 15 bytes plus the size of the pointer in my example, usually 4 bytes). Commented May 8, 2021 at 9:09

1 Answer 1

2

I'd say string memory layout is implementation dependent but you should generally have something like 15 bytes (for short string implementation) plus a pointer (for larger strings). So an array of std::string would be a fixed-size, contiguous array of strings (each 15 bytes plus the size of the pointer in my example, usually 4 bytes)

You can do this simple test: print the address of each string in your array, and then print the address of the first character in each string.

#include <iostream>
#include <string>

int main()
{
    std::string a[] = {
        "Hey",
        "There",
        "The quick fox jumped over the brown dog",
        "Bye!"
    };
    std::cout << a << "\n";
    for (const auto& str : a)
    {
        std::cout << &str << "\n";
        const int* ptr_to_first_char = reinterpret_cast<const int*>(&(str[0]));
        std::cout << "\t" << ptr_to_first_char << "\n";
    }
}

Results for gcc:

0x7ffc46eef050
    0x7ffc46eef060
0x7ffc46eef070
    0x7ffc46eef080
0x7ffc46eef090
    0x13e8eb0
0x7ffc46eef0b0
    0x7ffc46eef0c0
  • You see 16 bytes are actually reserved for each string. E.g. between ...f060, address of 'H' in "Hey", and ...f070, address of std::string("There").
  • You see other 16 bytes are also reserved, probably for the pointer to heap in case the string is bigger than 16 bytes. E.g. between ...f070 and ...f080.
  • All 0x7ffc... addresses should point to the stack, 0x13e8... address to the heap.

https://godbolt.org/z/4zK3c66Mx

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

4 Comments

What happens when instead of array of strings, let's say I have array of vectors? Is the vector stored at array location or reference to that vector ? ( Basically wanted to know about array of objects in general )
@Kancha The idea is similar. A std::vector will be implemented as a data structure with a given layout (e.g. pointer to first member, size, and so on). That data structure will be stored at the array location. Then, for each vector, its elements will be stored contiguously somewhere else. If you check this other online example, godbolt.org/z/b9WanYa87, you'll see vectors are stored contiguously in the stack, starting at the array address, and are sized 24 bytes. Their elements, though, are in the heap, and also stored contiguously.
Makes a lot of sense. So in conclusion, for array of objects, the information for each object will be there at array location, the elements of the object would be stored somewhere else. Thanks!
No. Arrays of objects will store each object at array location. It happens that std::strings and std::vectors are objects with pointers to the heap, so long strings and vector elements are stored in the heap. But not all objects store information in the heap: check godbolt.org/z/1334aTs8Y, where each object is a struct with two ints and the array is a sequence of ints in the end.

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.