qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH 1.1] virtio: Fix compiler warning for non Linux


From: Anthony Liguori
Subject: Re: [Qemu-devel] [PATCH 1.1] virtio: Fix compiler warning for non Linux hosts
Date: Wed, 30 May 2012 10:26:28 +0800
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:11.0) Gecko/20120329 Thunderbird/11.0.1

On 05/28/2012 08:39 PM, Stefan Hajnoczi wrote:
On Wed, May 23, 2012 at 5:03 PM, Stefan Weil<address@hidden>  wrote:
Am 23.05.2012 17:32, schrieb Kevin Wolf:

Maybe, but I already had patches rejected because of that style.
Did this policy change? I'd appreciate that!

Agreed, people have been asked to declare variables at the beginning
of the scope.  I don't understand why, C99 allows them to be declared
anywhere and it usually makes the code more readable IMO (you don't
have to jump to the definition to check a variable's type).

What's the problem with C99-style variable declarations anywhere in a function?

That was a dumb additional to C99 to help with C++ compatibility. The only reason it was added to C++ is because constructors can have side effects and if you want to do RAII, you really need to defer construction to right before you want to acquire the resource. IOW, if you want to do:

void foo(void)
{
   MyDataStructure *s;

   s = find_data_structure();
   // do non-trivial things to find s if it's NULL

   ScopedLock lock(s->lock);
   // more stuff holding s's lock
}

You have no choice but to do it this way. RAII is the only reasonable way to deal with exceptions too since the destructor for the lock will be called in the exception path (to release the mutex on error).

This is why C++ *had* to support mixing type declarations with code. Now here's why doing it in C is a really bad idea:

1) If you're looking at a complex function, it becomes difficult to find where the variable is declared. This can create interesting problems with shadowing.

2) It encourages having overly complicated functions. An advantage of putting variables at the top is that you quickly see when a function has way too many variables in it. Sprinkling the variable declarations through the function makes it far too easy to create monster functions.

3) It's not how the rest of QEMU is written. Consistency is the most important purpose of Coding Style.

(3) is the most important consideration of all.

Regards,

Anthony Liguori

Stefan






reply via email to

[Prev in Thread] Current Thread [Next in Thread]