0

Here, Student_info is a class. I am trying to initialize it with strings of different length but it always give the same size of object, when it is supposed to be dependent on the input string length. Any suggestions? (Name and Company are defined as string variable, not as char arrays)

Structure of class Student_info
{ string name, company;
  int yr, age;
}


      Student_info *s[5];
      s[0]= new Student_info("Aakash Goyal","form MNNIT Allahabad",2,57);       
      cout<<endl<<s[0]->getCompany()<<" is the company"<<endl;
      cout<<endl<<s[0]->getName()<<" is the name and the size is: "<<sizeof(*s[0]);


      s[1]= new Student_info("Hare Krishna","Hare Krsna Hare Rama",1,34);
      cout<<endl<<s[1]->getCompany()<<" is the company"<<endl;
      cout<<endl<<s[1]->getName()<<" is the name and the size is: "<<sizeof(*s[1]);
3
  • 2
    Please add the expected output and the actual output to your question. And also the definition of the Student_info class. Commented Aug 7, 2013 at 8:34
  • Any reason for the pointers and manually managing memory? Commented Aug 7, 2013 at 8:36
  • @chris yes, I am learning to code this way. Commented Aug 7, 2013 at 8:39

2 Answers 2

3

The result of sizeof is always constant for a given type, including std::string and your own type Student_info that aggregates strings. This is easy to see by induction: since every type is built by aggregating primitives (numeric types, pointers, etc) and all primitives have a fixed size, then any type you can make from them will also have a fixed size.

Types that store a variable amount of data such as std::string, std::vector and others do this by aggregating a pointer to a memory region where their data is stored. The size of that region is not constant, but the size of the pointer (and therefore the containing class) is.

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

9 Comments

how then can I get actual size?
@Aakash: Define "actual size" first. If "actual size" means "the amount of additional free memory I would have if I had not created this object at all" the answer is "you cannot".
@Aakash Use the length function to get the length of a std::string.
@Aakash: Not possible. In general, an object can aggregate other objects which in turn can aggregate other objects [...] which can aggregate pointers to dynamically allocated memory areas. There is no generic way to traverse this graph of dependencies and get a count of allocated bytes. You can only do it for specific implementations by examining the source and "reverse engineering" their memory allocation to determine exactly what gets allocated.
No and yes. Name, company, year, age are in a contiguous memory location. The problem is that name and company are boxes that contain a pointer to dynamically allocated memory (depending on the size of initializing value), and that memory is in an entirely different location.
|
2

sizeof() is a compile time operator. At runtime, it will always return the same constant value for a given object.

Even if you include a std::string in your object, and assign different values to it, the size of the enclosing object as calculated by sizeof() will remain the same since the data buffer which holds the string is handled internally by std::string (essentially through a pointer, which again has a constant size independent of the data it points to).

If you want to get the runtime length of a string, use its length() method.

Comments

Your Answer

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