473,396 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to be a better C programmer?

I personally like C, and do not like any OO languages. The reference
books for OO languages are too heavy for me. They just made things
complicated. Someone laughed at my opinion, saying Google code bases
are mostly written in C++.

I read somewhere about the best way to learn C (or a programming
language in general). I agree with the points. I quote them below:

"The best way to do it is to read some stuff written by masters of the
form, write some things yourself, read a lot more, write a little
more, read a lot more, write some more ... and repeat until your
writing begins to develop the kind of strength and economy you see in
your models."

For C books, I have carefully read The C Programming Language, The C
Answer Book, and The C Puzzle Book. For real C code, I have carefully
read about 20,000 lines written by UC Berkeley guys. I myself have
written about 15,000 lines, not counting tiny toy programs. It is my
situation.

My question is where I can find "some stuff written by masters of the
form" ? I think BSD source code is written by masters. Am I right?
Apr 11 '08 #1
36 1853
On Apr 10, 8:37 pm, istillsh...@gmail.com wrote:
I personally like C, and do not like any OO languages. The reference
books for OO languages are too heavy for me. They just made things
complicated. Someone laughed at my opinion, saying Google code bases
are mostly written in C++.

I read somewhere about the best way to learn C (or a programming
language in general). I agree with the points. I quote them below:

"The best way to do it is to read some stuff written by masters of the
form, write some things yourself, read a lot more, write a little
more, read a lot more, write some more ... and repeat until your
writing begins to develop the kind of strength and economy you see in
your models."

For C books, I have carefully read The C Programming Language, The C
Answer Book, and The C Puzzle Book. For real C code, I have carefully
read about 20,000 lines written by UC Berkeley guys. I myself have
written about 15,000 lines, not counting tiny toy programs. It is my
situation.

My question is where I can find "some stuff written by masters of the
form" ? I think BSD source code is written by masters. Am I right?
By the way, I think the author of PuTTY is a master. I used PuTTY
everyday and am fond of it. I examined its source code, and became
fond of it too.
Apr 11 '08 #2
By the way, I think the author of PuTTY is a master. I use PuTTY
everyday and am fond of it. I examined its source code, and became
fond of it too.

Apr 11 '08 #3
On Apr 11, 2:38 am, user923005 <dcor...@connx.comwrote:
Depends on who wrote it. BSD and relatives are written by a lot of
people.
For instance:
If Chris Torek wrote it, it's good.
If Peter McIlroy wrote it, it's good.
If Keith Bostic wrote it, it's good.
If Jon Bentley wrote it, it's good.
How about Terrence R. Lambert?
>
I think looking at what experts did is a good idea. But it is more
important to understand the fundamental nature of algorithms.
Otherwise, you will write stuff that is as pretty as theirs but it
blows chunks in performance.
I studied Fundamentals of Data Structure (in C) many years ago. I
could not recall the author's name now. Is that a good book? I need
to find time to review it again.

Apr 11 '08 #4
On Apr 11, 9:55*am, istillsh...@gmail.com wrote:
On Apr 11, 2:38 am, user923005 <dcor...@connx.comwrote:
Depends on who wrote it. *BSD and relatives are written by a lot of
people.
For instance:
If Chris Torek wrote it, it's good.
If Peter McIlroy wrote it, it's good.
If Keith Bostic wrote it, it's good.
If Jon Bentley wrote it, it's good.

How about Terrence R. Lambert?
His stuff is good (I went and looked over some of his work). My list
was not intended to be exhaustive. The authors that I mentioned were
named because I know of their work fairly well because they tend to
work on things that are interesting to me so I read them.
I think looking at what experts did is a good idea. *But it is more
important to understand the fundamental nature of algorithms.
Otherwise, you will write stuff that is as pretty as theirs but it
blows chunks in performance.

I studied Fundamentals of Data Structure (in C) many years ago. *I
could not recall the author's name now. *Is that a good book? *I need
to find time to review it again.
I have not read that one, so I cannot comment on it.
I like Knuth, Budd, Weiss, Sedgewick
and this is a must-read:
http://www.amazon.com/Introduction-A.../dp/0262531968
If you don't want to buy it, check it out from a library.
Apr 11 '08 #5
On Apr 10, 7:37 pm, istillsh...@gmail.com wrote:
I personally like C, and do not like any OO languages. The reference
books for OO languages are too heavy for me. They just made things
complicated. Someone laughed at my opinion, saying Google code bases
are mostly written in C++.

I read somewhere about the best way to learn C (or a programming
language in general). I agree with the points. I quote them below:

"The best way to do it is to read some stuff written by masters of the
form, write some things yourself, read a lot more, write a little
more, read a lot more, write some more ... and repeat until your
writing begins to develop the kind of strength and economy you see in
your models."

For C books, I have carefully read The C Programming Language, The C
Answer Book, and The C Puzzle Book. For real C code, I have carefully
read about 20,000 lines written by UC Berkeley guys. I myself have
written about 15,000 lines, not counting tiny toy programs. It is my
situation.

My question is where I can find "some stuff written by masters of the
form" ? I think BSD source code is written by masters. Am I right?
A Rumsfeldian koan: there are true masters; there are people who think
they are masters; there are people whom other people think are
masters; and then there's the rest of us.

The trick is figuring out who's who.

Ultimately, practice is the only way to significantly improve your
skills. It's good to look at what other people have done to get a
feel for good style and practice, but until you've pounded out
thousands of lines of code for yourself, you may not understand *why*
the code was written that way. You have to make mistakes, and go
through the effort of fixing those mistakes, to appreciate the lessons
that other people's code provides.
Apr 11 '08 #6
John Bode wrote:
>
.... snip ...
>
Ultimately, practice is the only way to significantly improve your
skills. It's good to look at what other people have done to get a
feel for good style and practice, but until you've pounded out
thousands of lines of code for yourself, you may not understand
*why* the code was written that way. You have to make mistakes,
and go through the effort of fixing those mistakes, to appreciate
the lessons that other people's code provides.
One of the advantages of having been at this for a while is that
you learn what your common mistakes are, and can arrange to
orgamize the code to automatically detect them. A couple of simple
tricks are:

1. Make similary functions have similar parameter lists.
2. Organize statements so that error will show up.

As an example of 2 consider "if (2 = x) ...". Placing the constant
first means that using a single = sign (the fault) is automatically
compiler detected.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #7
CBFalconer <cb********@yahoo.comwrites:
[...]
One of the advantages of having been at this for a while is that
you learn what your common mistakes are, and can arrange to
orgamize the code to automatically detect them. A couple of simple
tricks are:

1. Make similary functions have similar parameter lists.
2. Organize statements so that error will show up.

As an example of 2 consider "if (2 = x) ...". Placing the constant
first means that using a single = sign (the fault) is automatically
compiler detected.
Just to offer another perspective, I personally find the "if (2 == x)"
form extremely ugly. I understand that it's logically and
mathematically equivalent to "if (x == 2)" form, and that it can help
catch some errors, but for me it's just not worth it.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #8
On Apr 12, 4:38 am, CBFalconer <cbfalco...@yahoo.comwrote:
As an example of 2 consider "if (2 = x) ...". Placing the constant
first means that using a single = sign (the fault) is automatically
compiler detected.
Although many people recommend the form if (2 == x), I don't like it.
It is contrary to my common sense.
Jun 27 '08 #9
CBFalconer <cb********@yahoo.comwrites:
John Bode wrote:
>>
... snip ...
>>
Ultimately, practice is the only way to significantly improve your
skills. It's good to look at what other people have done to get a
feel for good style and practice, but until you've pounded out
thousands of lines of code for yourself, you may not understand
*why* the code was written that way. You have to make mistakes,
and go through the effort of fixing those mistakes, to appreciate
the lessons that other people's code provides.

One of the advantages of having been at this for a while is that
you learn what your common mistakes are, and can arrange to
orgamize the code to automatically detect them. A couple of simple
tricks are:

1. Make similary functions have similar parameter lists.
Whatever that means. I think it needs explaining.
2. Organize statements so that error will show up.

As an example of 2 consider "if (2 = x) ...". Placing the constant
first means that using a single = sign (the fault) is automatically
compiler detected.
I personally find that form to be ugly and at odds with the great
majority of code out there. "=" and "==" does happen but very rarely
with any half competent C programmer.
Jun 27 '08 #10
Richard wrote:
CBFalconer <cb********@yahoo.comwrites:
>John Bode wrote:
>>>
... snip ...
>>>
Ultimately, practice is the only way to significantly improve your
skills. It's good to look at what other people have done to get a
feel for good style and practice, but until you've pounded out
thousands of lines of code for yourself, you may not understand
*why* the code was written that way. You have to make mistakes,
and go through the effort of fixing those mistakes, to appreciate
the lessons that other people's code provides.

One of the advantages of having been at this for a while is that
you learn what your common mistakes are, and can arrange to
orgamize the code to automatically detect them. A couple of simple
tricks are:

1. Make similary functions have similar parameter lists.

Whatever that means. I think it needs explaining.
> 2. Organize statements so that error will show up.

As an example of 2 consider "if (2 = x) ...". Placing the constant
first means that using a single = sign (the fault) is automatically
compiler detected.

I personally find that form to be ugly and at odds with the great
majority of code out there. "=" and "==" does happen but very rarely
with any half competent C programmer.
Also many compilers and all lints give you warning about such typos.
Frankly I find it surprising that using = when you want == is
apparently a common error, since I have never committed this myself,
though I have of course had my share of typos.

Jun 27 '08 #11
santosh wrote:
) Frankly I find it surprising that using = when you want == is
) apparently a common error, since I have never committed this myself,
) though I have of course had my share of typos.

I've had keyboards that were a bit worn out, with the effect that
sometimes in double keystrokes, the second wasn't recognized.
That suddenly makes using = instead of == a common typo.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Jun 27 '08 #12
santosh said:

<snip>
Frankly I find it surprising that using = when you want == is
apparently a common error, since I have never committed this myself,
How do you know?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #13
CBFalconer <cb********@yahoo.comwrote:
As an example of 2 consider "if (2 = x) ...". Placing the constant
first means that using a single = sign (the fault) is automatically
compiler detected.
Not only is that aesthetically abominable, it leads one into a false
sense of security. Here you are, cleverly having set up all your
equalities so that you _know_ that a typo will lead the compiler to
complain, and then you type "if (table_of_data[1] = temporary_value)"...

Richard
Jun 27 '08 #14
Richard Bos said:
CBFalconer <cb********@yahoo.comwrote:
>As an example of 2 consider "if (2 = x) ...". Placing the constant
first means that using a single = sign (the fault) is automatically
compiler detected.

Not only is that aesthetically abominable,
Beauty is in the eye of the beholder. What about all those aesthetically
abominable breaks out of loops?
it leads one into a false sense of security.
One, maybe, but not all.
Here you are, cleverly having set up all your
equalities so that you _know_ that a typo will lead the compiler to
complain,
For certain values of "you", possibly. Here *I* am, wisely having set up my
equalities-that-have-constants with the constants on the left so that I
know the compiler will complain if I've typed an = operator instead of an
== operator *when comparing against a constant*.

There is no silver bullet. Putting constants on the left of comparisons
will not cause the compiler to cause about *all* typographical errors.
Neither will it cure whooping cough or boils. That doesn't mean it isn't
worth doing.
and then you type "if (table_of_data[1] = temporary_value)"...
....then presumably you have a bug <shrug>.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #15
Richard Heathfield wrote:
Richard Bos said:
>and then you type "if (table_of_data[1] = temporary_value)"...

....then presumably you have a bug <shrug>.
Which your unit tests will catch :)

--
Ian Collins.
Jun 27 '08 #16

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:3-******************************@bt.com...
Richard Bos said:
>CBFalconer <cb********@yahoo.comwrote:
>>As an example of 2 consider "if (2 = x) ...". Placing the constant
first means that using a single = sign (the fault) is automatically
compiler detected.

Not only is that aesthetically abominable,

Beauty is in the eye of the beholder. What about all those aesthetically
abominable breaks out of loops?

Even with just the one break? And how else would you stop this fragment
running forever:

#define endlessloop while(1)

endlessloop {
break;
}

(Apart from using return, goto or exit.)

--
Bart
Jun 27 '08 #17
Bartc said:
>
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:3-******************************@bt.com...
>Richard Bos said:
>>CBFalconer <cb********@yahoo.comwrote:

As an example of 2 consider "if (2 = x) ...". Placing the constant
first means that using a single = sign (the fault) is automatically
compiler detected.

Not only is that aesthetically abominable,

Beauty is in the eye of the beholder. What about all those aesthetically
abominable breaks out of loops?


Even with just the one break? And how else would you stop this fragment
running forever:

#define endlessloop while(1)
#undef endlessloop

And then I'd re-write the loop in accordance with my aesthetic prejudices.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #18

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:3-******************************@bt.com...
Richard Bos said:
>CBFalconer <cb********@yahoo.comwrote:
>>As an example of 2 consider "if (2 = x) ...". Placing the constant
first means that using a single = sign (the fault) is automatically
compiler detected.

Not only is that aesthetically abominable,

Beauty is in the eye of the beholder. What about all those aesthetically
abominable breaks out of loops?

Even with just the one break? And how else would you stop this fragment
running forever:

#define endlessloop while(1)

endlessloop {
break;
}

(Apart from using return, goto or exit.)

--
Bart
Jun 27 '08 #19
Bartc said:
>
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:3-******************************@bt.com...
>Richard Bos said:
>>CBFalconer <cb********@yahoo.comwrote:

As an example of 2 consider "if (2 = x) ...". Placing the constant
first means that using a single = sign (the fault) is automatically
compiler detected.

Not only is that aesthetically abominable,

Beauty is in the eye of the beholder. What about all those aesthetically
abominable breaks out of loops?


Even with just the one break? And how else would you stop this fragment
running forever:
Same way I did last time.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #20

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:3q******************************@bt.com...
Bartc said:
>>
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:3-******************************@bt.com...
>>Richard Bos said:

CBFalconer <cb********@yahoo.comwrote:

As an example of 2 consider "if (2 = x) ...". Placing the constant
first means that using a single = sign (the fault) is automatically
compiler detected.

Not only is that aesthetically abominable,

Beauty is in the eye of the beholder. What about all those aesthetically
abominable breaks out of loops?


Even with just the one break? And how else would you stop this fragment
running forever:

Same way I did last time.
Yeah, I have serious machine problems I think. At the moment the entire
motherboard is perched precariously on a road atlas on top of the case. I
think there's a bad connection in there somewhere.

Makes a mere software bug seem so trivial.

--
Bart
Jun 27 '08 #21
On Apr 11, 2:32 pm, user923005 <dcor...@connx.comwrote:
I studied Fundamentals of Data Structure (in C) many years ago. I
could not recall the author's name now. Is that a good book? I need
to find time to review it again.

I have not read that one, so I cannot comment on it.
I like Knuth, Budd, Weiss, Sedgewick
and this is a must-read:http://www.amazon.com/Introduction-A...omas-Cormen/dp...
If you don't want to buy it, check it out from a library.
It was not Fundamentals of Data Structure (in C). I was Data
Structure and Algorithm Analysis (in C) that I studied. Seven years
have passed; I almost forgot the book's name.
I think I would not bother Knuth's book. Data Structure and Algorithm
Analysis (in C) was good enough for me. I felt the subject quite
difficult 7 years ago, when I was a 2nd
year undergrad.
Jun 27 '08 #22
jxh
On Apr 10, 5:37*pm, istillsh...@gmail.com wrote:
...
I read somewhere about the best way to learn C (or a
programming language in general). I agree with the points. I
quote them below:

"The best way to do it is to read some stuff written by masters
of the form, write some things yourself, read a lot more, write
a little more, read a lot more, write some more ... and repeat
until your writing begins to develop the kind of strength and
economy you see in your models."
...
Note that in what you quote there is no mention of C. This is
because becomming a better C programmer is a specialization of
becoming a better programmer, which can also be taken to be a
specialization of becoming a better problem solver.

In this vein, I would encourage regularly taking part of any
activity that exercises the problem solving muscle of your
brain. The focus is on fundamentals:
- understanding the problem
- identifying the knowns
- identifying the relevant unknowns
- breaking the problem down into smaller problems if required
- determining if the problem (or sub-problems) resembles a
previously solved problem
- etc.

Problem solving, and programming in particular, is a detail
oriented activity. So, if you are not already, you should
train yourself to become a detail oriented person.
- pay attention to the "small" stuff, including (but not
limited to)
- commenting code
- matching your resource allocations with deallocations
- checking all return values for errors
- sanity checking passed in parameter values
- adding error logs for situations that are checked but
are considered to be exceptional
- following the coding guidelines
- double check your work
- incrementally unit test the code as the project progresses

The benefit of being a detail oriented person (one that
cares about dotting i's and crossing t's) is that you should
find yourself spending less time fixing bugs in your code.
Either you are coding fewer bugs, or that the bugs that do
occur have sufficient instrumentation that allows you to
identify the source of the problem quickly. Spending less
time fixing bugs in your code gives you more time to write
new code, or help fix bugs in other people's code, and both
of these help increase your standing among your peers.

For C programming specifically, if you are already comfortable
with the basics of the language itself (which I gather from
the C books you listed) I would add that the C-faq itself
should be read carefully. It provides good answers on how to
deal with the most common C programming issues (at least as
presented to the comp.lang.c forum).

-- James
Jun 27 '08 #23
On Apr 14, 6:18*pm, jx*@yahoo.com wrote:
On Apr 10, 5:37*pm, istillsh...@gmail.com wrote:
...
I read somewhere about the best way to learn C (or a
programming language in general). *I agree with the points. *I
quote them below:
"The best way to do it is to read some stuff written by masters
of the form, write some things yourself, read a lot more, write
a little more, read a lot more, write some more ... and repeat
until your writing begins to develop the kind of strength and
economy you see in your models."
...

Note that in what you quote there is no mention of C. *This is
because becomming a better C programmer is a specialization of
becoming a better programmer, which can also be taken to be a
specialization of becoming a better problem solver.

In this vein, I would encourage regularly taking part of any
activity that exercises the problem solving muscle of your
brain. *The focus is on fundamentals:
*- understanding the problem
*- identifying the knowns
*- identifying the relevant unknowns
*- breaking the problem down into smaller problems if required
*- determining if the problem (or sub-problems) resembles a
* *previously solved problem
*- etc.

Problem solving, and programming in particular, is a detail
oriented activity. So, if you are not already, you should
train yourself to become a detail oriented person.
*- pay attention to the "small" stuff, including (but not
* *limited to)
* *- commenting code
* *- matching your resource allocations with deallocations
* *- checking all return values for errors
* *- sanity checking passed in parameter values
* *- adding error logs for situations that are checked but
* * *are considered to be exceptional
* *- following the coding guidelines
*- double check your work
*- incrementally unit test the code as the project progresses

The benefit of being a detail oriented person (one that
cares about dotting i's and crossing t's) is that you should
find yourself spending less time fixing bugs in your code.
Either you are coding fewer bugs, or that the bugs that do
occur have sufficient instrumentation that allows you to
identify the source of the problem quickly. *Spending less
time fixing bugs in your code gives you more time to write
new code, or help fix bugs in other people's code, and both
of these help increase your standing among your peers.

For C programming specifically, if you are already comfortable
with the basics of the language itself (which I gather from
the C books you listed) I would add that the C-faq itself
should be read carefully. *It provides good answers on how to
deal with the most common C programming issues (at least as
presented to the comp.lang.c forum).

-- James
This is really well written.
I guess that James is "James C. Hu"
Is that right?
Jun 27 '08 #24
jxh
On Apr 14, 7:21*pm, user923005 <dcor...@connx.comwrote:
On Apr 14, 6:18*pm, j...@yahoo.com wrote:
...
This is really well written.
Thanks, Dann.
I guess that James is "James C. Hu"
Is that right?
Yep.

-- James
Jun 27 '08 #25
Thanks.

On Apr 14, 9:18 pm, j...@yahoo.com wrote:
- matching your resource allocations with deallocations
Do I have to match each malloc() with a free()? It is easy to omit a
few free()'s, but the program still works. I know it is not a good
idea not to free resources.
Is there any method to ensure each malloc() is accompanied by a
free()?
- adding error logs for situations that are checked but
I don't know how to this. Can you give more details?
- following the coding guidelines
What are the coding guidelines?
-- James
Jun 27 '08 #26
On 15 Apr 2008 at 20:03, is*********@gmail.com wrote:
Is there any method to ensure each malloc() is accompanied by a
free()?
There are many leak-detection programs available. A really excellent one
for Linux and BSD is valgrind.

Jun 27 '08 #27
jxh
On Apr 15, 1:03*pm, istillsh...@gmail.com wrote:
On Apr 14, 9:18 pm, j...@yahoo.com wrote:
- matching your resource allocations with deallocations

Do I have to match each malloc() with a free()? It is easy to
omit a few free()'s, but the program still works. I know it is
not a good idea not to free resources. Is there any method to
ensure each malloc() is accompanied by a free()?
The point of becoming detail oriented is to pay attention when
you use resource allocation and match it with a deallocation.
You want to avoid creating a resource leaks instead of spending
time to debug them.

There are many types of resources. In C, there are memory, files,
and variable argument lists that have APIs to allocate and
deallocate. C APIs outside of C proper introduce many more types
of resources. Avoiding resource leaks is best accomplished by
vigilantly checking each allocation is accompanied with a
deallocation, and such vigilance requires attention to detail.

There are tools to help track down resource leaks (purify, for
instance), but the goal is to write code that doesn't have this
problem in the first place.
>
* *- adding error logs for situations that are checked but

I don't know how to this. *Can you give more details?
Error logs typically mean a mechanism that records that
something wrong happened, and that this record can be
consulted when you discover that your program has a bug.
Typically, this means a file that is written to, but it
doesn't have to be. In C, some kind of call to fprintf
can serve to log errors to a file.
* *- following the coding guidelines

What are the coding guidelines?
These are typically supplied by your employer, or is
dictated by the chaperones of the code base you are
modifying or extending. They typically govern coding
style. I can usually peg a novice as being detail
oriented or not by whether their coding style matches
the coding style of the rest of the file to with they
were tasked with adding some code.

-- James
Jun 27 '08 #28
is*********@gmail.com wrote:
j...@yahoo.com wrote:
> - matching your resource allocations with deallocations

Do I have to match each malloc() with a free()? It is easy to
omit a few free()'s, but the program still works. I know it is
not a good idea not to free resources.

Is there any method to ensure each malloc() is accompanied by
a free()?
The easiest way is to write code of the form:

if (!(p = malloc(N * sizeof *p)) correct_the_fault(p);
....
/* code using p */
....
free(p);

which may nor always be suitable to the rest of your logic. Then
you have to look at other methods, such as lists, arrays, etc. But
the above will certainly work.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #29
is*********@gmail.com wrote:
Thanks.

On Apr 14, 9:18 pm, j...@yahoo.com wrote:
> - matching your resource allocations with deallocations

Do I have to match each malloc() with a free()?
If you're interested in robust code, yes. Admittedly most modern
operating systems reclaim all of the programs memory after the latter
terminates, but some programs like servers and daemons don't terminate
(to be precise, they run for a long time), so deallocating memory when
you have finished using it will allow your malloc implementation to, if
needed, return the memory to the system. This allows other programs to
use it and also keeps the total memory consumption of your program to
reasonable levels.

As you would probably have noticed most applications don't do this, but
that's no reason to imitate such practises.
Is there any method to ensure each malloc() is accompanied by a
free()?
If you mean an automated method, then no. C leaves that task completely
to the programmer. This gives great flexibility but it also requires
careful attention to details.

If you don't mind a reduction in portability then you might consider
using a Garbage Collector. A well respected one for C is the Boehm GC
(http://www.hpl.hp.com/personal/Hans_Boehm/gc/).
> - adding error logs for situations that are checked but

I don't know how to this. Can you give more details?
In general logging means that your program periodically saves
information and/or state to some form of permanent storage, to enable
programmers, system administrators, and users to study the execution of
the program and any problems it encounters. Many programs have their
own logging routines (or a secondary program for that purpose), but you
can also use something like syslog (http://www.syslog.org/).
> - following the coding guidelines

What are the coding guidelines?
<http://en.wikipedia.org/wiki/Code_conventions>
<http://en.wikipedia.org/wiki/Programming_style>

Jun 27 '08 #30
santosh wrote:
What are the coding guidelines?

<http://en.wikipedia.org/wiki/Code_conventions>
<http://en.wikipedia.org/wiki/Programming_style>

Thanks. I examined the NetBSD coding guidelines and liked it. I
don't not understand
the following statement though, particularly "and not depend on the
including file for that header including both" in the first sentence.

/*
* If a header file requires structures, defines, typedefs, etc. from
* another header file it should include that header file and not
depend
* on the including file for that header including both. If there are
* exceptions to this for specific headers it should be clearly
documented
* in the headers and, if appropriate, the documentation. Nothing in
this
* rule should suggest relaxation of the multiple inclusion rule and
the
* application programmer should be free to include both regardless.
*/
Jun 27 '08 #31
On Apr 15, 10:21 pm, santosh <santosh....@gmail.comwrote:
- following the coding guidelines
What are the coding guidelines?

<http://en.wikipedia.org/wiki/Code_conventions>
<http://en.wikipedia.org/wiki/Programming_style>
Thanks. I checked the NetBSD coding guidelines and liked them. I
don't understand the following statement though, particularly "and not
depend on the including file for that header including both" in the
first sentence.
What dose it mean?

/*
* If a header file requires structures, defines, typedefs, etc. from
* another header file it should include that header file and not
depend
* on the including file for that header including both. If there are
* exceptions to this for specific headers it should be clearly
documented
* in the headers and, if appropriate, the documentation. Nothing in
this
* rule should suggest relaxation of the multiple inclusion rule and
the
* application programmer should be free to include both regardless.
*/
Jun 27 '08 #32
is*********@gmail.com wrote:
santosh wrote:
>>What are the coding guidelines?
<http://en.wikipedia.org/wiki/Code_conventions>
<http://en.wikipedia.org/wiki/Programming_style>


Thanks. I examined the NetBSD coding guidelines and liked it. I
don't not understand
the following statement though, particularly "and not depend on the
including file for that header including both" in the first sentence.
All it is saying is that headers should be self contained. If header x
uses something declared in header y it should include it, rather than
the user of header x having to include both x and y. This is a common,
but not universal, guideline.

--
Ian Collins.
Jun 27 '08 #33
is*********@gmail.com wrote:
On Apr 15, 10:57 pm, Ian Collins <ian-n...@hotmail.comwrote:
>istillsh...@gmail.com wrote:
>>santosh wrote:
What are the coding guidelines?
<http://en.wikipedia.org/wiki/Code_conventions>
<http://en.wikipedia.org/wiki/Programming_style>
Thanks. I examined the NetBSD coding guidelines and liked it. I
don't not understand
the following statement though, particularly "and not depend on the
including file for that header including both" in the first sentence.
All it is saying is that headers should be self contained. If header x
uses something declared in header y it should include it, rather than
the user of header x having to include both x and y. This is a common,
but not universal, guideline.

I really learned something useful from the NetBSD style guide. But
why doesn't it allow
initialize variables' values in their declarations? I do that often.
That's a very contentions rule. I always try and introduce variables
when they can be initialised and not before.

--
Ian Collins.
Jun 27 '08 #34
jxh
On Apr 15, 7:21*pm, santosh <santosh....@gmail.comwrote:
istillsh...@gmail.com wrote:
On Apr 14, 9:18 pm, j...@yahoo.com wrote:
* *- matching your resource allocations with deallocations
Do I have to match each malloc() with a free()?

If you're interested in robust code, yes. Admittedly most
modern operating systems reclaim all of the programs memory
after the latter terminates, but some programs like servers and
daemons don't terminate (to be precise, they run for a long
time), so deallocating memory when you have finished using it
will allow your malloc implementation to, if needed, return
the memory to the system. This allows other programs to use it
and also keeps the total memory consumption of your program to
reasonable levels.
There are some types of resources which will not be reclaimed
after the program terminates, and other types for which no
tools exist to track their utilization, and are not associated
with memory that can be garbage collected. That is why I
emphasized the notion of resource rather than memory.

To give an example: Suppose there is a program that
continuously writes data to a file until the file has reached a
certain size. At that point, the program opens a new file and
the operation iterates.

As specified above, there are (at least) two problems:
- The program never closes the files it opens. On many systems
this will cause the program to eventually be unable to open
a file (and may cause any other program on the same system
to be unable to open a file as well).
- The medium on which the files reside is unlikely to have
infinite capacity. When the medium reaches capacity, the
program (and likely the entire system) will fail. This
problem would exist even if the program was fixed to close
the file it had previously opened.

-- James
Jun 27 '08 #35
On Apr 10, 5:37*pm, istillsh...@gmail.com wrote:
I personally like C, and do not like any OO languages. *The reference
books for OO languages are too heavy for me. *They just made things
complicated. *Someone laughed at my opinion, saying Google code bases
are mostly written in C++.

I read somewhere about the best way to learn C (or a programming
language in general). * I agree with the points. *I quote them below:

"The best way to do it is to read some stuff written by masters of the
form, write some things yourself, read a lot more, write a little
more, read a lot more, write some more ... and repeat until your
writing begins to develop the kind of strength and economy you see in
your models."

For C books, I have carefully read The C Programming Language, The C
Answer Book, and The C Puzzle Book. *For real C code, I have carefully
read about 20,000 lines written by UC Berkeley guys. *I myself have
written about 15,000 lines, not counting tiny toy programs. *It is my
situation.

My question is where I can find "some stuff written by masters of the
form" ? *I think BSD source code is written by masters. *Am I right?

I really wouldn't use 20,000 lines written by UC Berkeley guys as any
kind of benchmark. Let me explain. Once upon a time a person that
barely passed high school could have gone to the local community
college, taken a bunch of classes to improve their GPA and then could
have transferred into UC-Berkeley. I'm pretty sure a few them them
probably became computer science majors. A person that wasn't aware of
this process would be inclined to automatically assume the person that
wrote the C code had to be a brilliant because they went to UC-
Berkeley. When in reality, it was just some person that knew how to
play the 'college game.'

However, this is no longer the case. A few years ago UC-Berkeley
finally stopped admitting people that went to the local community
college to improve their GPA.
Jun 27 '08 #36
On 15 Apr, 23:03, CBFalconer <cbfalco...@yahoo.comwrote:
istillsh...@gmail.com wrote:
j...@yahoo.com wrote:
* *- matching your resource allocations with deallocations
Do I have to match each malloc() with a free()? *It is easy to
omit a few free()'s, but the program still works. *I know it is
not a good idea not to free resources.
Is there any method to ensure each malloc() is accompanied by
a free()?

The easiest way is to write code of the form:

* *if (!(p = malloc(N * sizeof *p)) correct_the_fault(p);
* *....
* */* code using p */
* *....
* *free(p);
yes, but simpler would be

P_type p;
/* code using p */

:-)

The only reason to use malloc() with code like this if your
implementation allows dynamic objects to be larger than automatic
objects. Or your stack (hello trolls!) is small.

A likely reason for dynamic data is where allocation and freeing
are widely separated or statically unpredictable (eg. event driven).

Or the number or size of objects is unpredicable.

which may nor always be suitable to the rest of your logic. *Then
you have to look at other methods, such as lists, arrays, etc. *But
the above will certainly work.
yes you then neen lists or arrays or whatever.

One trick to isolate the memory management in a separate "module".

Ultimately there is no replacement for care.

--
Nick Keighley

Jun 27 '08 #37

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
47
by: Jeff Relf | last post by:
Hi All, I plan on using the following C++ code to create nodes with unlimited children: // I would like to declare NodeT like this, // but it won't compile because Lnk_T is not defined yet....
15
by: gabor | last post by:
hi, there are 2 versions of a simple code. which is preferred? === if len(line) >= (n+1): text = line else:
39
by: bazad | last post by:
Hi, I am not using C all the time. I have a general understanding of C and nothing else. The recent reply to use strlcpy and strlcat showed me that I am not aware of the best and safe...
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
23
by: JoeC | last post by:
I am a self taught programmer and I have figured out most syntax but desigining my programs is a challenge. I realize that there are many ways to design a program but what are some good rules to...
21
by: Newish | last post by:
Hi Can someone please enlighten me. I hear different things. Some suggest that the difference between the languages in terms of capability and versitility has really narrowed down to a point...
19
by: geese786 | last post by:
my vote for navia.
43
by: Pawel_Iks | last post by:
I've read somewhere that c++ is something more than better c ... then I talk with my friend and he claimed that c++ is nothing more than better c ... I tried to explain him that he was wrong but I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.