Archive for the 'Coding' Category

Heap allocation experiment

Various compilers use different amount of memory spaces in heaps. As I compiled programs in GCC, MinGW GCC, and Microsoft Visual C++, I found different result. At a 100 byte char array, Visual C++ 2008 left 126 bytes before allocating another array, MinGW GCC left 112 bytes and in Ubuntu 8.10 Hardy, the built in GCC left 104 bytes. I used an P4 (which is a 686, supports EM64T), with 1536 MBs of RAM. And I got the mentioned results. My experiment was this:

#include <stdio.h>
#include <stdlib.h>
#define MAX 100

int main(int argc, char *argv[]){
int i;
FILE *output;
output=fopen(“data_hack1″,”w”);
fprintf(output,”char\n”);
for(i=1; i<=MAX; i++){
char *array1=(char*)malloc(i);
char *array2=(char*)malloc(1);
fprintf(output,”The free space for %d length int array is %d\n”,i,array2-array1);
}
fprintf(output,”\n\n”);

return 0;
}

You may compile and see the result yourself in the file named data.txt. You should also note what compiler and system you are using. You may also send me the result, by email (rivalslayer@yahoo.co.in) or commenting this post.

You can also try out this, and see the difference between allocating int and char.

#include <stdio.h>
#include <stdlib.h>
#define MAX 100

int main(int argc, char *argv[]){
int i;
FILE *output;
output=fopen(“data_hack2″,”w”);
for(i=1; i<=MAX; i++){
int *array1=(int*)malloc(i);
int *array2=(int*)malloc(1);
fprintf(output,”The free space for %d length int array is %d\n”,i,array2-array1);
}
fprintf(output,”\n\n”);
return 0;
}

This is also a program, which you can try out. In Ubuntu 8.10 GCC, for a 100 byte array, the program left about 26 bytes. I don’t know why! I haven’t tried this one out in other OS and compilers yet.

New form of poetry…

I was thirteen then, when I got my hands on a book called, “Basic for Schools”. That was my first step into the world of programming. The book made programming fun. Whenever I got time, I always sat in front of my computer, opened the GW-BASIC, and started to write program in that. Programs to do lots of jobs, e.g. to find prime numbers. Later, I got the Q-BASIC IDE, which also helped my a lot when writing programs.

After a year, I got a taste of C. I got someone who was learning C at that time. He was as engineering student. He didn’t had a computer, so he had to come to my home and used to write programs in that. And that gave me the taste of C. C provided finer control, he said. I got interested in C and started to learn C. I self studied the C language. I was dazzled by the flexibility of the language. To me, C was a world of fun. After a few days, I started to study Java. At first, Java was a hell lot difficult, with all those Class and Objects. I couldn’t make it out. It took time, but eventually I learned the ‘Class & Object’ thing. After I got the feeling of class, I realized how much potential it had.

Now, I like to be among codes all the time. In all these years I learned a bit of Visual Basic .NET, JavaScript, PHP and C++ also. Coding has a beautiful side. When you learn to feel the codes, you will see, that the pattern is just like poems. To me, it is another form of poetry, though it doesn’t help us to express our own feeling to others, but it does help to express our feeling to our computers.

If I am going to tell you something, I will say,”just peek into the world of programming and scripting, you will find many treasures buried inside” :-)


Tweets

Categories

rivalslayer - Flickriver
Ubuntu: For Desktops, Servers, Netbooks and in the cloud