473,394 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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?

Expand|Select|Wrap|Line Numbers
  1. main{
  2.  
  3. char* n[]={"name1","name2","name3"};
  4.  
  5. while(1);
  6. };
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??
Jun 20 '12 #1
4 2502
weaknessforcats
9,208 Expert Mod 8TB
This code:

Expand|Select|Wrap|Line Numbers
  1. 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.
Jun 20 '12 #2
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
Jun 21 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
Every compiler has it's own memoy management system. In the case of Windows machines a call to VirtualAlloc()is 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.
Jun 21 '12 #4
Banfa
9,065 Expert Mod 8TB
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 disproportionate 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.
Jun 26 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Santa | last post by:
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static', also at what conditions it can be used?. Thanks.
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
2
by: Amit | last post by:
Hello, I have a class where I store a lot of setting names. Right now they're all declared as consts : public const CurDoc as string = "CURRENT_DOCUMENT" From what I know about const, every...
1
by: Melson | last post by:
Hi Does anyone know the difference between Const variable and ReadOnly variable. Const Var1 = "123" Shared ReadOnly Var2 = "123"
4
by: Kasya | last post by:
Difference Between Const Char and Char
2
by: Jason | last post by:
What is the difference between 'const' and 'define()' ? When would I prefer using 'const' over 'define', or vice versa? It seems if i do: const secondsInMinute = 60; define("secondsInMinute",...
34
by: arnuld | last post by:
what is the difference between these 2: char name = "hackers"; char* name = "hackers";
3
by: gsrinivasanbe | last post by:
what is the difference between costant and ststic?
2
by: iPaul | last post by:
in c (and/or) c++ what is the difference between variables declared as: char and char*? thanks
2
by: ngpbalaji | last post by:
whats the Difference between const and #define in C? we can Use both for the case below... #define PI 3.1416; const float pi2 = 3.1416; for both the case actually what will happen when...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.