 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Flash Gordon Guest
|
Posted: Sat Mar 31, 2007 9:11 am Post subject: Re: Learning C? |
|
|
CBFalconer wrote, On 31/03/07 03:44:
| Quote: | Flash Gordon wrote:
Ian Collins wrote, On 30/03/07 21:05:
... snip ...
Always is a bit strong, there can be valid reasons for compiling
C code as C++. So I'd say use a C compiler for C and not a C++
unless you have a valid valid reason to do otherwise.
P.J. Plauger has a valid reason in that some customers want to
compile his libraries with a C++ compiler and some with a C
compiler, although I would say that was more to do with his
customers being less than sensible. It is hard to come up with
other reasons where there is not a better way.
It's not for his customers, it's for himself. He doesn't want
clueless customers calling him up to complain, or badmouthing his
^^^^^^^^^^^^^^^^^^
software.
|
If his customers did not do it he would have no requirement to do it.
| Quote: | So he restricts his source to the (somewhat lame)
intersection of C and C++.
|
Indeed.
--
Flash Gordon |
|
| Back to top |
|
 |
Flash Gordon Guest
|
Posted: Sat Mar 31, 2007 9:11 am Post subject: Re: Learning C? |
|
|
Ian Collins wrote, On 31/03/07 01:34:
| Quote: | Flash Gordon wrote:
Ian Collins wrote, On 30/03/07 23:08:
Flash Gordon wrote:
Ian Collins wrote, On 30/03/07 21:05:
Flash Gordon wrote:
No, DON'T. C++ requires casting the value returned by malloc, which is
bad practice in C, and there are various other gotchas. ALWAYS use a C
compiler for C and a C++ compiler for C++ since the languages are
different.
Always is a bit strong, there can be valid reasons for compiling C code
as C++. So I'd say use a C compiler for C and not a C++ unless you
have
a valid valid reason to do otherwise.
P.J. Plauger has a valid reason in that some customers want to compile
his libraries with a C++ compiler and some with a C compiler, although I
would say that was more to do with his customers being less than
sensible. It is hard to come up with other reasons where there is not a
better way.
I've answered this before - using C++ objects to simulate hardware
accessed through structures (the test harness replaces the C struct with
a C++ object that reacts appropriately to members being accessed).
I have used decent emulators and simulators that allowed me to trap on
the accesses. Then you can compile the code with the correct compiler
for the correct target and still do complex things on either read or
write. It even means you can test the actual images that will be loaded
in to the final HW, which has the added benefit of proving that you are
not hitting any bugs in your development tools. I would say therefore
that this is a better way of solving your problem.
Not realy, using a pure hosted simulation gives you control of how the
"hardware" responds
|
So does using the simulators and emulators I have used.
| Quote: | and is way quicker for running test suites,
especially when the target is a small 8 or 16 embedded device
|
Well, back in the 80s a pure software simulator of a Z80 could run
through the tests faster than I could review the results.
| Quote: | controlling hardware with a dramatic capacity for exploding!
|
What capacity for exploding in a SW simulation? Do you think the
simulator will simulate and explosion and wreck some bits on you HD? The
better hardware emulators can also be run without the target HW and give
you no chance of exploding.
| Quote: | This is not intended as a replacement for final on-target acceptance
testing, merely an aid for development.
|
What I suggested can cover both and has none of the problems you just
suggested it had. At least, between some time in the late 80s and some
time in the late 90s it didn't, I doubt it has got any worse since I was
working on embedded systems.
--
Flash Gordon |
|
| Back to top |
|
 |
Anthony Irwin Guest
|
Posted: Tue Apr 03, 2007 9:11 am Post subject: Re: simple question |
|
|
viv342 wrote:
| Quote: | i am a beginner in c,c++.i wanted to know that what is the benifit of
declaring the prototype of a function earlier and defining it later
rather than defining it earlier
|
Doesn't having a function prototype also add type checking on the
function so if you use the wrong type as a function argument or return
type the compiler will throw an error???
Kind Regards,
Anthony Irwin |
|
| Back to top |
|
 |
Richard Heathfield Guest
|
Posted: Tue Apr 03, 2007 9:11 am Post subject: Re: simple question |
|
|
viv342 said:
| Quote: | i am a beginner in c,
|
Hi, and welcome.
Learning two languages at once may not be wise, but hey, that's your
decision. For C++ questions, of course, comp.lang.c++ is the right
newsgroup, but you do ask a C question here:
i wanted to know that what is the benifit of
| Quote: | declaring the prototype of a function earlier and defining it later
rather than defining it earlier
|
Well, you start off by defining it earlier (which is why so many people
"put main at the bottom").
Then, one day, you write some mutually recursive functions, and realise
that "put the definition before (above) the call" doesn't work any
more. So you have to forward-declare one of the functions.
Then, another day, you want to re-use half a dozen routines in another
program, and you are far-sighted enough to realise that you might want
to re-use them again and again and again. So you put the half-dozen
routines in a library, and you put their prototypes in a header, so
that the compiler need only bother with the prototypes instead of
recompiling the same source over and over.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www. |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Tue Apr 03, 2007 9:11 am Post subject: Re: simple question |
|
|
viv342 wrote:
| Quote: | i am a beginner in c,c++.i wanted to know that what is the benifit of
declaring the prototype of a function earlier and defining it later
rather than defining it earlier
It's a matter of style whether you do this. It can help with |
documentation, but I tend to work from the bottom up, adding called
function definitions above the point of first use.
--
Ian Collins. |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Tue Apr 03, 2007 9:11 am Post subject: Re: simple question |
|
|
Klarth wrote:
| Quote: | You don't have to worry about the order you define you functions in
your code. For example, you might have two functions A and B, where A
calls B. You must then define B before defining A. This is because the
compiler is a single pass compiler, so if you define A first, it
doesn't know about the existence of B on its first and only pass.
Please don't top post. |
| Quote: | On Apr 3, 12:23 pm, "viv342" <viv...@gmail.com> wrote:
i am a beginner in c,c++.i wanted to know that what is the benifit of
declaring the prototype of a function earlier and defining it later
rather than defining it earlier
|
--
Ian Collins. |
|
| Back to top |
|
 |
Bill Pursell Guest
|
Posted: Tue Apr 03, 2007 9:11 am Post subject: Re: simple question |
|
|
On Apr 3, 5:35 am, Anthony Irwin <nos...@noemailhere.nowhere> wrote:
| Quote: | viv342 wrote:
i am a beginner in c,c++.i wanted to know that what is the benifit of
declaring the prototype of a function earlier and defining it later
rather than defining it earlier
Doesn't having a function prototype also add type checking on the
function so if you use the wrong type as a function argument or return
type the compiler will throw an error???
|
Using implicit declarations is a very bad idea, and
should not be done. As long as the definition of the
function is visible, you get the same type checking.
The main benefit of having a declaration is that
the function definition can exist in a seperate
compilation unit (eg. a seperate file) and can
be compiled independently of the first. For example:
(taking some code from a thread of a few days
ago)
[tmp]$ for i in a.c b.c; do echo "*********$i********"; cat $i; done
*********a.c********
#include <stdlib.h>
void foo( int i, int n);
int
main(void)
{
foo( 4, 7);
return EXIT_SUCCESS;
}
*********b.c********
#include <stdio.h>
#define P(x) printf("%d\n", x)
void
foo( int i, int n)
{
i < n ? (P( i ), foo( i + 1, n)), P( i ) : P( n);
}
[tmp]$ gcc -Wall -pedantic -c a.c
[tmp]$ gcc -Wall -pedantic -c b.c
[tmp]$ gcc a.o b.o
This gives several benefits:
1) if you change the definition of the function foo(),
you don't need to recompile main(). In a substantial
project, compilation time can be significant, and
the ability to compile independently is a great
benefit.
2) modularity makes maintenance manageable :)
--
Bill Pursell |
|
| Back to top |
|
 |
Klarth Guest
|
Posted: Tue Apr 03, 2007 9:12 am Post subject: Re: simple question |
|
|
You don't have to worry about the order you define you functions in
your code. For example, you might have two functions A and B, where A
calls B. You must then define B before defining A. This is because the
compiler is a single pass compiler, so if you define A first, it
doesn't know about the existence of B on its first and only pass.
On Apr 3, 12:23 pm, "viv342" <viv...@gmail.com> wrote:
| Quote: | i am a beginner in c,c++.i wanted to know that what is the benifit of
declaring the prototype of a function earlier and defining it later
rather than defining it earlier |
|
|
| Back to top |
|
 |
Stephen Sprunk Guest
|
Posted: Fri Apr 06, 2007 9:11 am Post subject: Re: Undefined reference |
|
|
<wdh3rd (AT) gmail (DOT) com> wrote in message
news:1175814582.733393.238450 (AT) d57g2000hsg (DOT) googlegroups.com...
| Quote: | I'm still new at C and can't solve this problem. I've looked through
the FAQ and on the Web, but am not having luck.
I'm getting an "undefined reference" error as well as a "Id returned 1
exit status" error.
I've pared down the code to a simple example:
|
That example is very, very broken. Try this:
/* square.h */
int sq_plus (int a, int b);
/* square.c */
#include "square.h"
static int square (int a) {
return a * a;
}
int sq_plus (int a, int b) {
return square(a) - b;
}
/* main.c */
#include <stdio.h>
#include "square.h"
int main() {
int a,b;
printf( "Enter two digits: " );
scanf( "%d%d", &a, &b );
printf( "Given %d and %d, squarePlus is %d", a, b, sq_plus(a,b) );
return 0;
}
/* end source files */
How you compile these files together is platform-specific.
<OT>If you're using GCC it'll go something like this:
gcc -ansi -pedantic -W -Wall -c square.c
gcc -ansi -pedantic -W -Wall -c main.c
gcc square.o main.o -o square
The -c option tells GCC not to link yet because you're compiling multiple
source files; it will produce a .o file for the .c file it's given. The
last line actually links the various .o files together (by calling ld,
usually); since the default output file is "a.out" for historical reasons,
you need the -o option to give the program a sensible name.</OT>
| Quote: | Some questions:
(1) I thought that main() was only supposed to be in the main function
file, but if I don't have a main() in square.c, I get errors.
(2) I don't understand why I'm getting the undefined reference error.
I'm using the Dev compiler and it seems that it is ANSI-compatible.
|
This has nothing to do with ANSI; your code is broken, and on top of that
you're not compiling/linking it correctly.
| Quote: | (3) Does the "ld returned 1 exit status" error go away when the
undefined reference error is solved as I'm assuing it does?
|
Yes.
BTW, why do you call the function "square plus" if you're _subtracting_ the
second argument? Shouldn't it be "square minus"?
S
--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
--
Posted via a free Usenet account from http://www.teranews.com |
|
| Back to top |
|
 |
David Thompson Guest
|
Posted: Sun Apr 15, 2007 9:12 am Post subject: Re: Recursive functions |
|
|
On Thu, 05 Apr 2007 00:08:02 +0000, Richard Heathfield
<rjh (AT) see (DOT) sig.invalid> wrote:
| Quote: | Keith Thompson said:
snip
How did you first learn about recursion?
I found someone standing nearer to Douglas Hofstadter than me, and asked
him "how did you first learn about recursion?"
|
<OT> Aside: IIRC IME the base case was Steele or maybe Minsky, but the
principle obviously is invariant under a 'translation' like that.
Do you mean distance(someone,Hofstadter) < distance(you,Hofstadter),
which is a good example of recursion, but should properly be written
in your sentence form with 'I', or ...
distance(someone,Hofstadter) < distance(someone,you) which is better
as an example of heuristic or at least depth-first search?
;-o </> |
|
| Back to top |
|
 |
Dave Vandervies Guest
|
Posted: Tue Apr 24, 2007 11:57 pm Post subject: Re: newbie |
|
|
In article <1177458392.859662.230400 (AT) r30g2000prh (DOT) googlegroups.com>,
poison tooth <fixxie.wits (AT) gmail (DOT) com> wrote:
| Quote: | i am smc and fairly new to the programing world (but not to computers)
i know quite a bit of unix and am learning ruby but i also want to
learn C
what would be the best guide/book to get me started
|
K&R2 ("The C programming Language", second edition, by Brian Kernighan
and Dennis Ritchie, ISBN 0-13-110362- .
It's written with the assumption that the reader is already familiar
with programming, but if you're motivated enough it's enough to learn
C as your first language, and if you're learning another language in
parallel I'd expect that to be enough background.
| Quote: | (it would be easier if it were on the web)
|
I believe Steve Summit has a tutorial posted that's fairly highly
regarded. Google should be able to find it.
Most resources on the web are garbage. If you find one you like and post
a pointer to it here, we'll probably do a pretty good job of ripping it
to shreds for you.
You could also do a lot worse than reading comp.lang.c. It probably
isn't the best way to learn the language, but combined with other
resources it's an excellent way to make sure you're learning it *right*.
dave
--
Dave Vandervies dj3vande (AT) csclub (DOT) uwaterloo.ca
[T]he author of this code makes no warrenty as to its effectiveness.
Is his paranoia justified?
--Chris Dollin in comp.lang.c |
|
| Back to top |
|
 |
Peter Nilsson Guest
|
Posted: Fri May 04, 2007 3:53 am Post subject: Re: Bit manipulation |
|
|
Hallvard B Furuseth <h.b.furus...@usit.uio.no> wrote:
| Quote: | Carramba writes:
... what way would you recommend for exchanges bit from
certain position and down?
/* n < (#bits in an unsigned int), otherwise behavior is
undefined */
|
It also works if a == b.
| Quote: | void crossover(unsigned *a, unsigned *b, unsigned n) {
unsigned mask = (1U << n) - 1;
unsigned swap = (*a ^ *b) & mask;
*a ^= swap;
*b ^= swap;
}
|
If there are N value bits in unsigned int, you can implement
n in (0..N] with...
mask = (1u << n - 1 << 1) - 1;
If you want n in [0..N] then just test for n == 0...
mask = n ? (1u << n - 1 << 1) - 1 : 0;
--
Peter |
|
| Back to top |
|
 |
Thad Smith Guest
|
Posted: Fri May 04, 2007 8:44 am Post subject: Re: Bit manipulation |
|
|
Hallvard B Furuseth wrote:
| Quote: | Carramba writes:
but code seems uggly and feels unsafe.. what way would you recommend for
exchanges bit from certain position and down?
[31a 30a ...5a 4a 3a 2a 1a 0a] and [31b 30b ...5b 4b 3b 2b 1b 0b] from
position 3 =
result1 = [31a 30a ...5a 4a 3a 2b 1b 0b]
result2 = [31b 30b ...5b 4b 3b 2a 1a 0a]
/* n < (#bits in an unsigned int), otherwise behavior is undefined */
void crossover(unsigned *a, unsigned *b, unsigned n) {
unsigned mask = (1U << n) - 1;
unsigned swap = (*a ^ *b) & mask;
*a ^= swap;
*b ^= swap;
}
|
This is an elegant implementation, but the mask is off by one position.
For example, if we want to exchange bits from position 1 and down, the
mask should be 3, not 1.
--
Thad |
|
| Back to top |
|
 |
Hallvard B Furuseth Guest
|
Posted: Sat May 05, 2007 9:11 am Post subject: Re: Bit manipulation |
|
|
Thad Smith writes:
| Quote: | Hallvard B Furuseth wrote:
Carramba writes:
[31a 30a ...5a 4a 3a 2a 1a 0a] and [31b 30b ...5b 4b 3b 2b 1b 0b] from
position 3 =
result1 = [31a 30a ...5a 4a 3a 2b 1b 0b]
result2 = [31b 30b ...5b 4b 3b 2a 1a 0a]
/* n < (#bits in an unsigned int), otherwise behavior is undefined */
void crossover(unsigned *a, unsigned *b, unsigned n) {
unsigned mask = (1U << n) - 1;
unsigned swap = (*a ^ *b) & mask;
*a ^= swap;
*b ^= swap;
}
This is an elegant implementation, but the mask is off by one
position. For example, if we want to exchange bits from position 1 and
down, the mask should be 3, not 1.
|
No, his example (quoted above) shows that for "position n", n bits
should be swapped.
--
Regards,
Hallvard |
|
| Back to top |
|
 |
Hallvard B Furuseth Guest
|
Posted: Sat May 05, 2007 9:11 am Post subject: Re: Bit manipulation |
|
|
Peter Nilsson <airia (AT) acay (DOT) com.au> writes:
| Quote: | Hallvard B Furuseth <h.b.furus...@usit.uio.no> wrote:
Carramba writes:
... what way would you recommend for exchanges bit from
certain position and down?
/* n < (#bits in an unsigned int), otherwise behavior is
undefined */
It also works if a == b.
|
When n >= #bits, you mean? No. It is behavior, not just the value,
which is undefined when n in x<<n is out of range. The program might
crash, for example.
--
Regards,
Hallvard |
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|