difference in between const char* n[ ]={"name1",..};, char* a[ ] ={"names1"..}

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pardhu0163
    New Member
    • May 2012
    • 9

    difference in between const char* n[ ]={"name1",..};, char* a[ ] ={"names1"..}

    Hi all,

    I am using Keil Real view compiler for lpc1788 controller. It has 512k flash and 96k RAM memory.

    After some programming on this, memory consumption is like this.

    Program Size: Code=88112 RO-data=32960 RW-data=3544 ZI-data=27896



    Now, i observed something after adding one array in main function, which i am not getting why it is happening like this?

    Code:
    main{
    
    char* n[]={"name1","name2","name3"};
    
    while(1);
    };
    In the above code, n is a local 2d variable which is having 3 strings. Each string's length is 5 characters so size will be 5 bytes for each. total array size will be 15 bytes.

    after adding this array,

    Program Size: Code=88132 RO-data=42644 RW-data=3544 ZI-data=27896

    How it is increasing RO-Data 9.4k bytes at a time.

    I changed it to const char* n[],and char n[3][5].
    And i removed 2 strings out of that, even though it showing same increment in RO-Data.

    How it is happening??
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:

    Code:
    char* n[]={"name1","name2","name3"};
    defines n as a local variable.

    n is an array of 3 elements. Each element is the address of a const char array. The sizeof n is the size of 3 addresses. On my machine this is 12 bytes.

    n is not a two dimesional array. The strings are 6 bytes and not 5 as you need to allow for the null terminator.

    Comment

    • pardhu0163
      New Member
      • May 2012
      • 9

      #3
      Hi sir,
      Thank you for correcting me. Yeah, it is taking 12 bytes. But i am getting little bit worry about RO-Data increment. Why it is incrementing that much.

      Can you explain it why?

      Thank you sir.

      Pardhu

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Every compiler has it's own memoy management system. In the case of Windows machines a call to VirtualAlloc()i s made to allocate memory in pages. These pages become your CRT heap. The size of the page is operating system dependent. Threrefore, you could see he size of your process expand a loat based on a small stack increase in your process.

        Generally, analyzing the memory management of a particular compiler is less productive than learning how to code efficiently.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          The string constants "name1" etc are (as the name implies) constants. As such most embedded compilers will add them into your constant data which is normally stored in flash which is why you are seeing an increase to the size of the RO data segment.

          That said the increase in size does appear disproportionat e to the amount of data you say you have added. If you really want to investigate this then again most embedded linkers (actually most linkers in general) will produce a map file that should let you see exactly what is stored where which may give you some hints as to what has happened.

          I agree with weaknessforcats final statement there are however times when it is useful to be able to analyse the memory management of your platform, particularly in the often restrictive world of embedded programming. Normally right at the start of a project to ensure you application will fit into the available memory or right at the end of a project when you application doesn't fit into available memory and you need to work out the best way of reducing its memory footprint.

          Comment

          Working...