0

I have heard a lot about the importance of programming style. In my opinion, indention is easy to deal with. But other things frustrated me a lot. Considering a particular example to demonstrate the use of inet_makeaddr.

/* demonstrate the use of host address functions */
#include <stdio.h>
#include <arpa/inet.h>
#include <netinet/in.h>

int
main(void)
{
 /* inet_makeaddr demo */
 uint32_t neta = 0x0a3e5500;
 uint32_t hosta = 0x0c;

 struct in_addr alla = inet_makeaddr(neta, hosta);

 printf("makeaddr of net: %08x and host: %08x = %08x\n", 
    neta, hosta, alla);

 return 0;
}

Somebody may want to write as follows:

 uint32_t neta;
 uint32_t hosta;
 struct in_addr alla;

 neta = 0x0a3e5500;
 hosta = 0x0c;
 alla = inet_makeaddr(neta, hosta);

Then others may always initialize the variable when defined:

 uint32_t neta = 0;
 uint32_t hosta = 0;
 struct in_addr alla = {0};

 neta = 0x0a3e5500;
 hosta = 0x0c;
 alla = inet_makeaddr(neta, hosta);

Is any one of these better than the other ones, or it is just a personal taste?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jichao
  • 40,341
  • 47
  • 125
  • 198

5 Answers5

5

I think the first of the three examples is the best: the second example one has uninitialized variables, and the third example has variables initialized to a meaningless (zero) value. I prefer to initialize variables (with a meaningful value) as soon as I define them (so that I don't have uninitialized variables). See also Should Local Variable Initialisation Be Mandatory?

Community
  • 1
  • 1
ChrisW
  • 54,973
  • 13
  • 116
  • 224
  • 2
    The third one is wasteful; there's no benefit to setting the variables to 0 and then overriding that assignment. The compiler might omit the zeroes - but it might not. – Jonathan Leffler Nov 10 '09 at 07:45
  • Not exactly,at least microsoft c/c++ compiler would initiliaze stack variable with CC.Initiliazing stack var(esp.struct) with 0 is becuase some Windows APIs only recieve zero initiliazed structures. – Jichao Nov 10 '09 at 07:50
  • @jcyang: In that case, you would actually be using 0 for the value and not immediately setting it with the value you should have used to initialize in the first place. Initializing to 0 and immediately setting to something else is just silly. – Chuck Nov 10 '09 at 08:32
  • @Chuck: In the case I only need to set some spec fileds of one structure and Windows require the other fileds to be zero-initiliazed,I will initialize the structure with zero. – Jichao Nov 10 '09 at 09:34
  • @jcyang: The 0xCC assignment is only done for debug builds (one of the reasons release code may behave differently) – Peter Olsson Nov 10 '09 at 17:55
  • @Peter Olsson:Thanks for the message.I have tried vc6 release and its true that release build does not do 0xcc assignment. – Jichao Nov 11 '09 at 01:08
  • You might like to read http://stackoverflow.com/questions/420343/separate-debug-and-release-builds – ChrisW Nov 11 '09 at 01:35
0

I like to initialize values when defining. At least you know you won't have any "silly" NULL reference errors.

Kyle Rosendo
  • 25,001
  • 7
  • 80
  • 118
0

The bottom one has the small advantage that even if you change the code so that the initialization is no longer performed, you'll never have garbage in the variables, but only zeros. The top one has the same advantage though. My own preference in C is for functions to be extremely short, so that you never have to worry about those kinds of things, so I use the top form or the second form. But if your functions are long-winded, initializing everything to zero might be the way to go.

0

Personally I define the variables close to the function call if they are interesting for the function call. If it is an uninteresting variable I usually define it it in the declaration.

Peter Olsson
  • 1,302
  • 3
  • 13
  • 19
0

It is usually always better to initialise variables in any language. Somehow it's just oen of those little things that make your life easier, just like leaving a trailing comma.

Although if you are going to initialise your variables it's probably best to do it with a value that means something to your algorithm, otherwise you're not solving anything, just changing the way everything behaves when you create a bug.

Swizec Teller
  • 2,322
  • 1
  • 19
  • 24