in

Microsoft Philippines Community

A community for users, customers, and partners of Microsoft products in the Philippines :)

Trivia #2

Last post 01-19-2006 10:10 PM by cruizer. 15 replies.
Page 1 of 1 (16 items)
Sort Posts: Previous Next
  • 01-11-2006 4:21 PM

    Idea [I] Trivia #2

    How many strings are allocated in the heap when this code is executed?

    string query = "SELECT * FROM Table1 A " +
                         "INNER JOIN Table2 B ON A.ID = B.ID " +
                         "WHERE A.Name = 'jokiz'";

  • 01-11-2006 4:25 PM In reply to

    • dehranph
    • Top 25 Contributor
    • Joined on 11-14-2004
    • Ala eh taga Batangas!
    • Posts 1,769
    • Points 15,340

    Re: Trivia #2

    4?
    Rodel E. Dagumampan
    Software Engineer - ASP.NET/C#
    Bel-air, Makati, Philippines

    Rodel E. Dagumampan
  • 01-11-2006 4:28 PM In reply to

    Re: Trivia #2

  • 01-11-2006 4:36 PM In reply to

    • cruizer
    • Top 10 Contributor
      Male
    • Joined on 07-20-2004
    • Singapore
    • Posts 9,417
    • Points 65,306
    • MVP

    Re: Trivia #2

    just one? the compiler will probably recognize that it's a concatenation of string constants.

    http://devpinoy.org/blogs/cruizer

    Naglalayong buksan at palayain ang kamalayan ng Pinoy .NET developer
  • 01-11-2006 4:51 PM In reply to

    • gwapo
    • Top 100 Contributor
    • Joined on 10-12-2004
    • Posts 429
    • Points 4,226

    Re: Trivia #2

     joeycalisay wrote:

    How many strings are allocated in the heap when this code is executed?

    string query = "SELECT * FROM Table1 A " +
                         "INNER JOIN Table2 B ON A.ID = B.ID " +
                         "WHERE A.Name = 'jokiz'";



    I would say 2.

    The first is the heap used to build the string, and the second is to hold the string.

    .maxstack 2
    ldarg.0 
    ldstr "SELECT * FROM Table1 A INNER JOIN Table2 B ON A.ID = B.ID WHERE A.Name = \'jokiz\'"
    stfld string object::query
    ret


    Correct?

    -chris
  • 01-11-2006 4:56 PM In reply to

    • dehranph
    • Top 25 Contributor
    • Joined on 11-14-2004
    • Ala eh taga Batangas!
    • Posts 1,769
    • Points 15,340

    Re: Trivia #2

    Only 1

    From here, the IL autmatically concatenate those string constants.

    .method private hidebysig static void  Main(string[] args) cil managed
    {
      .entrypoint
      // Code size       8 (0x8)
      .maxstack  1
      .locals init ([0] string query)
      IL_0000:  nop
      IL_0001:  ldstr      "SELECT * FROM Table1 A INNER JOIN Table2 B ON A.ID"
      + " = B.ID WHERE A.Name = 'jokiz'"
      IL_0006:  stloc.0
      IL_0007:  ret
    } // end of method Program::Main


    However for this statement it will be different,

    int x = 25;
    string query = "SELECT * FROM Table1 A " + x.ToString() +
    "INNER JOIN Table2 B ON A.ID = B.ID " +
    "WHERE A.Name = 'jokiz'";


      IL_0003:  stloc.0
      IL_0004:  ldstr      "SELECT * FROM Table1 A "
      IL_0009:  ldloca.s   x
      IL_000b:  call       instance string [mscorlib]System.Int32::ToString()
      IL_0010:  ldstr      "INNER JOIN Table2 B ON A.ID = B.ID WHERE A.Name = "
      + "'jokiz'"
      IL_0015:  call       string [mscorlib]System.String::Concat(string,
                                                                  string,
                                                                  string)
    thanks pare! 5start for u!

    Rodel E. Dagumampan
    Software Engineer - ASP.NET/C#
    Bel-air, Makati, Philippines

    Rodel E. Dagumampan
  • 01-11-2006 5:02 PM In reply to

    Re: Trivia #2

    yeah, just one instance in the heap.  point - compiler also does its own optimizations...

    thanks for the replies!

    chris bakit 2? elaborate more nga... baka mali kami...

  • 01-11-2006 5:08 PM In reply to

    • dehranph
    • Top 25 Contributor
    • Joined on 11-14-2004
    • Ala eh taga Batangas!
    • Posts 1,769
    • Points 15,340

    Re: Trivia #2

    oo nga sir chris, pareho lng naman ilasm ginamit natin. ;)
    Rodel E. Dagumampan
    Software Engineer - ASP.NET/C#
    Bel-air, Makati, Philippines

    Rodel E. Dagumampan
  • 01-11-2006 5:12 PM In reply to

    • gwapo
    • Top 100 Contributor
    • Joined on 10-12-2004
    • Posts 429
    • Points 4,226

    Re: Trivia #2

    Sorry, 1 is correct.

    Strings are arranged in the heap in order (that's why it's immutable), and there's a pointer that would identify the boundaries of each strings. I thought you were asking about how many heap allocations was made during the execution of the code, my response is two for the reason of "data" and "pointer", but I guess I over assumed it Smile [:)]

    Keep up the trivia!

    And welcome to MSDN Community joeycalisay, I hope dehranph may find his way to the community as well. dreamlordzwolf is already kicking in there Smile [:)]


    Regards,

    -chris
  • 01-11-2006 5:14 PM In reply to

    Re: Trivia #2

     gwapo wrote:


    And welcome to MSDN Community, I hope dehranph may find his way to the community as well.

    pre lasing ka ba today? just kidding...

    [EDIT] ahhh gets, i thought sa sobrang busy mo sa 2 forums, akala mo msdn community forums na 'to.  I've been there before kaya lang kapag free time lang ako naka-login.  I'm just there at winforms designer

  • 01-11-2006 5:16 PM In reply to

    • gwapo
    • Top 100 Contributor
    • Joined on 10-12-2004
    • Posts 429
    • Points 4,226

    Re: Trivia #2

     joeycalisay wrote:

     gwapo wrote:


    And welcome to MSDN Community, I hope dehranph may find his way to the community as well.

    pre lasing ka ba today? just kidding...



    I mean here:
    http://forums.microsoft.com/msdn/
  • 01-11-2006 5:21 PM In reply to

    • dehranph
    • Top 25 Contributor
    • Joined on 11-14-2004
    • Ala eh taga Batangas!
    • Posts 1,769
    • Points 15,340

    Re: Trivia #2

    talaga si dreamlordzwolf? he he, mike's shifting from vb.net to C# eh. makapunta na nga dyan.
    Rodel E. Dagumampan
    Software Engineer - ASP.NET/C#
    Bel-air, Makati, Philippines

    Rodel E. Dagumampan
  • 01-11-2006 5:32 PM In reply to

    Re: Trivia #2

     gwapo wrote:
    I thought you were asking about how many heap allocations was made during the execution of the code, my response is two for the reason of "data" and "pointer", but I guess I over assumed it Smile [:)]

    heap allocations?  same thing.  i believe isa lang string instance allocated sa heap.  and query just has a reference/pointer to it.

  • 01-12-2006 1:39 PM In reply to

    • gwapo
    • Top 100 Contributor
    • Joined on 10-12-2004
    • Posts 429
    • Points 4,226

    Re: Trivia #2

     joeycalisay wrote:
    heap allocations?  same thing.  i believe isa lang string instance allocated sa heap.  and query just has a reference/pointer to it.


    string my_string;   // this is equivalend to [__int32] containing null value.

    Is already one allocation, a null pointer. If you put data in to that string for the first time, then another allocation will be needed to hold the data i.e.,

    string my_string = "This is an actual string";
    // is equivalent to [__int32] containing an address of "This is an actual string" data.


    But of course, the CLR will handle this already for us, so we can stop worrying about it.
    The only point I'm making here is, a string cannot point to itself, which is not a big deal.

    Regards,

    -chris
  • 01-19-2006 7:54 PM In reply to

    Re: Trivia #2

    Hey, this is a cool question and great discussion.
    Stanley Tan
    Academic Program Manager
    Microsoft Singapore

    http://blogs.msdn.com/Stanley/
  • 01-19-2006 10:10 PM In reply to

    • cruizer
    • Top 10 Contributor
      Male
    • Joined on 07-20-2004
    • Singapore
    • Posts 9,417
    • Points 65,306
    • MVP

    Re: Trivia #2

    compilers nowadays are smart Smile [:)]
    http://devpinoy.org/blogs/cruizer

    Naglalayong buksan at palayain ang kamalayan ng Pinoy .NET developer
Page 1 of 1 (16 items)
Copyright © 2008 Microsoft Philippines Community

Powered by Community Server (Commercial Edition), by Telligent Systems