473,883 Members | 1,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why still use C?

no this is no trollposting and please don't get it wrong but iam very
curious why people still use C instead of other languages especially C++.

i heard people say C++ is slower than C but i can't believe that. in pieces
of the application where speed really matters you can still use "normal"
functions or even static methods which is basically the same.

in C there arent the simplest things present like constants, each struct and
enum have to be prefixed with "struct" and "enum". iam sure there is much
more.

i don't get it why people program in C and faking OOP features(functi on
pointers in structs..) instead of using C++. are they simply masochists or
is there a logical reason?

i feel C has to benefit against C++.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et
Nov 13 '05
687 23893
Richard Heathfield wrote:
CBFalconer wrote:
I, and apparently almost everyone else, thinks your malloc casts
are ridiculous.

.... snip ...
Since Mr Plauger is a highly respected figure (and rightly so,
IMHO), I am not prepared to dismiss his opinion lightly, but I am
curious to know the reasons for that opinion. Until then at least,
I will of course continue to recommend that casting malloc is
spurious and counter-productive.


Well, my post was primarily about the enum usage problem. However
I suspect that PJPs usage revolves around supplying source code,
probably shrouded, for installation on many systems with various
qualities of compilers. There are machines out there with only
K&R1 compilation facilities. He doesn't want to do any
hand-holding.

Shrouded code is not quite suitable for patching or debugging :-)
This does not apply to most of us. Maybe he will confirm or deny.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #341
E. Robert Tisdale wrote:
CBFalconer wrote:
I and, apparently, almost everyone else, thinks that
your malloc casts are ridiculous.
I don't think that they are ridiculous.


I don't think Chuck cares what you think.
The only possible reason for them is to compile with C++
which all consider a spurious reason.


I don't think that it is a spurious reason.


I don't think Chuck cares what you think.
C++ is, after all, the future of C.


No, C++ is a part of C's history. It's in the past. C is its own future.

<snip>

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #342
Dave Vandervies wrote:
Well yes, although I feel

foo = (struct myweirdstruct *)malloc(sizeof (struct myweirdstruct)) ;

looks prettier still.
How 'bout this one?
foo=(int (*)[17][42])malloc(105 * sizeof(int (*)[17][42]));
(Compare to:
foo=malloc(105* sizeof *foo);
I like this one much better.)


I do like the terseness of foo=malloc(105* sizeof *foo), but with a
"typedef int (*foop)[17][42]" I'd still prefer the cast :-)

Best regards, Sidney

Nov 13 '05 #343
Arthur J. O'Dwyer wrote:
On Tue, 21 Oct 2003, Sidney Cadot wrote:
Arthur J. O'Dwyer wrote:
int *x = malloc(1000*siz eof(double)) /* would go unnoticed */

No competent programmer would ever write a line such as the above.
(I can say this because you've already assured me that *you* would
never write the above, and neither would I, so I'm safe.
That's not much of an argument is it?


Well, the "rationalizatio n" for the "no competent programmer" line
*was* tongue-in-cheek --


I'm sorry, I was a bit annoyed at the time I replied.
however, it is true that no competent programmer would make a
mistake as blatant as the above.
That's quite a statement! Like chess grandmasters overlooking a
mate-in-one, every expert makes silly mistakes from time to time I think.
Anyone
who types that many characters to perform such a simple operation
should look at that line and say, "Hmm... this is more verbose than
needed. Oh! and it's also wrong. I'll fix it now."
Let me guess.... That's _also_ tongue-in-cheek, right? :-)
[...]
The c.l.c idiom is also the least verbose and most error-proof
idiom allowed by the language.
The second is as true as can be, the second, well, I needn't repeat
earlier statement.
(Brevity and correctness tend to
go hand-in-hand in C, because of the close relationship between
lines of code and bytes of executable. Verbose code promotes
tangledness, if you see what I mean.)
Casting doesn't add to the executable size of course, so I'd say this is
not applicable.
Sometimes you may want to alloc without directly assigning to a
variable. malloc()'s result is just an expression, as is the result
of 'new int[100]' in C++.

Yes (although trying to do strcpy(malloc(1 0),"hello")) in C *will*
bite you in the rear one of these days).


Sure, this is not very practical. But it goes to illustrate the point
that leaving the cast to the assignment is presuming too much. Sad thing
(for my argumentation anyway) is that you do assign the malloc() result
in 99% of cases, and even in your strcpy example there will be an
implicit cast to the type of the format argument. Darn :-)
int *y = (int*)malloc(10 00*sizeof(doubl e)) /* would be noticed */ [snip]

Yup. :) Would you notice it if the type-names were reversed, so
you actually *did* trash the heap on some systems, but not others?
Would you notice it if 1000 were 100000, thus running the risk of
a large memory "hole"?


This can be a mess. I think this is valid objection number three to
malloc result casting, and for me at least the most convincing one.
To be [clear], I'm talking compile-time here. Could you point me to
the right section of the standard that supports your assertion?


N869 section 6.5.16.1#2, which describes the order of operations
during the assignment:

malloc() is called, and then returns a (void *).
That (void *) is converted to (double *) and assigned to x.

With the cast, we get:

malloc() is called, and then returns a (void *).
That (void *) is converted to (double *) [and then...]
That (double *) is assigned to x.

Same exact thing. No change in executable; no change in value;
no change in type. Just extra verbiage.


I was thinking in terms of how a compiler would handle this: with
casted-malloc, it first changes the type of the void* to double*, after
which it will go and handle double*-to-double* assignment. with
uncasted-malloc, it will recognise void*-to-double* assigment and then
either directly or indirectly (via an inserted cast) generate the same
code, which is a slightly different execution path for the compiler to take.

However, the standard of course does not specify how a compiler should
do what it must do, so you are, in fact, absolutely right.
Yes, it's been rejected by most, rather out-of-hand I would say. I still
think my enum-to-int example is fair game, and I guess I could up with
some more if I did my best.

The enum-to-int thing is interesting, I'll admit. Of course, I've
never had any problems with enums myself; and anyway, I think the
*real* problem you have there is an abstraction issue -- you want
to change all your "magic numbers" to mnemonic names, right? Well,
that issue isn't limited to enums -- it applies to #defined names
just as much. Maybe your "house style" uses more enums than #defines,
but I think that's the exception rather than the rule.
We do that, yes. But my view on enums as something distinct from ints
stems more from early exposure to Pascal.
[...] But you've only definitively shown that it can help with the (very
rare in most circles) case of

enum Value { VALUE_FOO = 2 };

my_function_tak ing_enum(2);
/* needs to be */
my_function_tak ing_enum(VALUE_ FOO);
That's true, I've only given one example, and I must admit it took me a
while to think of it (it's been quite some time ago since I last did C++
in any seriousness). I still believe there are more examples. I'm
thinking about that.
That's something that I think most C programmers would solve with

grep 'enum' externs.h
grep 'my_function_ta king_enum' *.c
grep 'my_other_funct ion_taking_enum ' *.c
...
the one or two times in their lifetimes that the problem arose.
Certainly faster than trying to make their C code lint under C++,
*just* to catch this one potential bug.


....And I thought _I_ was practical kind of guy! :-)

Best regards,

Sidney

Nov 13 '05 #344
CBFalconer wrote:
I, and apparently almost everyone else, thinks your malloc casts
are ridiculous. The only possible reason for them is to compile
with C++, which all consider a spurious reason.
So I've noticed. I tried hard to make my case but I have not conviced
anyone. Perhaps it's time to just let it slide.
However, the enum <-> int case is different. It is the only way,
apart from struct, to truly create a new data type in C. As long
as those assignments go unflagged, it does very little good (but
is not useless). I think you (and I) would be happy if gcc could
be told to emit a warning for such usages.


I think we would, yes.

Best regards,
Sidney
Nov 13 '05 #345
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote in message news:<bm******* ***@sparta.btin ternet.com>...
James Kuyper wrote:
Christian Bau wrote: ....
I think the majority of actual C programs will not compile as C++
programs at all; if the programmer never heard of C++ then it is just
too easy to have "new", "class" or "template" as an identifier.


I doubt that this problem, while quite real, affects "a majority" of
actual C programs. I think the number of C programs that wouldn't
compile because they still use unprototyped function declarations
(mainly in legacy code, one hopes) is a much larger. Does anyone care to
provide actual numbers? I have no idea where to find them.


Well, one place I can look is in my current development directory. I chose
the first fifty C modules I could find[1].
Below, "Yes" means "C source compiled cleanly under a C++ compiler in
conforming mode", and "No" mea... well! :-)

Yes : 13
No : 37

I was pleasantly surprised that so many "passed" the test, but not at all
surprised that so many "failed". Some of the failures were for rather
esoteric reasons, e.g. to do with constness, but most were to do with void
*, and there was the occasional keyword issue as well.
[1] Hey, I don't have all day, okay? :-)


Well, what I was really looking for was a little more detailed: a
count of failed compilations, broken down by the reason for the
failure. How many failed because of C++ keywords, and how many because
of missing prototypes, and how many because of failure to cast
malloc() return values, etc.. However, beggars can't be choosers.

I just tried the same test with the first 81 C modules I could find,
all of which are part of the same C89 program, and most of which have
been modified by me personally at least once, and reviewed by me
multiple times. This set was significantly cleaner with respect to C++
compilation than yours:

Yes: 79
No: 2

Those two failures were due to the fact that the code used UINT16_MAX,
and relied upon a third-party header file which #included <stdint.h>.
Since both our code and the third party library are supposed to
"conform to C89", this counts as a pair of defects, modulo the fact at
"conform" doesn't actually mean as much as the writers of that
requirement apparantly expected.

The version of <stdint.h> that got #included #defines UINT16_MAX when
the compiler is in strict C89 ANSI mode, but not when the compiler is
in C++ mode. I can't argue that either behavior is incorrect, since
<stdint.h> isn't a standard header for either of those languages, and
UINT16_MAX isn't reserved in either language. It was processed exactly
as non-standard header should be, in both cases.

I'd argue that the compiler's ability to detect this complicated
defect had nothing to do with any special virtue of the C++ language,
but is merely a complicated side-effect of a peculiar choice made by
the implementors of of this particular implementation of <stdint.h>.
Nov 13 '05 #346
"Richard Heathfield" <do******@addre ss.co.uk.invali d> wrote in message
news:bn******** **@hercules.bti nternet.com...
CBFalconer wrote:
I, and apparently almost everyone else, thinks your malloc casts
are ridiculous.


True.
The only possible reason for them is to compile
with C++, which all consider a spurious reason.


Not true (ah, if only you had said "almost all"!). P J Plauger's mileage
varies. I'm still not sure why, though, because (AFAIAA) he has not
expounded the reason for his reason. That is, he has weighed in on the side
of malloc casting, and has pointed out that this is so that he can gain an
advantage by programming in the common subset of C and C++, but I'm not
entirely sure what that advantage is.

Since Mr Plauger is a highly respected figure (and rightly so, IMHO), I am
not prepared to dismiss his opinion lightly, but I am curious to know the
reasons for that opinion. Until then at least, I will of course continue to
recommend that casting malloc is spurious and counter-productive.


I thought I had given more than one reason, en passant, but here's a quick
review. Indeed the first motivation we had for adding casts to malloc calls
was to satisfy a market need to have all our Standard C library compile either
as C or C++. The C++ Standard (thanks to me) deliberately permits the C library
to have either extern "C" or extern "C++" linkage -- and we have serious
customers for both flavors. Having done so, we then found reasons to make
*all* of our C source code compilable as C++.

Generally I avoid casts for the reason so often given -- they're so strong
that they often hide problems. We add them where required for correctness,
for portability, for C++ compatibility, and to quiet silly warnings. Our
experience over the years is that the last category of casts do indeed
raise the cost of maintenance -- we fix something here and the forgotten
consequences there are masked by the cast, until we discover the bug by
a painful runtime trace.

But allocator casts often *help* us find bugs that arise during maintenance.
The classic pattern is a structure declared here, containing a pointer to
another structure declared somewhere else, for which storage is allocated
there, and freed some other there. That's why we allocate N times the
size of the thing we really want, cast the resultant pointer to a pointer
to that type, and store it in what should be a compatible type of pointer.
We've had the compiler inform us of bugs in both size and pointer types
as the code changes under maintenance and enhancement.

In our environment, bugs like failing to include stdlib.h get found and
fixed in the first pass or two of code writing. We generally perform
*many* passes over our code during development, and develop a companion
set of tests. So the kind of bugs masked by casts on malloc are cheap
to fix, while the kind exposed by such casts are much more expensive.

HTH,

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 13 '05 #347
"CBFalconer " <cb********@yah oo.com> wrote in message news:3F******** *******@yahoo.c om...
Richard Heathfield wrote:
CBFalconer wrote:
I, and apparently almost everyone else, thinks your malloc casts
are ridiculous.

... snip ...

Since Mr Plauger is a highly respected figure (and rightly so,
IMHO), I am not prepared to dismiss his opinion lightly, but I am
curious to know the reasons for that opinion. Until then at least,
I will of course continue to recommend that casting malloc is
spurious and counter-productive.


Well, my post was primarily about the enum usage problem. However
I suspect that PJPs usage revolves around supplying source code,
probably shrouded, for installation on many systems with various
qualities of compilers. There are machines out there with only
K&R1 compilation facilities. He doesn't want to do any
hand-holding.

Shrouded code is not quite suitable for patching or debugging :-)
This does not apply to most of us. Maybe he will confirm or deny.


Some people accused us of shrouding our earliest release of C++,
because it was so terse. But we now have a style with much more
whitespace and commenting. So it is certainly not our intention
to ship anything but readable and maintainable code.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 13 '05 #348
P.J. Plauger wrote:
In our environment, bugs like failing to include stdlib.h
get found and fixed in the first pass or two of code writing.
cat malloc.c // #include <stdlib.h>
#include <stdio.h>

int main(int argc, char* argv[]) {
const
int n = 20;
int* p = (int*)malloc(n* sizeof *p);
for (int j = 0; j < n; ++j)
p[j] = j;
for (int j = 0; j < n; ++j)
fprintf(stdout, "%2d = p[%2d]\n", p[j], j);
return 0;
}
gcc -Wall -std=c99 -pedantic -o malloc malloc.c malloc.c: In function `main':
malloc.c:7: warning: implicit declaration of function `malloc' gcc --version

gcc (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)

My GNU C compiler easily detects the missing declaration for malloc
with or without the cast.
I don't see *any* advantage to omitting the explicit cast.

Nov 13 '05 #349
On Sat, 18 Oct 2003 19:56:35 GMT, "P.J. Plauger" <pj*@dinkumware .com>
wrote:
And I've never been sorry. A more curious person might wonder why.


Because you've never ported your code to a machine with 16 bit ints and 24
bit pointers.
--
#include <standard.discl aimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 13 '05 #350

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
11266
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL) on the server because of that. Our site will have an SSL certificate next week, so I would like to use AIM instead of SIM, however, I don't know how to send data via POST over https and recieve data from the Authorize.net server over an https...
2
5865
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues to execute the code until the browser send his reply to the header instruction. So an exit(); after each redirection won't hurt at all
3
23053
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which field is completed.
0
8508
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. 354 roberto@ausone:Build/php-4.3.2> ldd /opt/php4/bin/php libsablot.so.0 => /usr/local/lib/libsablot.so.0 libstdc++.so.5 => /usr/local/lib/libstdc++.so.5 libm.so.1 => /usr/lib/libm.so.1
1
8621
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the column below. The viewer can select states from the drop down lists above the other two columns as well. If the viewer selects only one, only one column fills. If the viewer selects two states, two columns fill. Etc. I could, if appropriate, have...
4
18317
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the user comes back to a page where he had a submitted POST data the browser keeps telling that the data has expired and asks if repost. How to avoid that? I tried registering all POST and GET vars as SESSION vars but
1
6890
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url http://www.mis.gla.ac.uk/biquery/training/ but each of the courses held have maximum of 8 people that could be
2
31461
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value to :parameter I dont like the idea of making the SQL statement on the fly without binding parameters as I dont want a highly polluted SQL cache.
3
23617
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the results of the picture half the size. The PHP I have installed support 1.62 or higher. And all I would like to do is take and image and make it fit a 3x3. Any suggestions to where I should read or look would be appreciated.
0
9791
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11137
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10845
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9571
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7970
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7123
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5797
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4609
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.