Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 08:40 PM
DPfan
Guest
 
Posts: n/a
Default C++ as a better C

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"?



  #2  
Old July 19th, 2005, 08:40 PM
David B. Held
Guest
 
Posts: n/a
Default Re: C++ as a better C

"DPfan" <DPfan@yahoo.com> wrote in message
news:pmVmb.15545$Ec1.1412769@bgtnsc05-news.ops.worldnet.att.net...[color=blue]
> 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?[/color]

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.
[color=blue]
> Why pass-by-reference is easy in C++?[/color]

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

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



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003


  #3  
Old July 19th, 2005, 08:40 PM
Noah Roberts
Guest
 
Posts: n/a
Default Re: C++ as a better C

David B. Held wrote:
[color=blue]
> 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?),[/color]

I believe it does now.
--
Noah Roberts
- "If you are not outraged, you are not paying attention."

  #4  
Old July 19th, 2005, 08:40 PM
Kevin Buffardi
Guest
 
Posts: n/a
Default Re: C++ as a better C

> 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.


--
Kevin Buffardi
"Rockstars -- is there
anything they don't know?"
-Homer Simpson


  #5  
Old July 19th, 2005, 08:40 PM
DPfan
Guest
 
Posts: n/a
Default Re: C++ as a better C

David B. Held <dheld@codelogicconsulting.com> wrote in message
news:bnh8pp$ep4$1@news.astound.net...[color=blue]
> "DPfan" <DPfan@yahoo.com> wrote in message
> news:pmVmb.15545$Ec1.1412769@bgtnsc05-news.ops.worldnet.att.net...[color=green]
> > 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?[/color]
>
> 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.
>[color=green]
> > Why pass-by-reference is easy in C++?[/color]
>
> Because there is a builtin reference type that doesn't require
> you to dereference anything.
>[color=green]
> > What does it mean by "avoidance of the familiar invalid
> > pointer argument"?[/color]
>
> 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.
>[/color]

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


  #6  
Old July 19th, 2005, 08:40 PM
lilburne
Guest
 
Posts: n/a
Default Re: C++ as a better C

DPfan wrote:
[color=blue]
> David B. Held <dheld@codelogicconsulting.com> wrote in message
> news:bnh8pp$ep4$1@news.astound.net...
>[color=green]
>>
>>[color=darkred]
>>>What does it mean by "avoidance of the familiar invalid
>>>pointer argument"?[/color]
>>
>>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.
>>[/color]
>
>
> Thanks! Does C++ also need to handle the case of null pointer in a
> function?
>[/color]

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.

  #7  
Old July 19th, 2005, 08:42 PM
jeffc
Guest
 
Posts: n/a
Default Re: C++ as a better C


"DPfan" <DPfan@yahoo.com> wrote in message
news:t8Xmb.195009$0v4.15123638@bgtnsc04-news.ops.worldnet.att.net...[color=blue]
> David B. Held <dheld@codelogicconsulting.com> wrote in message
> news:bnh8pp$ep4$1@news.astound.net...[color=green]
> > "DPfan" <DPfan@yahoo.com> wrote in message
> > news:pmVmb.15545$Ec1.1412769@bgtnsc05-news.ops.worldnet.att.net...[color=darkred]
> > > 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?[/color]
> >
> > 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.
> >[color=darkred]
> > > Why pass-by-reference is easy in C++?[/color]
> >
> > Because there is a builtin reference type that doesn't require
> > you to dereference anything.
> >[color=darkred]
> > > What does it mean by "avoidance of the familiar invalid
> > > pointer argument"?[/color]
> >
> > 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.
> >[/color]
>
> Thanks! Does C++ also need to handle the case of null pointer in a
> function?[/color]

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.


  #8  
Old July 19th, 2005, 08:42 PM
Default User
Guest
 
Posts: n/a
Default Re: C++ as a better C

Kevin Buffardi wrote:[color=blue]
>[color=green]
> > Why pass-by-reference is easy in C++?[/color]
>
> void funct(int & byRef)
> { ... }
>
> much easier (and cleaner) than faking pass-by-reference by using pointers in
> C.[/color]


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
  #9  
Old July 19th, 2005, 08:43 PM
Kelsey Bjarnason
Guest
 
Posts: n/a
Default Re: C++ as a better C

On Sun, 26 Oct 2003 19:35:49 +0000, DPfan wrote:
[color=blue]
> 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?[/color]


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.

[color=blue]
> Why pass-by-reference is easy in C++?[/color]

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.

[color=blue]
> What does it mean by "avoidance of the familiar invalid pointer[/color]
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.


  #10  
Old July 19th, 2005, 08:45 PM
Default User
Guest
 
Posts: n/a
Default Re: C++ as a better C

Kelsey Bjarnason wrote:[color=blue]
> 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; }[/color]


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-lit...s.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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles