Sunday, September 27, 2015

C language : malloc free realloc and memory

Run this below program

#include<stdio.h>
#include<stdlib.h>



int main()
{

int *p=malloc(sizeof(int)*7);
printf("%d ",p);

free(p);
printf("%d  ",p[2]);


int *m=calloc(8,sizeof(int));
m[2]=100;
printf("%d ",m);
printf("%d  ",m[2]);
m[6]=90;
printf("%d  ",m[6]);

free(m);
realloc(m,4);
printf("%d ",m);
printf("%d  ",m[2]);
printf("%d  ",m[6]);
free(m);

}

First of all it will tell

1) syntax of free, malloc, realloc,calloc

2) If you pay attention we can see even after freeing  p , we can still access p[2].

The reason being " when we malloc or calloc some memory" compiler stores some additional information regarding that memory...size , initial values" , and it returns back actual RAM address.

When free is called, all it does is removing that information.  Then again depending on compiler free can set the pointer to NULL, can all delete the value stored in that RAM address.

But it seems GCC doesn't does that, therefore since we have address of physical address we can still access what is in it.   Therefore  whenever you call free in your code, REMEMBER to MAKE pointer NULL after that, else memory corruption can happen.


This also answers the question which is asked many times in interview , that how does FREE knows how much memory to free , when we don't pass any size to that.


Also the below code will give run-time error. Since with first free we have deleted any information regarding the pointer M. hence at second free compiler returns error.


int main()
{

int *M=(int *)malloc(sizeof(int));
free(M);
free(M);
}

C language : Memory Layout


Now since we have seen extern,static,local variable and also string. So the next important thing comes is Memory Layout.

Now I myself is studying at the same time and I have lots of doubts . Some get cleared over net and some I just assume to be true or false, until proved otherwise. :)

Most of us knows that C memory layout is basically

Stack, Heap,BSS,Code segment.   But we will here try to see how much we can get . The first thing I will be doing is to mention the various link i have gone through and after that what I understood from these.


http://www.geeksforgeeks.org/memory-layout-of-c-program/
http://cs-fundamentals.com/c-programming/memory-layout-of-c-program-code-data-segments.php
https://www.cs.cmu.edu/~guna/15-123S11/Lectures/Lecture06.pdf
http://www.tenouk.com/ModuleW.html


Now simply put,

Every machine which runs code has Opearting System, RAM , and then our C programs.

Taking GCC into perspective,

The stages of writing and running a program involves.

Using example of below program

#include<stdio.h>

#define CHETAN 12

int main()
{
 printf("%d",CHETAN);
}

1) the best command I got over net to generate all types of file during various stage is

> gcc -o memory memory.c  --save-temps

This will generate

1)  Preprocessor  Stage  :  memory.C  to memory.I  file  Preprocessed code is generated
2)  Compiler Stage   :  memory.I file to memory.S  file   Assemble Code is generated
3)  Assembler Stage  :  memory.S file to memory.O file  Object Code is generated
4)  Linker Stage  :  memory.O file to ELF file                  Executable Code is generated.


Now interested people can workout and see different types of files. All files except ELF and object can be opened with any text editor.

To see Object file code , you can use

> objdump -d memory.o


So we have seen the stages and various code file generated . Now what happens when you run the executable.

The LOADER loads the program into RAM/ primary memory, which creates PROCESS ADDRESS SPACE, since every program we run is a process.


Now every process/ C program can have below types of  variables.

initialized global variable : DATA
uninitialized global variable : BSS
initialized static global variable :  DATA
uninitialized static global variable:    BSS
static local variable ( it should be always initialized):  DATA
static local variable ( in case it is uninitialized):             BSS

local variables:  STACK
malloc space:    HEAP

string literal : ( like char *p="asasasas")  :  READ ONLY DATA    
constant    : READ ONLY DATA


Note  the pointer which points to MALLOC space , there storage location will depend on where that pointer is define.


And finally the object file code which was generated :   TEXT

In case of ELF file, the various section are

ELF file section

.bss        BSS
.text       TEXT
.rodata   READ ONLY DATA

the other gcc command which can be used

> readelf -a memory.o


So this was about ELF section,  STILL I am not sure where local variables and malloc gets stored.


Now about PROCESS ADDRESS SPACE.

It has following sections.

Starting from LOWER ADRESS OF MEMORY.

TEXT : This portion of a process contains the instruction code.  Some OS can share this section between two process running the same code. it is READ ONLY SECTION

DATA segment :  It has  DATA as defined above. Each process has its own set, no SHARING.
 IT is further divided into two parts .

            READ ONLY : global constants , string literals.   char *s="Hello";
            READ WRITE : global and static variables which are not constant. Also  char s[]="hello";

Now if you remember previous post about string, you know the difference between char*s and char s[].

BSS segment : It has BSS as defined above. NO SHARING.

HEAP segment : HEAP.

STACK segement : STACK.


One more thing,  I was wondering where does constant uninitialized variables are stored, On further searching on net I find out that it depends on your compiler.

So  all constant can be saved in text section.  or depending on whether they are global or local and initialized or uninitialized , they can also be stored in thier respective section , i.e BSS,DATA,STACK  since each of these section can have write protected region in them.




So now we know  the stages of C program and how it is stored in RAM.  Further we will see some code and try to find out memory of each section.

we can use SIZE command in ubuntu to check for memory size.

We will be using below program and then add more variables.

int main()
{
}


Now with this program , when I run size I get

text      data   bss   dec     hex    // there is no stack or heap section shown.  The values were
1033    276    4      1313   521

Leave the text part,  I am not sure why   data and bss  has some values even though we have not defined any type of variables.

Lets assume that to be some kind of overhead, in lieu of better explanation.

Now we add a local variable

int main()
{

int a;
}

Now i have  added some local variable, first not assigned any value  there was no change , then when i assigned them value  size of  dec and hex got changes.

You can check more from your side.

Now adding one global int variable, uninitialized   bss size got increased by 4.

Adding uninitialized   static variables,  bss size increased by 4.

Initializing those variables, make data size increase by 4, and bss gets decreased by 4.

Having extern int a;  doesn't increase size since no memory was saved, extern simply tell compiler that this variable is saved somewhere else.



If suppose for  below program the size is like

int main()
{

int a;
}

data   bss
20       4

then what will be size for below program


int main()
{

int a;

int *p=(int *)malloc(sizeof(int)*3);

scanf("%d",&a);

}

it will be

data   bss
28       4

the increase of size of  data is nothing to do with  int *p.....but it seems every function call we made,

that function pointer gets saved in data ...since we have malloc()  and scanf()  size increased by 4*2.


if we add a printf , data size will increase further by 4 bytes.

It seems local function call doesn't increase size , even in local function we are doing malloc.

but if in local function we define static variable, that is stored in DATA segment.



Thursday, September 24, 2015

C language : String


Recently I was working on some string related programs.  I came across some error, when I asked my friend he also faced the same issue.

Even though it is very trivial, I came to know since we were away from some basics for long time, we faced this issue.


In C we don't have string type. So we basically use either


char *cb="name";
char c[5]="name";

Now the difference between two are  cb is pointer to char array.  and c is itself a array which has "name" stored in it.

More deeeply,

when we do *cb="name";

In C memory layout --->data segment--->initialized data segment  "name" is stored and  cb is stored either in heap,stack,data segment  depending on local,static,extern variable.

And it stored value of "name".  Also the address where name is stored is READ ONLY.

So you cannot do cb[2]='g';

Now using cb, we can perform strcmp function or other function which will use the string stored at cb pointer.

But if we want to do strcpy , then error will be thrown. Simply because we haven't MALLOCed anything.

So what about the below functions. Working below two functions will tell more difference 


#include<stdio.h>
#include<string.h>
#include<stdlib.h>


int main()
{

char *t;

t= (char *)malloc(sizeof(char)*7);
t="chetan";
printf("%s",t);
strcpy(t,"rathore");
printf("%s",t);
}

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{

char *t;

t= (char *)malloc(sizeof(char)*7);
strcpy(t,"rathore");
printf("%s",t);

t="chetan";
printf("%s",t);
return 0;
}

The below program will further give some insight

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{

char *t;

t= (char *)malloc(sizeof(char)*7);
strcpy(t,"rathore");
printf("%s",t);

t="chetan";
printf("%s",t);
free(t);
return 0;
}

Now when we use

char cb[5]="name";

here cb is allocated space in heap or stack depending on local,static,extern.

so we can perform cb[3]="k"

also all strcmp and strcpy will work.  But what will not work here is

cb=NULL, since cb is not pointer.
cb='\0'; since cb is not pointer

BOTH WILL GIVE COMPILE TIME ERROR

Also strcpy(cb,NULL)  or strcpy(cb,'\0')  will give RUNTIME ERROR

Please pay attention to COMPILE ERROR and RUNTIME and LINK TIME ERROR.


Below program will tell difference between pointer of any type and array of same type.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
int t[4];
int *a;
t=&a;

}


At the closing stages , some more points .

In case you have defined array char

char strin[6];

then to make string empty , we can use  strcpy(strin,"");

but in case we have char pointer , we could have used

char *strin;

strin=NULL;  or strin='\0';




C language : Static and Extern



Some of points regarding static and extern.

1) By default , global variables are extern

2) Local variables are neither extern or static by default.

3) Defining a local variables as extern, doesn't have any effect.
3.1) defining local variables as extern and assigning value to it results in COMPILE ERROR.

So never have local variables as extern.

4) Local variables as static saves its value between function calls.

Also it is better to have value assigned to static value at time of definition.

5) Define global variable as STATIC only if you want that variable scope as limited to that file.


Both STATIC and EXTERN variable are saved either in BSS if uninitialized and in DATA if initialized.



Regarding to function, compiler treat them as implicitly EXTERN. therefore we don't need to add extern in front of function.

But if you need to limit the scope of function to some one file, then static can be added in front of function.

But then LINK ERROR will come if you are trying to access this function from some other file.

STATIC with function name is mostly used if you have 2 definition of same function, then you can limit the scope of one function by using STATIC.

Monday, September 21, 2015

C Language : Static


So in the last post , we have seen the EXTERN.  In this post we will see STATIC .

We can define STATIC in global variables or local variables.

If we define STATIC in global variables, then the global variables scope will be that file only.
So even if you have extern that variable in some other file , then error will come.

Program 1)

static1.c

#include<stdio.h>

static int a=10;

int main()
{
func()
}

static2.c

extern int a;

void func()
{
printf("%d",a);
}

now if you GCC and compile both files separately then , there will be no COMPILE ERROR.
But on creating object file, LINK ERROR will come. Since a is not visible outside the static1.c

Program 2)

static1.c

#include<stdio.h>

static int a;
a=10;

int main()
{
func()
}

This will give COMPILE ERROR, since non static definition cannot follow the static definition.



Now lets see what happens when static is defined with local variable.

Program 3)

static1.c


#include<stdio.h>

int main()
{

func();
func();
}

int func()
{
static int a;
a=10;
a++;
printf("%d",a);
}

the output of the function will be 11 and 11

Since static variable are assigned value at time of declaration. If we assign value after declaration, then everytime func is called , that value will be put in the function.

Program 4)

static1.c


#include<stdio.h>

int main()
{

func();
func();
}

int func()
{
static int a=10;
a++;
printf("%d",a);
}

this will behave as it should be , output will be 11 and 12.

What will happen if static variable is not initialized.

All static and extern variables will be initialized to zero if not explicitly assigned some value.

Sunday, September 20, 2015

C Language : Extern


I marvel at the way we humans are designed or made. We simply can't remember things which we are not in contact with for a certain period of time .

Its like either our memory gets rewritten by new memories or the way of accessing our old memories is not good enough.

I am pretty sure , I would have read about EXTERN a lot of times, but not using it on daily basis and suddenly again we have to go to some book, to check the usage.

Then the second thing is curiosity. I know a certain way of using EXTERN, but then a sudden urge will come to see what if  I have defined it in some other way or some other place and etc etc .

Now One more time and quite possibly not the last time , here again I go about EXTERN.


All the below program snippet use GCC on Ubuntu

To best see how extern works,  compile each .C file separately and  then make executable

Program 1)

extern.c file

#include<stdio.h>
int a=10;

int main()
{
    printf("%d",a);
  func();
}

extern2.c

int a=20;

int func()
{
printf("%d",a);
}

Compile both programs
gcc -c extern.c
gcc -c extern2.c

two object files will be generated  extern.o  and extern2.o

Try to generate executable
gcc -o output extern.o extern2.o

You wll get LINKING ERROR , saying multiple defination of a

Program 2)

Now remove the int a=20;  line in extern2.c  and try again

This time you will see , COMPILE ERROR for extern2.c , since a is undefined.

Program 3)

In extern2.c , add    extern int a;  at the start

  This will work and no error will come , since

1) having extern , tells compiler to check for a at run time .
2) At run time , only one declaration of a is present in extern.c

Program 4)

What if you add  extern int a=2; either in extern.c or extern2.c

There will be no compile error, but extern will not work,  it will be simply as you have defined
int a=2;



What about local variable defined as extern ?

This will not have any effect.
1) If you initialize value at same time as declaring then COMPILE ERROR will come.

else

it will simply treat a as global variable, note that a should be declare as global variable in some file.

ex

Program 5)

extern.c

#include<stdio.h>

int main()
{

extern int a=8;          // COMPILE ERROR


Program 6)

extern.c

#include<stdio.h>

int main()
{

extern int b;
printf("%d",b);
}

extern2.c

int b=10;

int func()
{
printf("%d",b);
}

This will work.

Even

extern.c

#include<stdio.h>

int b=10;

int main()
{

extern int b;

printf("%d",b);
}

will work.

Program 7)

extern.c

#include<stdio.h>

int main()
{

extern int b;
printf("%d",b);
}

extern2.c


int func()
{
int b=10;
printf("%d",b);
}

There will be no COMPILE ERROR, but will be LINK ERROR  since there is no global definition of  a.

If you try the above programs, you will pretty much know the working of extern.




Thursday, September 10, 2015

C++ : My Learnings Part 1



So I picked up one book and is going thru it, since I have already some work experience behind me, I will rather focus more on points which i have came across till now in my work with C.


In this part we will focus on

1) Enum

2) Struct

3) Union

4) Anonymous Union and Enums 

5) Void pointers

6) Scope resolution Operator 

7) Macro (#define) 

8) Typedef 

If we avoid all the tricky questions which can be asked, The basic difference between

TYPEDEF , ENUM , MACRO ( #define)  is that

typedef  has to be used only when you are trying to have a new DATA TYPE. 

ENUM has to be used, when you need to define long list of const , which are in real life. 

Ex : Weekdays , Months , list of student name in class....So even though we can use #define or const int ,  it will be difficult , firstly due to
1) Readability purpose
2) If we need to insert some new variable in list, we have to manual then change the value of each #define

MACRO (#define)  1) Sud not be used as such, since they actually don't anything , but just replace text. So if you can avoid them you should do that. But they can be used when want to better readability or you have to replace a lengthy text with small code

Ex : Suppose if you use lot of for loops in your code .  You can then simply use

#define FOR(start,end)  for(int i=start;i>=0;i--)

In my embedded code , we generally use this with enum to assign function pointers , since the function called will change depending on input.

Also  from MEMORY point of view ,  MACRO don't allocate any space , enum allocate space for just one variable .

Ex :

enum {SUNDAY=0, MONDAY=1)  // total size  will be 4 bytes ; for a machine assigned 4 bytes for int

const int SUNDAY =0;
const int MONDAY =1;

total size will be 8 bytes

#define SUNDAY 0
#define MONDAY 1

no space allocated.

So as can be seen enum is much better than const int , since the space used will be less. I have hardly seen many const in my code , May be const use will be more in OO languages rather than C

Also #define should not be used to assign integer or some other values to some variables, since code can get complex.


TYPEDEF is pretty straightforward, it should be used when you want to give easy name to some new data type which you have created from existing data types.

It also brings us to check the difference between typedef and #define

typedef  int* INT
INT a,b ;
a=&c;
b=&d;


#define INT int*
INT a,b ;
a=&c;
b=&d;

The second one throws error since, #define will simply replace text of INT, and only a will be treated as int* and not b.

In below code

int*   a,b;  or int   *a,b , only  A is pointer and not b.
Same effect happens with #define use.

Anonymous Enum is simply when you just want to use a given variables as CONST, you don't want to assign any value in it.

So don't have to give any type name to enum in that case.


Scope Resolution Operator is ::  and
1) It is available only in C++ and not in C.
2) It is used when their is clash between local and global variable.

Ex :

int a=20;
int main()
{

int a=30;
{
  int a=40;
  printf("%d",::a)
}
}

here ::a will print 20 and not 30 ...it will always take GLOBAL VARIABLE value.


Struct and unions are pretty much self explanatory, if someone has done some C/C++ coding.

So there is no need to go much deep in it. the below program will clear some more doubt.

http://ideone.com/qzt9wU


Void pointers we will cover later once going thru function pointers.


Wednesday, September 2, 2015

MS colleges



http://gregurublog.com/2015/01/13/universities-gre-score-300-to-310/

300-310

  1.  Illinois Institute of Technology – Chicago (Chicago, Illinois)
  2. Tuition Fees : Around 22,000$
Tuition fees : – 15000 $   texas is tax free


Tuition Fee : around 14,000$

Tuition fees : 23, 670 $

Tuition Fees: around 14,000$

12 University of Tampa (Tampa, Florida)

13  University of Houston (Houston, Texas)

http://gregurublog.com/2015/06/17/top-25-university-list-for-gre-score-310-to-320/

Tuesday, September 1, 2015

Courage and Predicament


Is our life better than the animals ?

They roam free, eat what is made available to them , sleep and wakes up at their designated time.

Of course , they have to fight more for their survival than us.  But what is more important FREEDOM or SURVIVAL.

The post is not about what we should do or what not. But mainly how in today's world we are caged and how FEAR has crept in us.

I came from middle class family, I remember when I was young and even till 12th the values of HUMANS EMOTIONS and interaction was given higher importance. They was less FEAR of FAILURE, there was less scrutiny. The student evaluation use to happen only during exams.


Now I am working in COMPANY, the bigger name company you work in and bigger package you get , they less control you have of your own LIFE. The FAILURE becomes exponentially equal to your package.

 Last year , lot of people from my company get laid off. Everyone was sad, when they get bad rating. But afterwards getting new job everyone is more happy.

That's how uncertain and random our life is . We can get sad about something, thinking it as the worst thing happened to us. But in the end we never know and more often than not we end up happy.

Why ...just because we are no longer caged. For my friends who are now working in new company , will definitely again start felling caged after 1-2 years, since FEAR will once again start making its way.

Now this year , they have given me average rating in mid year appraisal, which more seems to make me end up with GOOD rating rather than Very good rating.  ( Our system is quite F*** up)

Now instead of feeling sad , I feel quite relieved,   no pressure is their to maintain the top position.
Now I sit back and relax.

Off course the FEAR of MONEY might came when in MARCH , hike is given. Because of that FEAR I have to prepare and switch company.  Phewwwwwwww

Why we don't get COURAGE to do what we like, WHY when conditions become WORSE , then only we FOLLOW our HEART.

This is my ideal life ........have a nice backpack.....filled with 2-3 pairs of shoes , some dry fruits and chocolates....water and money to refill my stomach and move around from one place to other on foot.

Walk in the desert , have my feet dipped in ocean, feel wind against my face on mountain top ....and climb trees in forest.

A life where MIND no longer is burden thinking about FUTURE and just working out the PRESENT.

IoT : QCOM : QCA4002 and QCA4531



I like IoT , since it will let all the small small devices talk with each other. But it is becoming very diverse in all respect.

The number of devices, the types of solutions, types of platforms , OS , communication protocols all are very very diverse.

The good thing which happens due to diversity is you can find which particular platform you love and type of device and work on it.

The bad thing is there is no head start, the solution you are targeting can already be available or might get developed in better and faster way in future.

This post is generally my attempt to check what different different companies are doing and then decide what will I do .


1)  QCA 4002


Some information I am looking at is
1) How to make some application for this
2) Which server to connect.
3) Can I connect some other board to this.

The OS used by  development board seems to be  MQX RTOS.
The development board is SP140.

q) How QCA4002 and SP140 get link together.


A very good link

http://www.arrow.com/iotimmersions/presentations/pdf/QCOM_Arrow_IoE_Immersions.pdf

QCA4002 is just a wifi module, which using any kind of MCU ( microcontroller) you can use.

So basically MCU will be used to connect sensors and QCA4002.  So the development board or IDE to develop some applications will be based on MCU rather than QCA4002,

You have to take data from sensor , perform some task and send that data using QCA4002 to server.

SP140 is one kind of MCU.  GT202 is also a wifi module. Just think GT202 and QCA4002 as alias of same thing.


Like SP140, K22F120M is also a MCU.



As you can see from this link  https://lian-mueller.com/cart/  ,  which all products use QCA4002 wi-fi module.

This link will also be helpful in clearing more details.

http://www.silexamerica.com/products/connectivity-solutions/embedded-wireless/evaluation-kits/sp140-sp144-qca4004-evaluation-kit/

http://linuxgizmos.com/latest-atheros-iot-socs-include-openwrt-friendly-model/

If you see the above link , you can see that the IoT based devices which we were talking till now, used RTOS to work with, which can be difficult to anyone never worked before. 

But there is similiar IoT device by QC  QCA4531, which used Linux OS , and to develop application on it you have to use  

QCA Software Development Kit (QSDK).


Still some question which were raised and I need to check is

1) All Joyn and other standards
2) How to connect sensor to these devices QCAXXXX, since on net I haven't seen any demo application for these.