Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C function pointers and UB

Reply
Thread Tools

C function pointers and UB

 
 
Keith Thompson
Guest
Posts: n/a
 
      08-23-2011
Niklas Holsti <(E-Mail Removed)> writes:
[...]
> I'm not sure if different objects of a null record type are required to
> have different addresses in Ada. I have not needed that.


<OT>
I don't think that's required. Experimentally, two declared null record
objects have distinct addresses, but array elements have the same
address. (Note that Ada doesn't define array indexing in terms of
addresses.)
</OT>

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-23-2011
James Kuyper <(E-Mail Removed)> writes:
<snip>
> [...] In C, this is
> completely unnecessary, because unlike C++, it allows implicit
> conversion of Object* to void*.


C++ allows that implicit conversion too -- it's the reverse that is not
done implicitly.

<snip>
--
Ben.
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      08-23-2011
On 08/23/2011 06:02 PM, Ben Bacarisse wrote:
> James Kuyper <(E-Mail Removed)> writes:
> <snip>
>> [...] In C, this is
>> completely unnecessary, because unlike C++, it allows implicit
>> conversion of Object* to void*.

>
> C++ allows that implicit conversion too -- it's the reverse that is not
> done implicitly.


You're right - which makes this whole "deleter" typedef even more puzzling.
--
James Kuyper
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      08-24-2011
On 8/23/2011 12:48 PM, nroberts wrote:
> I'm from C++ and having to write some C. Could someone tell me if the
> following would illicit UB? [...]


Others have commented on the specifics of your question, and
even on some of its far-fetched generalities, but no one (as far
as I've seen) has yet mentioned the other issue: "Illicit" doesn't
mean what you think it means. It's illegal. Try "elicit."

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      08-24-2011
On Tue, 23 Aug 2011 09:48:39 -0700, nroberts wrote:

> I'm from C++ and having to write some C. Could someone tell me if the
> following would illicit UB? I have a feeling that it will work OK but
> I hate generating UB without knowing it.


It would work on "common" platforms, but it's not guaranteed by the
standard.

For compatibility, you would need to either change the type of freeObject
to take a void*:

void freeObject(void* obj) { free(obj); }

or wrap it with a function that does:

void objectDeleter(Object* obj) { freeObject(obj); }

BTW: providing a dedicated "free" function is a wise move even if it
simply calls free(), as it ensures that the *correct* free() is used.

This is a very real issue on Windows, as a process can end up using
multiple distinct versions of MSVCRT, e.g. due to the program linking
against one version and also against a DLL which is linked against a
different version.

Anything allocated by one version's malloc() must be freed using the same
version's free(). To ensure that this happens, a library which returns
pointers to dynamically allocated memory must provide its own deallocation
routine(s). If the main program simply calls free(), it may well end up
calling the wrong free().

 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Smart pointers and member function pointers n2xssvv g02gfr12930 C++ 3 11-27-2005 10:51 AM
void pointers & void function pointers Peter Goddard C Programming 3 05-16-2005 09:44 PM
Pointers and References (and References to Pointers) Roger Leigh C++ 8 11-17-2003 10:14 AM
Template specialization of pointers with function pointers Phil C++ 1 09-16-2003 02:17 AM



Advertisments