Friday, November 24, 2006

Dynamic Memory Allocation in C, C++ - my strange experiences with gcc and g++

Yesterday i learnt few strange things about malloc() in C and new, delete operators in C++.
Compiler referred is gcc for C, and g++ for C++ programs.

First one:
int *a=malloc(sizeof(int) *0); //i am trying to allocate 0 bytes of memory and pointing that location with pointer a.
==> What should happen, Segmentation fault( run time error) , since allocating 0 bytes of memory is absurd and then referencing it through a pointer a is too wrong. It went through compilation and run perfectly on 'dereferencing' as a[0]=5, a[1]=2,... .
==> But i get segfault when i am trying to allocate -ve bytes of memory.

Second one:
int *a= new int[0];
==> the same effect with 0 size memory allocation and runtime error when trying to allocate (-ve)size of memory.

Third one:
This one, i strongly beleive that it sould raise runtime error, as it is mentioned in C++ standards.
int* a=new int[10];
......
delete a; //Here it should be delete[] a;
==> When an array of objects are created with new operator, while deleting all objects, we should use delete[], instead of delete; since delete will only delete that object.

Fourth one:
Do you think that on freeing or deleting the dynamic memory, the contents/memory locations will be deleted permanantly? No, it shouldn't be. The compiler will just cut of those associated links and the memory locations still with the content that we have assigned. If u want to try, assign the locations to other pointer before freeing/deleting.
My experience is:
int *a=malloc(sizeof(int)*4);
a[0]=1;a[1]=2;a[2]=3;a[3]=4;
int *t;
t=a;
free(a);
printf("%d , %d , %d , %d ",t[0],t[1],t[2],t[3]);
//Answer is "0 , 2 , 3 , 4"

int *a=new int[4];
a[0]=1;a[1]=2;a[2]=3;a[3]=4;
int *t;
t=a;
delete(a);
printf("%d , %d , %d , %d ",t[0],t[1],t[2],t[3]);

//Answer is "0 , 2 , 3 , 4"

==> I am not able to figure out the logic behind making the value of base address to 0 upon deleting. If it is to make value of deleted location as '0', then it should be with all the locations that are dynamically created. But it isn't. And also one more thing is 'write' operation is more costly than delinking.

Your comments are appreciated, if you have proper reasons.

Tuesday, November 21, 2006

Thy Self in Johari Window - an update

This post is a follow-up to my previous post, and i thought that i could update it. Thanks to those for putting their effors on letting me know better.
If you want to know more about johari window (Jeo-Harry window), please visit the following links.

http://en.wikipedia.org/wiki/Johari_window
http://kevan.org/johari
http://www.noogenesis.com/game_theory/johari/johari_window.html
http://business.nmsu.edu/~dboje/503/johari_window.htm

Saturday, November 18, 2006

BhimaShankar - One of the 12 Jyotirlingas


Last Saturday i went to BhimaShankar, which is located in the Western Ghats mountain range near to Pune. The distance is 110 km from Pune, and we (me, my friend Sridhar and two of his roommates) went by a cab. The journey is thrilling and the place is located in the far end of the Sahyadri Mountain range. Our trip is just a Half Day only, as we just want to dharshan the Swayambhu Linga. Bhimashankar is also the origin of Bhima River which merges up with Krishna river at Raichur.

This jyothirlinga is one of the famous and ancient ones, which was associated with lord 'Shiva', and has formed upon destroying the demon 'Tripurasara'. If you want to know more about the story and, its formation, refer to this page.

It is believed that the ancient shrine was erected over a Swayambhu Linga or a Self Emanated Shiva Linga. Thus the Linga is exactly in the centre of the floor of the Garbagriha or the Sanctum.

This place gives a wonderful view of Mumbai (ofcourse, which we hav been able to view due to fog), and the hill stations around. Its not more than a pilgrim’s paradise, also a trekker’s delight and traveller’s sojourn. But the ghat road is not so good, and if you opted to go by bus, you will definitely face inconvenience.

This place is good for trekkers as one of the hill is almost 90-deg slope, in the Mumbai side, and i came to know that many trekkers love to climb it. And it is also an extremely dangerous place, if slipped, 100% chances of no survival.

In the near bottom the main hill, there is one point which is said that as origin of the Bhima River. But the disappointing thing is that the Linga was covered up by a silver coating. We came to know that the cover will be released daily twice, once in the early morning 4.30 , and midday 12.00-12.30 pm. The darshan time starts early from 5.30AM and Maha Puja went at 12.00-12.30pm.
Some of the pics that we have taken in the journey.









Thursday, November 09, 2006

I am delighted (and a bit busy..)

ఈ రోజు నాకు చాల సంతోషంగా ఉంది. కావలసినంత తెలుగును చదివాను. కొన్ని రోజుల నుంచి నేను అనుకుంటున్నా నాకేమీ పని లేదు, లైఫ్ బోర్ కొడుతుందని. కాని నిన్నటి నుంచి నేను చాల బిజి. కారణం ఏంటో తెలుసా? చాల మంది తెలుగు బ్లగర్స్ వున్నారని తెలుసుకుని, వాళ్ళ బ్లాగులు చదవటం మొదలుపెట్టా. ఇంకా చదువుతూనే వున్నా.

అసలు నాకు తెలుగు ఎందుకు చదవాలని అనిపించింది అంటే, చాలా రోజుల తర్వాత నేను తెలుగులో ఉత్తరం వ్రాయల్సి వచ్చింది, మా మామయ్యకు. ఉత్తరమే ఎందుకు, ఫోన్ చెయ్యోచ్చుగా అనుకుంటున్నరా? ఒక చెక్కు పంపాల్సివచ్చింది. ఉత్త చెక్కును కవర్ లొ పెట్టి పంపలేముగా, దాంతో ఏవో కొన్ని మాటలు రాద్దామని పేపర్ తీసుకున్నా. అంతే! నాకు తెలుగు రాయటం రావట్లేదు. ఎంత ట్రై చేసినా ఇంగ్లీషులోనే పదాలు వస్తున్నాయి. చివరికి నాలుగు ముక్కలు రాసి పోస్ట్ చేసా. ఇంక అప్పుడు నిర్ణయింఛుకున్నా నేను నా తెలుగు ప్రతిభను తిరిగి తెచ్చుకోవాలని. (నా తెలుగు రైటింగ్ చాలా బాగుండేది, కాని ఈ ఇంజనీరింగ్ చదువులు చదివి అంతా మర్చిపోయా.)

వర్తమానంలోకి వస్తే, ఇప్పటి వరకు నేను కొన్ని తెలుగు బ్లాగులు మాత్రమే చదివాను. వాటిలో శోధన, చదువరి, గడ్డిపూలు, స్వాతి కుమారి, క్రిష్ణదేవరాయలు, స్నేహమా, మరి ముఖ్యంగా వీవెన్. ఇంకా నేను చదవ వలసిన బ్లాగులు చాలా వున్నై. వాటిని రేపటికి వాయిదా వేస్తూ....

- ఉమా..

Wednesday, November 01, 2006

మా తెలుగు తల్లికి మల్లెపూదండ (Maa Telugu Talli ki Mallepoodanda)


On the Occuaction of Andhra Rastra Avatarana Dinostavam!