Calling C library functions from C++ program

M

mthread

Hi,
I am using C (ex : calloc etc..) library functions in my C++
program. I am able to compile and link without any issue. But I am
skeptic about the usage (ie whether it will cause any issues in
future) as I have learned that both C & C++ follow their own language
linking.

I have also understood in order to call a C method from a C++ program
I need to declare the method using
' extern "C" ' keyword. Kindly let me know if it necessary for me
declare the C methods with ' extern "C" ' keyword for my usage in C++
program.
 
S

Sherm Pendley

Paavo Helde said:
For using e.g. malloc() you should not declare it yourself. You should
include the relevant header, e.g. <stdlib.h>

Shouldn't that be <cstdlib>?

sherm--
 
J

jason.cipriani

<stdlib.h> is working and not going away. As far as I have understood,
calling it deprecated was kind of wishful thinking in the current standard,
and will be corrected in the next standard. IMO, the main controversy is
that it is not understandable why I should call a C function through a name
in std:: namespace, albeit in C there are no namespaces.

It's understandable because the C++ standard defines it as so (sorry I
do not have the document in front of me to cite section numbers). In C+
+, use <cstdlib>, and std::malloc(). It's true <stdlib.h> is not going
away, and I have to admit I frequently use it myself, but it's only
maintained for backwards compatibility with C.

I'm not so sure it will be "corrected" in the next standard (nor what
you mean by "corrected"), but they'd certainly be well within their
rights to make stdlib.h go away entirely, seeing as how it's been
deprecated for quite some time.

Jason
 
J

James Kanze

This has been up before. See e.g.

By "corrected" I was referring to something what James Kanze
mentioned in a message in that thread: "Implementations where
the <c...> libraries work as specified are so rare that the
standards committee changed the specification."

The change is independant of any removal of deprecation. In the
current standard, inclusion of a <c...> header is not allowed to
introduce any names in the global namespace (except things
required to be macros, like assert). In practice, most
implementations use the C library, doing something along the
lines of:

#include <stdio.h>
namespace std {
using ::printf ;
...
}

There are some very, very good reasons for this, but according
to the current standard, it's illegal. The next version of the
standard will authorize it---if you include <c...>, you're
guaranteed to get the names in std::, and it's unspecified
whether they are also present in ::.
However, I am not fully sure what the change involved. I
thought this was the removal of "deprecated" status of C
headers, but rereading that thread I now see they only talk
about who exactly should propose the issue to the committee.

The current CD still declares the <...h> headers as
"deprecated". (CD, here, means committee draft; an official
draft which has been submitted for voting on by the national
bodies.)

I'll admit that I'm of two minds as to which to use. Somethings
are so much a part of C++ (e.g. size_t, or time()), that it
seems normal to prefix them with an std::, and thus to include
<cstddef> or <ctime>. On the other hand, if you really are
using them "as C", or if you're using them as Posix headers
(e.g. <time.h>, with strptime), it seems more normal (to me, at
least) to use the <xxx.h> forms, with the functions in ::. Of
course, if you're into using std::vector<T>::size_type, rather
than size_t or std::size_t, and other solutions for e.g. time
(say Boost), then about the only times you'll want to include
them is for Posix. And Posix doesn't defined any <cxxx>
headers. (Of course, in cases where Posix defines a modified
version of the C headers, with extensions, I'd expect to find
those extensions available in the corresponding <cxxx> header,
in the same way as the standard C functions are. But that's a
QoI issue, not guaranteed by any standard.)
 
J

Jobin Paul

Hi,
If you are skeptic about stdlib.h then just use cstdlib which will
bring the c functions to std::.
Which also means they are c++ equivalent to the c ones. ( May be both
will get linked to the same library by the compiler)

Then you should never worry about linkage and declaration

Regards
Jobin
 
J

Juha Nieminen

mthread said:
I have also understood in order to call a C method from a C++ program
I need to declare the method using
' extern "C" ' keyword.

Nitpicking, but C has no "methods". A method is, by definition, a
member function of a class, and C has none of that.

What C has is functions.
 
J

Juha Nieminen

Paavo said:
Nitpicking continued: C++ also does not have methods; instead it has member
functions. The standard only mentions "method" in sentences like: "The
method by which a sequence of preprocessing tokens between a < and a >
preprocessing token pair or a pair of " characters is combined into a
single header name preprocessing token is implementation-defined."

Just because the standard doesn't use the term "method" as a synonym
for "member function" doesn't mean it isn't. Maybe the standard simply
wants to avoid confusion by using a more unambiguous term (and
rightfully so).
 
S

Stefan Ram

Juha Nieminen said:
Just because the standard doesn't use the term "method" as a
synonym for "member function" doesn't mean it isn't.

Correct, but it means that by the standard
we can not tell whether it is.
 
J

James Kanze

Just because the standard doesn't use the term "method" as a
synonym for "member function" doesn't mean it isn't.

The problem is that no two people agree exactly what "method"
should mean in C++. The "experts" don't use it, so you can't
base your meaning on what they do, and in other contexts, the
use varies.

The word was introduced in Smalltalk, I think, to stress the
fact that this wasn't a function call; in Smalltalk, you don't
call a method, you send a message, which results in a method
being invoked to handle it. At least, I think this was the
Smalltalk vocabulary. Using it for something in a statically
typed language, where you really do "call" a function, is really
more a marketing ploy than anything else---it makes your
language sound more OO, but it certainly doesn't help
communications. (Eiffel seems to speak of commands and queries,
which correspond more or less to non-const and const functions
in C++. A decidedly better choice than method. Or function,
for that matter; in C++, function a misnomer as well; procedure
or routine would be a better choice, especially when the return
is void.)
Maybe the standard simply wants to avoid confusion by using a
more unambiguous term (and rightfully so).

The choice of method by Smalltalk wasn't very judicious, given
the other meanings of the word, and its widespread use in
software engineering with those meanings. Still, context is
almost always sufficient to disambiguate, and I don't think it
creates a real problem there.

When talking about C++, using the word method does create a
problem, because one is never sure what is meant: any function,
just member functions, just non-static member functions, just
virtual member functions...
 
J

Juha Nieminen

James said:
When talking about C++, using the word method does create a
problem, because one is never sure what is meant: any function,
just member functions, just non-static member functions, just
virtual member functions...

Btw, given that "static" is just a kind of hackish reuse of a C
keyword in C++ class functions, what would you call those functions?
You have member functions (which might or might not be virtual), virtual
functions (which are specifically dynamically bound), and... What would
be the proper name for those function with the "static" keyword?

("Class methods" comes to mind as a possible term, but...)
 
J

James Kanze

[...]
That's probably true. I do use "method" to mean "member
function," just for brevity.

Except that not everyone understands it that way.
Decided by whom? I don't see the advantage.

What do you mean, "decided by whom?". It's a language issue;
there isn't any deciding body (at least in English). The
advantages are simple: they don't have the ambiguity of
"method", which is already used in another sense in software
development, and they distiguish between const and non-const.
Isn't that just a Pascalism?

I think it was around long before Pascal. Fortran certainly
distinguishes between the two (although I think is uses the term
subroutine, rather than procedure).
I recall holy wars about the difference between a function and
a procedure from my undergraduate days. I sort of like the
Basic/Perl/etc. terminology, in which all of these things are
just "subroutines."

Conceptually, there's a real difference. Practically, it's
harder to say: in theory, a function returns a value, and
doesn't modify state (doesn't do anything), where as a procedure
(or whatever you want to call it) "does something" (so you
wouldn't want to use it in an expression). In practice, some
things that are conceptually functions will modify state (e.g.
rand()), and its frequent that a procedure which does something
will also want to report whether whatever it was trying to do
succeeded. Which sort of blurrs the distinction at the language
level, or rather means that the distinction at the language
level doesn't correspond to the distinction at the conceptual
level. Still, in practice, it seems safer to make the
distinction between a procedure call and an assignment
statement, rather than just depending on side effects.
 
J

James Kanze

Btw, given that "static" is just a kind of hackish reuse of a
C keyword in C++ class functions, what would you call those
functions?
You have member functions (which might or might not be
virtual), virtual functions (which are specifically
dynamically bound), and... What would be the proper name for
those function with the "static" keyword?

Static member functions. There are three characteristics, so
three words are used. In practice, you don't bother with the
ones where the context makes it clear, or where it doesn't
matter. But on the whole, the terminology is very descriptive,
and very precise, but not necessarily convenient or intuitive.
("Class methods" comes to mind as a possible term, but...)

Java calls them static methods. In the end, all Java does is
replace the word function with the word method, for, as far as I
can see, no good reason---Java's methods are a lot closer to
C++'s (or even C's) functions than they are to Smalltalk's
methods. As I said, doubtlessly a marketing decision: methods
sounds more OO. But you still end up making the distinction in
Java between static methods and non-static methods. And final
methods, and abstract methods---calling a function abstract,
rather than pure virtual, would probably be an improvment in C++
nomenclature, but I don't see anything to be gained by simply
replacing the word function (which is what is used in all of the
reference texts) with method.
 
S

Stefan Ram

James Kanze said:
in theory, a function returns a value, and doesn't modify state
(doesn't do anything), where as a procedure (or whatever you
want to call it) "does something"i

Not, when »function« is used as in ISO/IEC 14882:2003(E).

Otherwise, it depends on the »theory« chosen.
If you do not say, /which/ theory this is based on,
no one can tell whether this statement applies.
In practice, some things that are conceptually functions will
modify state (e.g. rand()),

Again, I fail to see how »rand()« is »conceptually a
function«, but it surely is a function in the sense
of ISO/IEC 14882:2003(E).

If you do not use the word »function« according to ISO/IEC
14882:2003(E), you should specify according to which other
source you use it.
it seems safer to make the distinction between a procedure call
and an assignment statement, rather than just depending on side
effects.

At least, it seems safe to require that for each
function, there is a specification of its behavior,
which includes the value of its call and its effects.

In fact, ISO/IEC 9899:1999 (E) and ISO/IEC 14882:2003(E)
give us just this for all the standard functions of C++.
 
J

Juha Nieminen

James said:
Static member functions.

I'm not sure that's such a great idea.

"Static" sounds a lot like the opposite of "dynamic". A "dynamic
function", in turn, sounds like a dynamically bound function, in other
words, a virtual function. Thus one could deduce that a "static
function" is a member function which is not virtual.

Naturally class functions specified as "static" are indeed not
virtual, but they aren't regular member functions either. They are more
specific than that.

I think in some writings the terms "member function" and "class
function" are used (the latter being, in C++, defined with the "static"
keyword). OTOH "class function" can also be confusing if it has not been
clearly established what it means.
 
J

jason.cipriani

  I'm not sure that's such a great idea.

  "Static" sounds a lot like the opposite of "dynamic". A "dynamic
function", in turn, sounds like a dynamically bound function, in other
words, a virtual function. Thus one could deduce that a "static
function" is a member function which is not virtual.


I'm not so sure. "Static" means unchanging, unmoving. A "static
function" is more accurately a function that does not modify it's own
code, and that remains in the same place (e.g. does not spontaneously
become a member of another class). On the other hand, a "dynamic"
function could modify itself, e.g. a member function of a polymorphic
virus implementation, or perhaps a "dynamic" function is one such as
this:

class A { void function (); };
class B { };

Where at any given moment, possibly when you least expect it, the
compiler is free to make function() be a member of B instead.

In fact, I'd like to propose that in the next C++ standard, that any
member function not explicitly identified as "static" may, at any
given time, become a member of an arbitrary other class in the
program. I think that would remove any remaining ambiguity from this
important issue.


Jason
 
J

James Kanze

I've never seen any confusion on the matter, but I take your
word for it, and rethink the terminology.

Most of the time, there is no real confusion. Human languages
contain a lot of redundance, and context helps. (In practice,
too, most of the people who use the word "method" are OO freaks,
and don't have any non-member functions:).)
Of course they do. To me, a "command" is an object
representing a user request, and "query" is a specification of
information to be obtained from a relational database. I'm
pretty sure "method" is more widely understood than "command"
or "query."

I didn't say that they were perfect. Just better than "method".
It's true that if I hear the word "query", I think data base,
and if I hear the word "command", I think of something entered
at the command line. But neither word comes up otherwise in C++
(or Eiffel, or whatever compiled programming language I'm
using). Where as I use different methods to develop C++ code
all the time. It's all a question of how much the context might
overlap.
So do "const" and "non-const."

With regards to clarity, the "official" C++ terminology is
pretty close to perfect for C++ (where there is no distinction
between functions and procedures, or whatever you want to call
them). With regards to verbosity, "non-const non-virtual member
function" isn't the shortest thing around:). But most of the
time, context will allow dropping some of the adjectives, with
no loss of understanding.

And I wouldn't use "command" and "query" in a C++ community,
because no one would understand what I was talking about.
Similarly, I don't normally complain if someone uses method, as
long as the context makes it clear. Because people understand
the term---whether it was well chosen to begin with or not.

[...]
I had also heard that functions mapped data from one space to
another (i.e. returned values), whereas procedures did not.

That's more or less the "theory" I was thinking about. The
"mathematical" definition.
The only prohibition I've ever heard on side effects, outside
of functional programming is for "predicates." I am firmly of
the opinion that if something has side effects, it should not
be called a predicate.

When I said "in theory", I thought it would be understood: in an
ideal world. I'm well aware that practical considerations
intervene. (And the reason you hear more about the prohibition
on side effects in functional programming is that functional
programmers tend to be more concerned with theory than C++
programmers, who are, judging from those I know, generally
pragmatists.)
I don't know that "just" depending on side effects is any less
clear than assignment.

One rule that I've found useful for ensuring clarity is that a
statement does one, and only one thing. Statements that modify
10 or 15 different variables, or that modify a lot of variables
while also controlling flow, make programs harder to understand.
So having an assignment statement, and a procedure call
statement, makes some sense.

But again, it's a general rule, not something absolute. IMHO, C
goes too far in one direction, Pascal too far in the other, but
on the whole, I'd favor something closer to Pascal than to C.
In particular, given a vector v, v.insert(3) seems much
clearer to me than (say) v += 3 (the string style) or v << 3
(the Qt list style).

I'm totally in agreement there. But v.insert() doesn't have to
be a function (in the Pascal sense); it could be a procedure.

Where the discussion becomes interesting (because there is no
one right answer) is when one introduces the possibilities of
chaining, or something like v.erase( iter ), which returns a
valid iterator so you can continue iterating. The Pascal rule
that you can't ignore a return value doesn't work well with
these sort of functions.
Similarly, I'm fine with assignment statements that have no
immediate side-effects, e.g. those used in expression
templates.

Well, they do have side effects in the compiler (and of course,
they're always constant expressions fully evaluated by the
compiler).

The important point here, however, is that the idioms we use
have grown up within the language, based on the language. Had
the language been different, we'd have developed different
idioms. In the end, you always end up with a complex total
which is more than just the sum of its parts. I find
discussions about "what if" some aspect of the language were
different interesting, abstractly, but in practice, of course,
you can't just change one part and expect everything else to
remain the same, and you can't really know what the effect would
be on everything else.
 
J

James Kanze

I'm not sure that's such a great idea.

I don't really think is was such a good idea, either. But that
was then; that's what they're called in the standard, and in all
of the literature on C++ I've seen, and I think it is too late
to change that now. The influence is strong enough that Java
uses the word static for this as well, and calls them static
methods.

If you want to discuss what they should have been called, then
I'll all for it---I love such discussions. Just don't expect
them to have any real effect---they're just for fun. (See my
discussion with Jeff Schwab elsethread.)
"Static" sounds a lot like the opposite of "dynamic". A
"dynamic function", in turn, sounds like a dynamically bound
function, in other words, a virtual function. Thus one could
deduce that a "static function" is a member function which is
not virtual.
Naturally class functions specified as "static" are indeed not
virtual, but they aren't regular member functions either. They
are more specific than that.

They're very different from other member functions; I'll grant
you that. In reality, they're really free functions with a
particular scope and priviledged access rights.
I think in some writings the terms "member function" and
"class function" are used (the latter being, in C++, defined
with the "static" keyword). OTOH "class function" can also be
confusing if it has not been clearly established what it
means.

A bit like method:).

Seriously, when precision is important, I'd stick with the
nomenclature in the standard; it's more or less formally
specified. Otherwise, whatever will be correctly understood by
other people in context is fine. (But I'm not sure that "class
functions" would be widely understood.)
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top