The sizeof(array)/sizeof(element) works for fixed-length-array of fixed-length-arrays (not of pointers).
As an array of strings we most often use a (fixed-length-)array of pointers-to-various-(fixed-)length-strings so this trick wouldn't work.
sizeof() is used for objects which size is known at compile time. It's not applicable to dynamically allocated data itself.
When an object contains pointers like in the case of an array of strings,
sizeof() returns the size of the highest-level (fixed-size) structure. Often it's just the size of a single pointer. It does not include the size of the allocated data pointed to by the pointers. Because that data actually is not part of the main object, it's indeed one or more separate objects (we have aggregation here instead of composition, see http://en.wikipedia.org/wiki/Object_composition).
In C++ using vectors is very convenient for your needs. Other suitable standard containers could be used too.
length() and size() methods are synonyms, see http://www.cplusplus.com/reference/string/string/size/)
P.S. Please note that for std::string s object sizeof(s) is a constant independent of the actual (variable) string length returned by s.length(). The actual allocated memory size is returned by s.capacity() and could be greater than length().
Example using vector array:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string s = "01234";
cout << "s[" << s.length() << "]=\"" << s << "\"" << endl;
cout << "sizeof(s)=" << sizeof(s) << " (implementation dependent)" << endl;
cout << endl;
s += "56789012345";
cout << "s[" << s.length() << "]=\"" << s << "\"" << endl;
cout << "sizeof(s)=" << sizeof(s) << " (implementation dependent)" << endl;
cout << endl;
vector<string>vs={"12","23","345","456","567","67888","7899999999","8","9876543210"};
cout << "vs[" << vs.size() << "]={";
size_t sz=0;
for (size_t index=0; index<vs.size(); index++)
{
sz+=vs[index].size();
if (index>0)
cout << ",";
cout << "\"" << vs[index] << "\":" << vs[index].size();
}
cout << "}:" << sz << endl;
cout << "sizeof(vs)=" << sizeof(vs) << " (implementation dependent)" << endl;
return 0;
}
Result:
s[5]="01234"
sizeof(s)=8 (implementation dependent)
s[16]="0123456789012345"
sizeof(s)=8 (implementation dependent)
vs[9]={"12":2,"23":2,"345":3,"456":3,"567":3,"67888":5,"7899999999":10,"8":1,"9876543210":10}:39
sizeof(vs)=24 (implementation dependent)
result
. Without it, it's impossible to tell what your problem is. – Calypsosizeof
calls. – Medullarychar
(e.g.char result[][16] = { "maximum", "of", "fifteen", "characters", "per", "string" };
)... What evidence is there to suggest that the machine is 128-bit? What does 128-bit even mean? Is it the width of the bus? If so, there are 64-bit machines that don't have 64-bit buses and 32-bit machines that do. Is it the width of the native types? If so, even Pentium Pros back in 1995 had a few 128-bit registers... – Arvad