tweaks in C

What does following code mean ?


It means that continue accepting a string before you encounter the character ‘a’. This can be used to input a string containing white space characters by putting ‘\n’ as the character where to stop.


atomicity and alignment of data in memory

A data item is aligned in memory when its address is a multiple of its size in bytes. For instance, the address of an aligned short integer must be a multiple of two while the address of an aligned integer must be a multiple of four.

Why is it important to know about alignment ?

Assembly language instructions that make zero or one aligned memory access are atomic.  Generally, a unaligned memory access is not atomic.

return and exit from main: difference

What is the difference between returning from main with some exit code and calling exit giving the exit code as parameter ?

Basically the difference between following programs !

int main()
return 43;

int main()

well, there are three ways for the processes to exit: –

1. Voluntary exit (implicit)
2. Voluntary exit (explicit)
3. Involunatary exit

Voluntary exit can be implicit e.g. in case of return and explicit e.g.
in case of a call to exit(). Involuntary exit is like being killed by a
third process, receiving a SIGSTP signal (or other signals whose default ot set
behavior results in terminating the process).

+ ‘return’ is an implicit voluntary termination of process
+ ‘exit’ is an explicit voluntary termination.

Note: both calls actually execute code in the function ‘do_exit’ ultimately.

I also tried writing above programs and inspecting the assembly just to know if
there is any difference in the code ultimately. Here are they:

[ used ‘objdump -d’ function and showing here only the function ‘main’ disassembly]

//ret.o – main
080482f4 :
80482f4: 55 push %ebp
80482f5: 89 e5 mov %esp,%ebp
80482f7: 83 ec 08 sub $0x8,%esp
80482fa: 83 e4 f0 and $0xfffffff0,%esp
80482fd: b8 00 00 00 00 mov $0x0,%eax
8048302: 29 c4 sub %eax,%esp
8048304: b8 2b 00 00 00 mov $0x2b,%eax
8048309: c9 leave
804830a: c3 ret
804830b: 90 nop

//exit.o – main
08048324 :
8048324: 55 push %ebp
8048325: 89 e5 mov %esp,%ebp
8048327: 83 ec 08 sub $0x8,%esp
804832a: 83 e4 f0 and $0xfffffff0,%esp
804832d: b8 00 00 00 00 mov $0x0,%eax
8048332: 29 c4 sub %eax,%esp
8048334: 83 ec 0c sub $0xc,%esp
8048337: 6a 2b push $0x2b
8048339: e8 26 ff ff ff call 8048264
804833e: 90 nop
804833f: 90 nop

So there is some difference and looking closely it is that in case of ‘exit’
and explicit ‘call’ is made using call instruction (and hence pushing of
arguments to the stack) while this overhead is avoided in case of a return.

Ok. so that was a short dip in the code for two empty programs – result of a free mind.

how to enforce the instantiation of objects on heap ?

A nice question, I came through while being in an interview. Well, so how do you make sure that the object can be instantiated only on heap and not on stack ? so, if you are of the kind who don’t think without being given the use and purpose .. well the purpose is that your class may be a heavy one so storing objects on stack may be a killer.

Answer popped up my mind quite easily. Let’s suppose we have a class A,

class A {
int a[10000];
void print_arr();

Use the C++ access specifiers and make the constructor itself private. Now there is no way to instantiate the class. Pretty useless.

class A {
int a[10000];
void print_arr();

Now let’s make it useful by adding a factory function (or method) to it. This will act as the only way to instantiate an object. As with any factory method, it has to be static to be actually useful. Otherwise its as dumb a class as the one above.

class A {
int a[10000];
static A* getAnObject() { return new A(); }
void print_arr();

well that was it. problem solved. I am such a happy go lucky. But my interviewer friend had yet another idea (probably) in his mind and he asked me for some other way.

I am still puzzled.. some one there for help ?

fork and vfork

quick question: what’s the difference between fork() and vfork() system calls ?

quick answer: vfork() system call creates a process that shares the memory address space of its parent.


fork() is implemented by linux as a clone() system call whose flags parameter specifies both a SIGCHLD signal and all the clone flags cleared and whose child_stack parameter is 0.

vfork() is implemented by linux as a clone() system call whose flags parameter specifies both a SGCHLD signal and flags CLONE_VM and CLONE_VFORK and whose second parameter is 0.

[ discussion: copy on write ]

This is a concept of making the process creation using fork() efficient in that instead of copying the parent’s address space while process creation, it is shared but as soon as either of them write on the page, kernel allocates a new page and assigns it to the writer process.

Most of the time, forking is required just to run a new process in which case it’s a waste to copy the whole parent address space.