Archive

Posts Tagged ‘Programming’

How I lost all of my source files in less than a minute..

October 5, 2009 Leave a comment

This one is actually pretty old, like about two weeks old, I don’t know why I didn’t post it.

So it was a sunny day, about two weeks ago. All day I tried to setup a normal makefile for my project. I wrote and discarded about 5-6 different versions. Finally after about 10-12hours I got one that was pretty good. The problem was that I couldn’t group the object files that was created, in one directory, so all objects was separated over the project directory. A simple command to remove them:
rm `find ./ -name "*.o"`
And thats it, all objects are gone. You can guess the future from this point, but Ill tell it anyway. So it is late, about 1 AM, my makefile is completed, but I need to remove the object that I might left from previous compilations, so I do:
find ./ -name "*.c"
It gives me a listing of all files that it found. Then I do
rm `find ./ -name "*.c"`
For about 20 seconds I watch the output of the command to understand what the hell I did, and when I understood… it was too late.. Here is the comment or IRC:

16:02:06 <skwo> OH F**CK
16:02:14 <skwo> I just wiped half o my os OO
16:02:24 <skwo> is there anyway to restore files that was removed by rm?
16:02:26 <skwo> omg

(Don’t pay attention to the time, the logs are logged in US time I think).

Of course there were no way to restore the files, cause the designers of EXT filesystem, decided to remove the file at all when its deleted… Yes it was embarrassing 😦 Luckily I had a backup that contained about 80% of the lost code. After this day I setup and SVN for this project, and I wont code for more than 10 hours anymore.

void main()? No int main()!

September 16, 2009 2 comments

Today at the morning, on my way to the bus station, I asked my self “What should I post today?”

Few years ago (about 3-4) I posted a question about C on one of the forums. I also included my code, it had void main() in it. People started to yell at me, that this is bad and not according the standard. Later I discovered that ANSI says that main must return a value, so I took it as a rule to follow the standard. I couldn’t really find any reason why void main() is bad, as it worked all the time no matter on what OS.

So, today when I know assembly I could find the answer to the holy war between int main and void main.

So what is the difference between void main() and int main()?

Well first of all lets take a look at the assembly code we get after we compile each version of main:

$cat foo.c
int main() { return 0; }
gcc foo.c -o foo
objdump -d foo
08048374 <main>:
08048374:     55                                 push %ebp
8048375:       89 e5                            mov %esp,%ebp
8048377:       b8 00 00 00 00     mov $0x0,%eax
804837c:       5d                                 pop %ebp
804837d:      c3                                  ret
804837e:      90                                  nop
804837f:       90                                 nop

And the second one

$cat foo.c
void main() {}
gcc foo.c -o foo
objdump -d foo
08048374 <main>:
8048374:        55                         push %ebp
8048375:        89 e5                   mov %esp,%ebp
8048377:       5d                          pop %ebp
8048378:        c3                         ret
8048379:        90                        nop
804837a:        90                        nop
804837b:        90                        nop
804837c:        90                        nop
804837d:       90                        nop
804837e:       90                        nop
804837f:        90                       nop

So the only difference in the line mov $0x0, $eax

How does return mechanism works?

So what really happens when you write return 5? This generates an assembly code of mov eax, 0x5. So we can tell for sure that the value in eax register is the value that the function returns. So why int and not void? The standard says that any application must return an error code to the OS, while 0 represents that the execution was successful and everything else represents an error. What the OS does with this value? Nothing special to be honest, but assume the following scenario:

Application A needs to execute application B. A can not continue without B finishes, however B can fail, in that case A have to fail also. How A will know that B failed? Exactly! It will check the value of the eax register after B finished to execute. If we used void main(), and the end of main eax will be undefined and may have any value! So its like Russian roulette 😛 This why we have to write int main()!

I hope this post was useful and you learned something new! Leave comments and have a good day 🙂

Categories: Programming Tags: , ,

Come Back, or “Welcome No. 2” Post

September 15, 2009 Leave a comment

Wow, I forgot about my bloggie 😦

Bloggie says: “Write ten times that you are sorry!”

Me: “I’m sorry I’m sorry I’m sorry I’m sorry I’m sorry I’m sorry I’m sorry I’m sorry I’m sorry I’m sorry”

Bloggie: “Good!”

Well for those who wonders who the hell I am. I am a 1337 programmer ^^

Nah not really, I just like computers very much, I like programming, I am good at it. In this blog Ill write stuff about computers the most, some life stuff (don’t worry I wont cry about “how sucky my life is” cause my life is awesome :p ), programming, linux, music (metal at most \m/) and thats it.

You are free to comment, share your blogs, read and enjoy 😛

See you 🙂