Category: C++

Which memory is used for variables defined globally?

October 16, 2013 at 10:58 pmCategory:C++

Suppose we need to work with 8MB stack, & wish to use standard C++ arrays.
Is it true, that

uses heap memory, & for that reason does not segfault (as opposed to when DP is declared in main)? Is it different from allocating the memory via new / malloc in main (besides free issues)?

In modern OS’s, the memory used by an executable is split into (usually) five distinct sections:

  • The Code section (also known as text in Linux/Unix systems for hystericalhistorical reasons). This is where your functions “live”. Often moreover used for constant values, such as char *s = "Hello, World";, the “Hello, World!” part is stored in the CODE section.

  • The “Initialized” data section (also known as “data”) – for global (in C & C++ terms static) data that has been given a value, e.g. int x = 42;

  • Uninitialized data, moreover known as BSS, Block Storage section – for global data that is not given a value, & thus initialized to zero. int y; in a global context, or static int status; would fall into this section.

All of the above sections are defined in the executable. Some executables have more sections than this, yet these are the “typical” ones. An example of an “extra” section is a “read-only data” section, which may be used to store for example string data, rather than storing it in the “code” section.

Once the executable is loaded, two more sections are created by the OS:

  • A stack, which is used to hold local variables inside functions, & is moreover used to “get back” to the calling function. The stack is commonly fairly restricted in size, yet nowhere near as small as it used to be – these days, the stack is regularly in the “a few megabytes” size range. My first machine I used, had a stack of 256 bytes (and that was hard-coded). If you wanted more than that, you had to arrange that by making your own software defined stack. Not very pleasant!

  • A heap. This is used for “dynamic allocation” – for example when creating storage for arrays that vary in size depending on program input. The contents of the heap is not known before the program has started to run. In modern systems, the heap starts out with a small size, & is allowed to grow (but there is a limit, if nothing else when the machine itself runs out of memory – yet often the limit may be lower than that from a system configuration, to avoid one application using up all of the memory in the machine).

In the above example, DP is in the “uninitialized data” section.

Why below code's output is “not found” instead of “found”?

October 6, 2013 at 10:58 pmCategory:C++

below code gives “Not found” output. But I expect it to donate “found”. Where is my mistake?

In *x==y[i] you are comparing the value of two chars instead of the data pointed to by two pointers. Use the strcmp function instead. It returns 0 if the two strings pointed to by the given two pointers are equal. So alter it to strcmp(x, y[i]) == 0

Also you should alter the char *y parameter to char **y or char *y[] because y is an array of pointers to strings, not just one pointer.

Finally, compare(array, *numbers); should be called as compare(array, numbers); because you want to pass a pointer to the array of strings, not just a pointer to one string (numbers is of type char*[4] yet it will decay to type char** when passed as an argument).

Find the value for the address in GDB (Cent OS 6)

October 6, 2013 at 10:58 pmCategory:C++

For analysis purpose we want to know the which data(message) is stored in the address. Is there any option to find the message in GDB.

In the other words we know the address (0x80488b4) of memory yet we want moreover know the message stored in that address through GDB.

Sample code :

If you know the type typemsg_tof the message, you could dereference it, e.g. print *(typemsg_t*) option_value

You might moreover be interested by the GDB watchpoint ability.

It is worth taking some time to read GDB documentation !

Adding char to string in C++

September 27, 2013 at 10:24 amCategory:C++

I work with Eclipse & Arduino.

I want to add a char to a string. I tried to use append,insert ( yet these can not be resolved)
I tried to use += yet when i print the string it always have one char.Basically i deletes the string & writes only the new char i want to add in.
I tried moreover concat & it does the same thing.Also strcat gives me headache with the operands cause it needs a const char pointer & i want to add a char that changes.

It sounds like what you want (for convenience) is the String object class available with the Arduino library.
http://arduino.cc/en/Reference/StringObject

How to speed up compile with “make” in Linux

September 16, 2010 at 7:23 amCategory:C++ | Linux | Mangos

There is an option which is

x=the number of concurrent  jobs

For a Dual Core 2 2.0 GHZ CPU, make -j5 will be a feasible choice.

CAUTION: it will cost much more CPU usage while the number increases,  please be careful to use this in any live environment.