Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how String object is allocate memory without having new keyword or constructor?

Tags:

c#

In C# if we want to create a variable of type string we can use:

string str="samplestring"; // this will allocate the space to hold the string

In C#, string is a class type, so if we want to create an object, normally we have to use the new keyword. So how is allocation happening without new or constructors?

like image 914
sam Avatar asked Oct 19 '12 14:10

sam


People also ask

How is a string stored in memory?

Strings are stored on the heap area in a separate memory location known as String Constant pool. String constant pool: It is a separate block of memory where all the String variables are held. String str1 = "Hello"; directly, then JVM creates a String object with the given value in a String constant pool.

How is memory allocated for strings in Java?

Stack space contains specific values that are short-lived whereas Heap space used by Java Runtime to allocate memory to objects and JRE classes. In Java, strings are stored in the heap area. Why Java strings stored in Heap, not in Stack? String Literal is created by using a double quote.

Can memory be allocated without creating an object?

Explanation: The object memory allocation takes place when the object constructor is called. Declaration of an object doesn't mean that memory is allocated for its members. If object is initialized with another object, it may just get a reference to the previously created object.

Where are string objects stored in memory using new keyword?

4. String Literal vs String Object. When we create a String object using the new() operator, it always creates a new object in heap memory.


3 Answers

When you write

string str="samplestring";

compiler will generate two instructions:

  1. Firstly, ldstr gets a string literal from the metadata; allocates the requisite amount of memory; creates a new String object and pushes the reference to it onto the stack.
  2. Then stloc (or one of it's short forms, e.g. stloc.0) stores that reference in the local variable str.

Note, that ldstr will allocate memory only once for each sequence of characters.

So in example below both variables will point at the same object in memory:

// CLR will allocate memory and create a new String object
// from the string literal stored in the metadata
string a = "abc";

// CLR won't create a new String object. Instead, it will look up for an existing
// reference pointing to the String object created from "abc" literal
string b = "abc";

This process is known as string interning.

Also, as you know, in .NET strings are immutable. So the contents of a String object cannot be changed after the object is created. That is, every time you're concatenating string, CLR will create a new String object.

For example, the following lines of code:

string a = "abc";
string b = a + "xyz";

Will be compiled into the following IL (not exactly, of course):

  1. ldstr will allocate memory and create a new String object from "abc" literal
  2. stloc will store the reference to that object in the local variable a
  3. ldloc will push that reference onto the stack
  4. ldstr will allocate memory and create a new String object from "xyz" literal
  5. call will invoke the System.String::Concat on these String objects on the stack
  6. A call to System.String::Concat will be decomposed into dozens of IL instructions and internal calls. Which, in short, will check lengths of both strings and allocate the requisite amount of memory to store the concatenation result and then copy those strings into the newly allocated memory.
  7. stloc will store the reference to the newly created string in the local variable b
like image 86
Nikolay Khil Avatar answered Nov 14 '22 23:11

Nikolay Khil


This is simply the C# compiler giving you a shortcut by allowing string literals.

If you'd rather, you can instantiate a string by any number of different constructors. For example:

 char[] chars = { 'w', 'o', 'r', 'd' };
 string myStr = new String(chars);
like image 34
goric Avatar answered Nov 14 '22 22:11

goric


According to the MS documentation you do not need to use the new command to use the default string constructor.

However this does work.

char[] letters = { 'A', 'B', 'C' };
string alphabet = new string(letters);

c# Strings (from MSDN programming guide)

like image 32
Spevy Avatar answered Nov 14 '22 23:11

Spevy