String array

M

Mark S.

Hi,
let's say I have following array of strings:
string s[] = { "This ", "is ", "a ", "very strange test." };

Am I correct in my line of thought that since each element somehow has a
size of 4 and the total size of the array is 16, I can use
sizeof(s)/sizeof*(s) as I would with other built-in types?
Or is there something else to consider? And why is the size of the
string always 4?

Kind regards
Mark
 
B

Bart van Ingen Schenau

Hi,
let's say I have following array of strings:
string s[] = { "This ", "is ", "a ", "very strange test." };

Am I correct in my line of thought that since each element somehow has a
size of 4 and the total size of the array is 16, I can use
sizeof(s)/sizeof*(s) as I would with other built-in types?

Yes, you can use the sizeof/sizeof method on any array, regardless of
the base type. It even works for non-built-in types (as you already
noticed, because std::string is technically also a user-defined type).
Or is there something else to consider? And why is the size of the
string always 4?

sizeof(std::string) only gives you the amount of bookkeeping data that
std::string uses. The actual string content is stored in a separate
buffer, which is dynamically allocated.
Kind regards
        Mark

Bart v Ingen Schenau
 
M

Mark S.

Bart said:
Hi,
let's say I have following array of strings:
string s[] = { "This ", "is ", "a ", "very strange test." };

Am I correct in my line of thought that since each element somehow has a
size of 4 and the total size of the array is 16, I can use
sizeof(s)/sizeof*(s) as I would with other built-in types?

Yes, you can use the sizeof/sizeof method on any array, regardless of
the base type. It even works for non-built-in types (as you already
noticed, because std::string is technically also a user-defined type).
Or is there something else to consider? And why is the size of the
string always 4?

sizeof(std::string) only gives you the amount of bookkeeping data that
std::string uses. The actual string content is stored in a separate
buffer, which is dynamically allocated.
Kind regards
Mark

Bart v Ingen Schenau
Alright, thank you!
 
P

Pascal J. Bourguignon

Mark S. said:
Hi,
let's say I have following array of strings:
string s[] = { "This ", "is ", "a ", "very strange test." };

Am I correct in my line of thought that since each element somehow has
a size of 4 and the total size of the array is 16, I can use
sizeof(s)/sizeof*(s) as I would with other built-in types?
Or is there something else to consider? And why is the size of the
string always 4?

sizeof(std::string) is not always 4, it depends on the implementation.
It might be three long words (eg. 12 chars) or more, or less.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top