J
jxh
This is really well written.
Thanks, Dann.
I guess that James is "James C. Hu"
Is that right?
Yep.
-- James
This is really well written.
I guess that James is "James C. Hu"
Is that right?
- matching your resource allocations with deallocations
- adding error logs for situations that are checked but
- following the coding guidelines
Is there any method to ensure each malloc() is accompanied by a
free()?
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()?
I don't know how to this. Can you give more details?
What are the coding guidelines?
Thanks.
Do I have to match each malloc() with a free()?
Is there any method to ensure each malloc() is accompanied by a
free()?
I don't know how to this. Can you give more details?
What are the coding guidelines?
santosh said:
All it is saying is that headers should be self contained. If header xThanks. 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.
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.
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?
Note the words "declaring variables in functions". Defining a functionCBFalconer said:Simple. Declaring and initializing a variable makes it exist. If
you include the header in two or more files used in your program
the variable is multi-defined, and there is a linkage error.
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.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.