C++ as a better C

D

DPfan

When Stroustrup talking about C++ as a better C, he mentioned two points:

1. Inline functions in C++ are better than macros in C.
2. Easier pass-by-reference (and the avoidance of the familiar
invalid pointer argument).

Why inline functions are comparative to macros here?
Why pass-by-reference is easy in C++?
What does it mean by "avoidance of the familiar invalid pointer argument"?
 
D

David B. Held

DPfan said:
When Stroustrup talking about C++ as a better C, he
mentioned two points:

1. Inline functions in C++ are better than macros in C.
2. Easier pass-by-reference (and the avoidance of the familiar
invalid pointer argument).

Why inline functions are comparative to macros here?

Because C programmers commonly wrote macros for code
that was expected to be called many times. Since C does
not have inline functions (or does it now?), that was the fastest
way to write such code. Consider min(), for instance.
Why pass-by-reference is easy in C++?

Because there is a builtin reference type that doesn't require
you to dereference anything.
What does it mean by "avoidance of the familiar invalid
pointer argument"?

If you write a function that takes an argument by pointer, and
expects that pointer to be valid, if a user passes a null pointer,
the function has to handle that code specially.

Dave
 
N

Noah Roberts

David said:
Because C programmers commonly wrote macros for code
that was expected to be called many times. Since C does
not have inline functions (or does it now?),

I believe it does now.
 
D

DPfan

David B. Held said:
Because C programmers commonly wrote macros for code
that was expected to be called many times. Since C does
not have inline functions (or does it now?), that was the fastest
way to write such code. Consider min(), for instance.


Because there is a builtin reference type that doesn't require
you to dereference anything.


If you write a function that takes an argument by pointer, and
expects that pointer to be valid, if a user passes a null pointer,
the function has to handle that code specially.

Thanks! Does C++ also need to handle the case of null pointer in a
function?
 
K

Kevin Buffardi

Why pass-by-reference is easy in C++?

void funct(int & byRef)
{ ... }

much easier (and cleaner) than faking pass-by-reference by using pointers in
C.
 
L

lilburne

DPfan said:
Thanks! Does C++ also need to handle the case of null pointer in a
function?

No and neither does C.

The only time you need to handle a null pointer is when
either the function you call states it might return a null
pointer, or the function you write says it will accept a
null pointer.

We have an agressive policy of stating pre and post
conditions to functions. If a precondition states that ptr
must not be null then one of the first things you'll see in
the function body is:

ourAssert(ptr != 0, "Null pointer");

very occassionally you might see:

if (ptr == 0) {
ourAssertFail("Null Pointer");
return something_benign_or_error;
}

but that is pretty rare. Our coding guidelines also state
that if a method is not prepared to handle a null pointer it
should take a reference instead. Doesn't stop someone from
doing the following though:

Curve* c = 0;
do_something(*c);

that's where yor class validity checker comes in.
 
J

jeffc

DPfan said:
Thanks! Does C++ also need to handle the case of null pointer in a
function?

Yes, but the point is that when you use a reference instead, it's guaranteed
to be an actual object, so there's nothing to check. You only avoid it by
using references in C++, not simply by using C++ and the same old pointers.
 
D

Default User

Kevin said:
void funct(int & byRef)
{ ... }

much easier (and cleaner) than faking pass-by-reference by using pointers in
C.


Not the example you have, where the input variables could be silently
changed by the called function. At least if you pass pointers, you know
there's a chance something might happen to your variables.





Brian Rodenborn
 
K

Kelsey Bjarnason

When Stroustrup talking about C++ as a better C, he mentioned two points:

1. Inline functions in C++ are better than macros in C.
2. Easier pass-by-reference (and the avoidance of the familiar
invalid pointer argument).

Why inline functions are comparative to macros here?


If I have to perform some operation repeatedly, normally I'd write a
function. However, the overhead of calling the function may be high
compared to the actual runing time of the function and I may need to call
it many times - such as looping to read data from a port. Instead of
using a function, which may have unacceptably high overhead, I can use a
macro, which removes the overhead by expanding the macro's code inline.

Problem: macros have no concept of type safety and can have some nasty
side effects. I'd like to avoid these.

Inline functions offer many of the benefits of macros, without most of the
risks. Hence, if you need the speed and you have inline function support,
you'd be better off using it than macros.

Why pass-by-reference is easy in C++?

Because it was designed to be. :)

In C, to pass a value by reference and to modify the result would require
something like this:

void func( int *ptr ) { *ptr = 3; }
int main() { int x; func( &x ); return 0; }

In C++ you can accomplish the same thing this way:

void func( int& ptr ) { ptr = 3; }
int main() { int x; func(x); return 0; }


Note that you don't need to remember to use the & operator at
point-of-call (in main, in this case) and you don't need to use *ptr to
access the referenced value. In more complex code, this can save some
typing and should avoid cases where you accidentally assign a pointer,
instead of the value:

void func( int *x, int *y ) { x = y; }
int main() { int a, b = 3; func( &x, &y ); return 0; }

-- Presumably, func is supposed to change the value in a to 3 - but
because the coder forgot to use *x and *y, it doesn't. Worse, because
the assignment he made is perfectly valid, no diagnostic is likely to be
emitted.

What does it mean by "avoidance of the familiar invalid pointer
argument"?


Try this:

void func( int *x ) { *x = 3; }

int main()
{
int *a, *b = NULL;
a = malloc(sizeof(int));
b = a;
free(a);

if ( b )
func(b);

return 0;
}

What happens when func(b) is called? Func gets an invalid pointer, and
you have a bug. It is somewhat more difficult to get an invalid reference
in C++ which would duplicate this sort of problem.
 
D

Default User

Kelsey said:
In C, to pass a value by reference and to modify the result would require
something like this:

void func( int *ptr ) { *ptr = 3; }
int main() { int x; func( &x ); return 0; }

In C++ you can accomplish the same thing this way:

void func( int& ptr ) { ptr = 3; }
int main() { int x; func(x); return 0; }


This usage is fairly controversial. The C pointer version alerts the
user of the function that there is a danger the variables used in the
function call may be modified. There is no such warning when called with
references.

The CLC++ FAQ addresses this:


http://www.parashift.com/c++-faq-lite/references.html#faq-8.6


It comes down on the side of using references as above, but many
programmers (and not just old C programmers) disagree with this.
Stroustrup recommends against it, see Section 5.5 of TC++PL 3rd Ed.




Brian Rodenborn
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top