473,396 Members | 2,023 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.

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

Jul 19 '05 #1
9 2183
"DPfan" <DP***@yahoo.com> wrote in message
news:pm*********************@bgtnsc05-news.ops.worldnet.att.net...
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

---
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
Jul 19 '05 #2
David B. Held wrote:
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.
--
Noah Roberts
- "If you are not outraged, you are not paying attention."

Jul 19 '05 #3
> 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
Jul 19 '05 #4
David B. Held <dh***@codelogicconsulting.com> wrote in message
news:bn**********@news.astound.net...
"DPfan" <DP***@yahoo.com> wrote in message
news:pm*********************@bgtnsc05-news.ops.worldnet.att.net...
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.


Thanks! Does C++ also need to handle the case of null pointer in a
function?
Jul 19 '05 #5
DPfan wrote:
David B. Held <dh***@codelogicconsulting.com> wrote in message
news:bn**********@news.astound.net...

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.

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.

Jul 19 '05 #6

"DPfan" <DP***@yahoo.com> wrote in message
news:t8***********************@bgtnsc04-news.ops.worldnet.att.net...
David B. Held <dh***@codelogicconsulting.com> wrote in message
news:bn**********@news.astound.net...
"DPfan" <DP***@yahoo.com> wrote in message
news:pm*********************@bgtnsc05-news.ops.worldnet.att.net...
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.


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.
Jul 19 '05 #7
Kevin Buffardi wrote:
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.

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
Jul 19 '05 #8
On Sun, 26 Oct 2003 19:35:49 +0000, DPfan wrote:
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.
Jul 19 '05 #9
Kelsey Bjarnason wrote:
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-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
Jul 19 '05 #10

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...
3
by: Muhd | last post by:
<usualDisclaimer>Please forgive me if this is in the wrong group, and if so, what is the right group.</usualDisclaimer> Let me start off by first saying im a newb. Ok, with that out of the way I...
24
by: Faith Dorell | last post by:
I really donīt like C.You can write better programs in BASIC than in C, if you donīt like this language. I donīt understand how C became so popular, although much better programming languages...
43
by: Rob R. Ainscough | last post by:
I realize I'm learning web development and there is a STEEP learning curve, but so far I've had to learn: HTML XML JavaScript ASP.NET using VB.NET ..NET Framework ADO.NET SSL
33
by: Protoman | last post by:
Which is better for general-purpose programming, C or C++? My friend says C++, but I'm not sure. Please enlighten me. Thanks!!!!!
22
by: JoeC | last post by:
I am working on another game project and it is comming along. It is an improvment over a previous version I wrote. I am trying to write better programs and often wonder how to get better at...
19
by: Alexandre Badez | last post by:
I'm just wondering, if I could write a in a "better" way this code lMandatory = lOptional = for arg in cls.dArguments: if arg is True: lMandatory.append(arg) else: lOptional.append(arg)...
23
by: mike3 | last post by:
Hi. (posted to both newsgroups since I was not sure of which would be appropriate for this question or how specific to the given language it is. If one of them is inappropriate, just don't send...
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
3
by: Ryan Liu | last post by:
Hi, Is Async I/O (e.g. NetworkStream.Begin/End Read/Write) always better than synchronous I/O? At least as good? When I don't concern about easy or difficult to write code, should I always...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.