473,883 Members | 1,680 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 23866
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > writes:
Keith Thompson wrote:
E. Robert Tisdale writes:
4. The C compiler is going to apply the conversion
from int to char* in any case.

No. If there's a prototype in scope, the value returned by malloc()
will be treated as void*, and will be implicitly (if there's no cast)
or explicitly (if there is) converted from void* to char*. If there's
no prototype in scope, the compiler will simply assume that the value
returned by malloc() is of type int, and, in the presence of a cast,
will apply some (possibly non-trivial) conversion from int to char*.
On many systems, this happens to work (for example, if int and char*
happen to be of the same size, and the calling convention returns
integers and pointers the same way).


Can you show us an example of a "system"
on which this happens *not* to work?


Yes, an IA-64 Linux system. I could probably find a couple of others.

Here's the program that fails:
=============== =============== ==========
#include <stdio.h>

int main(void)
{
int *p;

printf("sizeof( int) = %d\n", (int)sizeof(int ));
printf("sizeof( void*) = %d\n", (int)sizeof(voi d*));

p = malloc(sizeof(i nt));
printf("p = [%p]\n", (void*)p);
fflush(stdout);

*p = 42;
printf("*p = %d\n", *p);

return 0;
}
=============== =============== ==========

And here's the output:
=============== =============== ==========
sizeof(int) = 4
sizeof(void*) = 8
p = [0xd10]
Segmentation fault
=============== =============== ==========

When I add the line "#include <stdlib.h>" after "#include <stdio.h>",
I get the following output:
=============== =============== ==========
sizeof(int) = 4
sizeof(void*) = 8
p = [0x6000000000000 d10]
*p = 42
=============== =============== ==========

Changing "malloc(sizeof( int))" to "(int*)malloc(s izeof(int))" does not
affect the behavior of either version of the program, but it changes
one of the warning messages for the failing version from "assignment
makes pointer from integer without a cast" to "cast to pointer from
integer of different size" (this is with gcc).

gcc produces a warning message about the implicit declaration of
malloc if and only if I specify "-Wall".

--
Keith Thompson (The_Other_Keit h) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #421
In article <3F************ **@jpl.nasa.gov >
E. Robert Tisdale <E.************ **@jpl.nasa.gov > writes:
Can you show us an example of a "system" on which [falsely claiming that malloc() returns int, e.g.,
by failing to include <stdlib.h>]happens *not* to work?


He may not be able to, but I have two examples:

- Certain 680x0-based systems return integers in register D0
and pointers in A0. Failure to declare malloc() correctly,
thus obtaining the implicit int declaration in C89, results
in the following machine code:

call _malloc
mov.l d0,-8(a6)

for "int *x = (int *)malloc(sizeof *x);", when the correct
code sequence is:

call _malloc
mov.l a0,-8(a6)

The value in d0 happens not to equal the value in a0 depending
on which library you use -- in at least one case, a0 is always
the same as d0+4. (This results in the "mysterious " situation
where:

int *x = (int *)malloc(sizeof *x) + 1;

actually works correctly! Some users then believe that the C
library malloc() routine "is broken, it always returns a value
four bytes smaller than the correct one".)

- Certain 80x86-based implementations have 16-bit "int"s and
32-bit pointers, in certain "mixed models". A library I wrote
back in the mid-1980s -- the "Maryland Window System", a text-
based full-screen windowing library -- broke when ported to
these systems because I had used casts (as required in pre-1989
K&R C) but failed to "#include <stdlib.h>" (which did not
exist in pre-1989 K&R C anyway).

If and when C99 compilers become typical and diagnostics are required
for failure to declare malloc(), calls that use casts will still
get the same kinds of required diagnostics as calls that both fail
to use casts and fail to have a prototype in scope. Until then,
I will continue recommending avoiding the casts. In the last twelve
years (since C89 compilers became typical), I have seen at least
a few hundred instances of bugs hidden by such casts, and zero --
count them, zero -- instances of bugs exposed by such casts.

Of course, I *also* recommend running one's compiler with "no
prototype in scope" warnings turned on. In practice, however,
this runs into three common problems:

- People use compilers that lack such warnings.

- People fail to turn on the warning anyway.

- People turn that warning (and/or others) on, but the (typically
legacy) code they are maintaining or extending produces so many
warnings that they wind up ignoring all the warnings and/or
turn them off again.

All three of these are fixable, at some cost. But compare that
with the cost/benefit equation for "cast or don't cast malloc",
where the cost of casting malloc is "sometimes hides bugs" and
"requires more keystrokes", while the benefit is -- nothing at all!
(Not, that is, unless you consider "being able to take syntactically
and semantically correct C code and compile it with a different
language's compiler, risking silent changes in the semantics that
sometimes break working code" a "benefit".)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #422
David R Tribble wrote:
.... snip ...
And I prefer this:
if (a > b and (a > c or a == -1)) ...
over this:
if (a > b && (a > c || a == -1)) ...

This is standard C++, but requires an extra #include in C.


Which is #include <iso646.h>

--
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 #423
"Keith Thompson" <ks*@cts.com> wrote in message
news:lz******** ****@cts.com...
Yes, an IA-64 Linux system. I could probably find a couple of others.


So, you can in general say that if sizeof(void *) != sizeof(int) the code
will fail?

What's sizeof(long) and sizeof(long long) on that system by the way?
Nov 13 '05 #424
Serve Lau wrote:

"Keith Thompson" <ks*@cts.com> wrote in message
news:lz******** ****@cts.com...
Yes, an IA-64 Linux system. I could probably find a couple of others.


So, you can in general say that if sizeof(void *) != sizeof(int) the code
will fail?


That's generally a sufficient condition for failure. However, it's not
the only possible way. A void* return value and an int return value
might be passed by different mechanisms, which would cause problems even
if they were the exact same size. Also, it's not unheard of for the the
conversion int->char* to produce a different output pointer than would
be produced by void*->char*, even if the void* and the int happen to
have exactly the same representation.

There's also problems with this code's argument passing if int and
size_t have different sizes, different argument passing mechanisms, or
different representations for the value being passed.

If 'int' and size_t have the same size, its likely that size_t is
"unsigned int"; in which case they'd be required to have the same
representation for positive values of the 'int'. However, that's not
actually required. For an extremely unlikely example, int could be a
4-byte little-endian type, while size_t could be a 4-byte big-endian
unsigned long.
Nov 13 '05 #425
"Serve Lau" <i@bleat.nospam .com> writes:
"Keith Thompson" <ks*@cts.com> wrote in message
news:lz******** ****@cts.com...
Yes, an IA-64 Linux system. I could probably find a couple of others.
So, you can in general say that if sizeof(void *) != sizeof(int) the code
will fail?


Not necessarily; undefined behavior is never guaranteed to "fail" (for
any definition of "fail" you can come up with). If malloc() happened
to return a pointer whose upper 32 bits were 0, it could easily work.
What's sizeof(long) and sizeof(long long) on that system by the way?


sizeof(long) and sizeof(long long) are both 64. It would probably
have made more sense to make sizeof(int) 64 as well, but that would
leave a gap in the type system (barring the use of extended types); if
short were 16 bits, there would be no 32-bit type, and vice versa.

--
Keith Thompson (The_Other_Keit h) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #426
Keith Thompson <ks*@cts.com> writes:
Fergus Henderson <fj*@cs.mu.oz.a u> writes:
I tend to use macros such as the following, defined in a per-project
header file:

#include <stdlib.h>

#define NEW(type) \
((type *) malloc(sizeof(t ype)))
...The macros look fine, but the casts are still unnecessary and
potentially dangerous. Why not just

#define NEW(type) \
(malloc(sizeof( type)))

?


That won't detect common errors where the result is assigned to a
pointer of a different type, e.g.

T1 *p = NEW(T1);
T2 *q = NEW(T1); /* cut-and-paste error, should be T2 not T1 */

--
Fergus Henderson <fj*@cs.mu.oz.a u> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.
Nov 13 '05 #427
James Kuyper <ku****@saicmod is.com> writes:
Keith Thompson wrote:
...
The macros look fine, but the casts are still unnecessary and
potentially dangerous. Why not just

#define NEW(type) \
(malloc(sizeof( type)))
Because those dangerous casts are the key motivation for those macros.
This NEW() macro is intended to do something vaguely similar to the
'new' operator in C++: it allocates memory of the correct size and
returns a pointer to the correct type. The advantage is that the type
used in the cast is always correctly matched with the type used in the
sizeof(). The disadvantage is that there's no mandatory warning if
there's no declaration of malloc() in scope,


That's not possible, since (as I made clear in my original post in this
thread) the header file which defines NEW() also includes <stdlib.h>.
or a declaration with the wrong return type.


No, since <stdlib.h> is included, a declaration with the wrong return
type would mean that a diagostic was mandatory, since a constraint
(C99 6.7 [#4]) was violated.

So, none of the disadvantages that you have pointed out actually apply
to the code that I posted. I will continue to use such code, and will
continue to object if anyone labels such code as bad practice.

--
Fergus Henderson <fj*@cs.mu.oz.a u> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.
Nov 13 '05 #428
Richard Heathfield <do******@addre ss.co.uk.invali d> writes:
Fergus Henderson wrote:
I tend to use macros such as the following, defined in a per-project
header file:

#include <stdlib.h>

#define NEW(type) \
((type *) malloc(sizeof(t ype))) .... They are safer than using malloc() manually, since they avoid the
possibility that you accidentally specify a type inside the sizeof() which
doesn't match the type of the pointer which you are assigning the result
to.


But it is only by putting a type in the sizeof that you risk the accident!
With the following technique, there is no such risk.

T *p = malloc(N * sizeof *p);


With that technique, you run a different risk.
You may accidentally type

T *p = malloc(N / sizeof *p);

or

T *p = malloc(N * sizeof p);

or

T *p = malloc(N * sizeof **p);

or

T *p = malloc(N * sizeof *p);
T *q = malloc(N * sizeof *p); /* cut-and-paste error */

and get no warning at all, even when using a good compiler such as GNU C
with all reasonable warnings enabled.

In contrast, with the NEW() macro that I described above, all of the
corresponding errors would either be caught or just wouldn't occur in
the first place.

--
Fergus Henderson <fj*@cs.mu.oz.a u> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.
Nov 13 '05 #429
Keith Thompson <ks*@cts.com> writes:
The real problem is that a cast is too blunt an instrument. If there
were an operator that meant "convert a void* value to FOO*", [...]


You might find the following macros useful:

/*
** CHECK_EXPR_TYPE (expr, type):
** This macro checks that the given expression has a type
** which is compatible with (assignable to) the specified type,
** forcing a compile error if it does not.
** It does not evaluate the expression.
** Note that the specified type must be a complete type,
** i.e. it must not be a pointer to a struct which has
** not been defined.
**
** The implementation of this macro looks like it dereferences
** a null pointer, but because that code is inside sizeof(), it will
** not get executed; the compiler will instead just check that it is
** type-correct.
*/
#define CHECK_EXPR_TYPE (expr, type) \
((void) sizeof(*(type *)NULL = (expr)))

/*
** CAST_TO_FROM(ex pr, srctype, desttype):
** The same as EXPR cast to type DESTTYPE,
** except that it checks that the expression has a type which is
** compatible with (assignable to) SRCTYPE, forcing a compile error
** if it does not.
*/
#define CAST_FROM_TO(ex pr, srctype, desttype) \
(CHECK_EXPR_TYP E((expr), srctype), \
(desttype) (expr))

--
Fergus Henderson <fj*@cs.mu.oz.a u> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.
Nov 13 '05 #430

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
10736
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
10840
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
10409
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9570
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
7969
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
7120
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
5795
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...
0
5989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3230
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.