Sign in to follow this  
Tispe

size of struct

Recommended Posts

Tispe    1468
I want to know how a struct object varies in size with a string or char[] member variable.

It seems the object is always 32 byte long nomatter what the string is. Is the string actaully in heap and the variable name is really just a pointer to a dynamic string array?

I am asking because I want to send structs over WinSock and I need to cast a Char Buffer[] into a struct.

[code]
#include <iostream>
#include <string>

using namespace std;

struct Obj{
string name;
};

void main(){

Obj test1, test2;
test1.name = "123";
test2.name = "1234";

cout << "test1: " << sizeof(test1) << " " << test1.name << endl;
cout << "test2: " << sizeof(test2) << " " << test2.name << endl;

test1.name = "12345678901234567890123456789012345678901234567890";
test2.name = "test1234aedgegy";

cout << "test1: " << sizeof(test1) << " " << test1.name << endl;
cout << "test2: " << sizeof(test2) << " " << test2.name << endl;

system("pause");
}
[/code]

Share this post


Link to post
Share on other sites
UltimaX    468
It's always 32 bytes because your structure has a std::string, which is 32 bytes. So you're basically doing sizeof(std::string)

You may want to try:
test1.name.size()
test2.name.size()

Which will give you the length of the string.

Share this post


Link to post
Share on other sites
Buckeye    10747
The structure, as you suspect, contains only a reference to a string object, not the string itself.

NOTE:
Don't use sizeof( [i]variable-name[/i])! Use sizeof( [i]data-type[/i] ). [b]sizeof()[/b] is [b]not[/b] a function, it's a compile-time operator and doesn't calculate anything at run-time.

You'll have to use name.length() or something similar.

EDIT: ninja'd Edited by Buckeye

Share this post


Link to post
Share on other sites
Molle85    172
The compiler will pad structs and classes to speed up reading/writing to it, if you want to want your struct to spend as little space as possible then you need to pack it
[url="http://msdn.microsoft.com/en-us/library/2e70t5y1%28v=vs.80%29.aspx"]http://msdn.microsoft.com/en-us/library/2e70t5y1%28v=vs.80%29.aspx[/url]

Share this post


Link to post
Share on other sites
Bregma    9214
[quote name='Tispe' timestamp='1302718081' post='4798051']
It seems the object is always 32 byte long nomatter what the string is. Is the string actaully in heap and the variable name is really just a pointer to a dynamic string array?
[/quote]
Yes, a std::string has a fixed size. It may or may not keep the actual string in dynamic storage duration, depending on if your implementation uses the SSO (short string optimization) or not.

You will find marshalling an internal data structure over a network will meet with doubleplus ungood fail if all you do is dump binary. You need to use a wire protocol and stick with it. Issues such as endianness, padding, ABI compatibility, word sizes, opaque structure, and pointers will bite you sharply in the bump the moment you deviate from the machine you tested on.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this