tags:

views:

119

answers:

3

What type of variable that can contain 1,000,000,000(a decimal number) takes the most memory space?

  1. int in C
  2. string in C
  3. string in Java(which uses unicode)
+3  A: 

A Java String. Under the hood A Java String consists of an object with 3 fields, one of which points to a separate array object containing the characters. Plus of course, Java Strings are composed of 16 bit characters.

If you are worried about memory usage over all other criteria, don't use Java. But for most applications, memory usage is the least of your concerns.

It is worth noting that 1,000,000,000 can be represented using a Java int which will be the same size as a C signed or unsigned (32 bit) integer.

Furthermore, a C int is not necessarily big enough to represent 1,000,000,000. On some platforms, int is 16 bits, and this is allowed by the C standard.

Stephen C
Y - to clarify, C defines int as the native machine word size (so int can be 16, 32 or 64 bit etc). I guess technically a C int could be the most inefficient storage for 1BN in a 512bit machine word scenario, but hopefully I won't be around to see this.
nonnb
@nonnb - to be absolutely technically correct, the C standard says that the range of `int` is at least -32758 to +32767. It can be (and often is) greater than that. However, the standard says *absolutely nothing* about `int` being the same size as a "native machine word".
Stephen C
+1  A: 

The C standard doesn't state many storage requirements. As it is, you could have:

  • 256-bit C ints that take 32 bytes to store anything (see @nonnb's comment)
  • A wide C string (wchar_t[]) that uses UCS-4/UTF-32 characters (as all GNU implementations do, apparently)
  • A C implementation that uses 32-bit chars (which would have to be on a system with 32-bit bytes)
aib
Incidentally, 21-bit-or-larger `wchar_t` is the only way a conformant C implementation can support all of Unicode. It's also the recommended way (see the __STDC_ISO_10646__ macro in C99). Implementations with 16-bit `wchar_t` are simply broken.
R..
aib