memory allocation of character in c and java -
how memory space allocation affects operations on string length in (both) c , in java ? (this in reference fact c string variable packs 4 bytes per word java string variable packs 2 half words per word)
the fact there ain't c string variable in c , arrays ;arrays of characters. 1 char
in c occupies 1 byte
. string literals stored array of characters , terminating \0
appended @ end.
in java programming language, strings objects. string contains following:
- a char array— separate object— containing actual characters;
- an integer offset array @ string starts; length of string;
- another int cached calculation of hash code.
this means if string contains no characters, require 4 bytes char array reference, plus 3*4=12 bytes 3 int fields, plus 8 bytes of object header. gives 24 bytes (which multiple of 8 no "padding" bytes needed far). then, (empty) char array require further 12 bytes (arrays have 4 bytes store length), plus in case 4 bytes of padding bring memory used char array object multiple of 16. in total, empty string uses 40 bytes.
calculating memory usage string have take account fact character in array 2 bytes.
java->string.length()
constant time operation in number of characters contained in string because java string class stores length field.
c-> strlen()
traverse whole array until \0
calculate length of string runtime grows size of string.
Comments
Post a Comment