Dynamic Memory Allocation and Data Structure

Yesterday I learned about data structure and I tried making my very first code related to it. What I learned about structure is it is just like any other data type (eg. int, char). What differentiate structucture from an ordinary data type is that it can hold a different type of data. So a structure is a container of any number of variable of any data type and even a structure itself (nested structure) . I also tried typedef. See below for the practice code I did yesterday:

#include <iostream>
#include <string>

using namespage std;

struct person_test {
int age;
string name;
} girl, boy;

//struct using typedef. with typedef it will create an alias
typedef struct second_practice {
int age;
string name;
int year_birth;
} THISISIT;

int main ()
{
girl.age = 28;
boy.age = 27;

girl.name = “Tzekoi”;
boy.name = “Makoi”

cout << “GIRL” << endl;
cout << “Name: ” << girl.name << endl;
cout << “Age: ” << girl. age << endl << endl;

cout << “Boy” << endl;
cout << “Name: ” << girl.name << endl;
cout << “Age: ” << girl. age << endl << endl; //Secod practice
THISISIT *chingot; //Instead of “struct   second_practice” it gets replaced with THISISIT
THISISIT makoi;

chingot = &makoi; //chingot->age is the same with (*chingot).age
chingot->age = 28;
chingot->year_birth = 1984;
chingot->name = “Tzekoi”;

cout << “Second Examople” << endl;
cout << “Age: ” << chingot->age << endl;
cout << “Name: ” << chingot->name << endl;
cout << “Year: ” << chingot->year_birth << endl << endl;
cout << “Age: ” << makoi.age << endl;
cout << “Name: ” << makoi.name << endl;
cout << “Year: ” << makoi.year_birth << endl << endl;

makoi.name = “Makoi”;
makoi.year_birth = 1984;
makoi.age = 27;

cout << “Age: ” << chingot->age << endl;
cout << “Name: ” << chingot->name << endl;
cout << “Year: ” << chingot->year_birth << endl << endl;

cout << “Age: ” << (*chingot).age << endl;
cout << “Name: ” << (*chingot).name << endl;
cout << “Year: ” << (*chingot).year_birth << endl << endl;
}

Today I also learned about Dynamic Memory Allocation. From the word Dynamic Memory Allocation we can easily get an idea that is about assigning memory on run time. How to do this I have based the sample code I tried on the very comprehensive site for c++ tutorial http://www.cplusplus.com/doc/tutorial/dynamic/. The following lines of code I have run:

#include<iostream>
#include<new>

using namespace std;

int main ()
{
int *pChing;
int index = 5;

pChing = new (nothrow) int[index];

if (pChing == 0)
{
cout << “ERROR: memory could not be allocated”;
} else {
for (int i = 0; i < index; i++ )
{
pChing[i] = i;
}
for (int i = 0; i < index; i++)
cout << pChing[i] << endl;
delete[] pChing;
}
return 0;
}

Another Pointer Example Compiled using GCC

I have been calling myself a C++ programmer for almost 5 years now and I still find pointer very hard to understand. On my previous entry I believe I have posted an example. Today I ran my own code using pointer and applying my basic knowledge on GCC. See the code below:

#include <iostream> 

using namespace std;

int main ()

{

int ching, makoi;

int *pointer;

pointer = &ching;

*pointer = 10;

pointer = &makoi;

*pointer = 20;

cout << “ching ” << ching << endl;

cout <<“makoi “<< makoi << endl;

}

OUTPUT:

ching 10

makoi 20

Another example is:

#include <iostream> 

using namespace std;

int main ()

{

int ching, makoi;

int *pointer;

makoi = 1;

pointer = &makoi;

ching = *pointer;

cout << “ching ” << ching << endl;

cout <<“makoi “<< makoi << endl;

}

OUTPUT:

ching 1

makoi 1

I got to learn how to love pointers. I got to master pointers. It starts today. I’d like to share this quote I heard from glee.

“Without passion you will never succeed.” 🙂

Starting GCC

I have been used to using visual studio 6.0 when it comes to compiling and executing my code in C. With a click of a button then your code gets compiled and executed. Now that I’m in a new company things have changed and I got introduced to a new compiler, GNU Compiler Collection or GCC. For 9 months now I have not really explored on the compiler for we use script to automate the build, compile, link and clean of our code.  So I never really had any experience using gcc. There were several sites that would explain about gcc and how to get started andthere I saw what huge difference it was with visual 6.0. To start with it does not have a built in editor so we have to rely on other editors to do our code. I am using vi. Then there is no button for compile and all those you need to do to your code. You have to type the commands for you to get things working. Well it’s free that’s the good thing. So below is my very first taste of gcc.

//hello.cc

#include <iostream>

int main ()

{

std::cout<<“Hello there Ching-ching \n”;

return 0;

}

To compile you enter the following command:

$ g++ -c hello.cc //creates hello.o this is not yet executable

or

$ g++ hello.cc   // creates a.out which is the executable of hello.cc to run simply call ./a.out

To make the hello.o executable:

$ g++ hello.o -o hello

To execute enter the following command:

$./hello

Output:

Hello there Ching-ching

Another command is:

$ g++ hello.c -o hello //easiest way to compile and create an executable file

You may refer to man g++ for the commands. That’s all for now. 😀

Pointers in C++

So today I was actually thinking of posting about pointers but it was not supposed to be today. But an officemate of mine from other team who is also a c++ programmer here asked me a pointer related question and I laughed at the coincidence and because I really don’t know what to answer her. I guess this means the entry for pointers won’t have to be prolonged. It is now or never.

I have been using c++ for 3 years now and still I really can’t find myself to understand pointers. It has always been my greatest weakness coming across pointers. Now I have to get to know them and really try to be close to them and know them by heart. So pointers. hmmm let me google first. here’s a site http://www.cplusplus.com/doc/tutorial/pointers/ with drawings and arrows and all. At first glance it seem easy to the eye but once you go further you get dizzy. Lets just break this down to what I understand now.

To start lets have a definition:

“Pointers are said to “point to” the variable whose reference they store.”

“reference they store” is like an address of the container. So for example a person lives in a condo of  adress makati city. variable here is the person, condo is the memory block in makati.

Next lets look at the pointer operator:

  • & is the reference operator and can be read as “address of”
  • * is the dereference operator and can be read as “value pointed by”

From the above mentioned short introduction lets see if we can have a sample:

Example of REFERENCE

ching = &person

So the value of ching is the address of person which makati city.

Example of DEREFERENCE

makoi = 1

tzekoi = &makoi -> tzekoi now points the address of makoi

koi = *tzekoi -> the value of koi will be 1

For now that is all I have. Got to go back to work now. I will add more when I get to have time. I hope I don’t forget this. It seem easy. Its just a matter of getting use to this.

Linux and Unix

I got curious about the difference between LINUX and UNIX after it has been brought up during the phone interview I had yesterday. I was asked if I had any experience with Linux and I said yes. He followed it up if with if I also know Unix. It made me think because all this time I thought UNIX and LINUX were one that unix is just the equivalent of windows that has an xp, 98, 7. So Unix has a Linux and Solaris. But I found out too late that these two are different. Just because they seemed to appear the same in spelling I assume they were actually two fruits of the same tree. So I’ve read from this site and was I enlightened. I won’t explain what is already elaborated on the site. The link is http://www.cyberciti.biz/faq/what-is-the-difference-between-linux-and-unix/. And also it was mentioned there that Linux is just a kernel. What is a kernel? Here is what I googled and it is the most that made sense from the hundreds of results:

“In computing, the kernel is the main component of most computer operating systems; it is a bridge between applications and the actual data processing done at the hardware level. The kernel’s responsibilities include managing the system’s resources (the communication between hardware and software components).” -wikipedia

So with this, if Linux is just a kernel then it is not much of a use. From my previous entry there was a list of Linux distribution. The Linux distribution included the RedHat. So Linux Redhat would provide you with applications like browser, editor, compiler, and etc. Unix on the other hand already comes in with A-Z applications, as mentioned on the link. It turns out I do have an experience with Unix because I once deployed and tested our codes in Sun Solaris OS. I remember having a hard time with the commands. Majority of the commands are the same though but there were differences as well which I have not yet explored for we didn’t really touch more on Solaris. But if I do become familiar with all of the Linux command maybe I’ll get some time to compare them with Solaris.

Well I sure had fun learning about Unix and Linux. More and more when I am not busy with something then. Looking forward to every new learning. 🙂