Let's say we have a library with an opaque data type implemented as a
struct in the library's C source file. The definition of the struct
isn't duplicated in the header included by the programs that make use of
this library. Is it legal for the header and the programs that include
it to declare pointers to this struct for which no definition is in
scope and to pass those pointers into functions declared in that header? 8 2776
Chris Barts <ch************ @gmail.com> wrote: Let's say we have a library with an opaque data type implemented as a struct in the library's C source file. The definition of the struct isn't duplicated in the header included by the programs that make use of this library. Is it legal for the header and the programs that include it to declare pointers to this struct for which no definition is in scope and to pass those pointers into functions declared in that header?
In short, yes.
But you cannot initialise these pointers, except to null; you cannot
assign values to them unless that value is passed to you from a function
which does have a declaration for the struct type in scope; and you
cannot dereference those pointers. For an opaque type, of course, this
is precisely what you want.
Richard
"Chris Barts" <ch************ @gmail.com> wrote in message
news:ZN******** ************@on ewest.net... Let's say we have a library with an opaque data type implemented as a struct in the library's C source file. The definition of the struct isn't duplicated in the header included by the programs that make use of this library. Is it legal for the header and the programs that include it to declare pointers to this struct for which no definition is in scope and to pass those pointers into functions declared in that header?
Yes, it's perfectly legal and IMO often quite useful, since only changes in
the interface require users of the interface to be recompiled, and even
better, all changes to the members of the struct are made inside the
library. The only downsides are that you can't use macros for (typically)
quicker access to struct members, and in most cases dynamic allocation is
required, so performance may suffer slightly.
Alex
Richard Bos wrote: Chris Barts <ch************ @gmail.com> wrote:
[partial snip]Is it legal for the header and the programs that include it to declare pointers to this struct for which no definition is in scope and to pass those pointers into functions declared in that header?
In short, yes.
That's good to know.
But you cannot initialise these pointers, except to null; you cannot assign values to them unless that value is passed to you from a function which does have a declaration for the struct type in scope; and you cannot dereference those pointers.
And all of those limits are precisely what I'd expect: Unless the
definition is in scope, there is no way to know the names of the
members, let alone their offsets into the object.
For an opaque type, of course, this is precisely what you want.
Very true.
Chris Barts wrote: Let's say we have a library with an opaque data type implemented as a struct in the library's C source file. The definition of the struct isn't duplicated in the header included by the programs that make use of this library. Is it legal for the header and the programs that include it to declare pointers to this struct for which no definition is in scope and to pass those pointers into functions declared in that header?
Yes.
--
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!
Alex Fraser wrote: "Chris Barts" <ch************ @gmail.com> wrote in message news:ZN******** ************@on ewest.net...
[snip] Yes, it's perfectly legal and IMO often quite useful, since only changes in the interface require users of the interface to be recompiled, and even better, all changes to the members of the struct are made inside the library.
This was precisely my thinking: It enforces complete locality of
everything within the opaque type, preventing programmers, and therefore
the library's author, from becoming wedded to implementation details.
I'm a pretty strong believer in that sort of thing.
The only downsides are that you can't use macros for (typically) quicker access to struct members, and in most cases dynamic allocation is required, so performance may suffer slightly.
The key word here, I believe, is 'slightly': Given the advantages stated
above, it would be rare (hopefully) that the performance edge of the
less-safe case is a deciding factor in how the library is implemented.
[semi-OT]Plus, passing a pointer into a function is typically a cheap
operation, so if the library itself is reasonably optimized the problem
is essentially solved.[/semi-OT]
"Chris Barts" <ch************ @gmail.com> wrote in message
news:75******** ************@on ewest.net... Alex Fraser wrote: The only downsides are that you can't use macros for (typically) quicker access to struct members, and in most cases dynamic allocation is required, so performance may suffer slightly.
The key word here, I believe, is 'slightly': Given the advantages stated above, it would be rare (hopefully) that the performance edge of the less-safe case is a deciding factor in how the library is implemented.
The getc() function/macro is an obvious example of the trade-off. Most of
the time, it doesn't do very much, so if implemented as a function, the
overall overhead from the function calls can be a significant fraction of
the total time.
The performance cost of dynamic allocation/deallocation can usually be
minimised, and is less likely to be significant anyway. One thing I didn't
mention is that dynamic allocation needs explicit deallocation, which is
sometimes inconvenient. But some sort of cleanup besides deallocating the
structure is often needed anyway, so it's a bit of a moot point - the
inconvenience often exists either way.
Alex
On Wed, 12 Jan 2005 08:30:49 -0700, Chris Barts
<ch************ @gmail.com> wrote: Alex Fraser wrote: "Chris Barts" <ch************ @gmail.com> wrote in message news:ZN******** ************@on ewest.net... [snip: using pointers to struct type encapsulated in module] Yes, it's perfectly legal and IMO often quite useful, since only changes in the interface require users of the interface to be recompiled, and even better, all changes to the members of the struct are made inside the library.
This was precisely my thinking: It enforces complete locality of everything within the opaque type, preventing programmers, and therefore the library's author, from becoming wedded to implementation details.
I'm a pretty strong believer in that sort of thing.
One warning: s/preventing/discouraging/ . Some people will still poke
around in your source, or hex-dump the data, or disassemble your code,
and find and use things you wanted to keep hidden. And some people
will occasionally perhaps even accidentally do "dirty" things that
cause dependencies -- like, say, memcpy'ing 32 bytes to a savearea and
later memcpy'ing it back and assuming that's enough. (I speak from
experience on both sides of this fence.)
Using 'struct hidden *' is a very good start, but _also_ document
clearly that you intend the type to be opaque -- and hope that you
either persuade users you are right to do so, or are able to ignore
the anguished screams that do arise.
- David.Thompson1 at worldnet.att.ne t
Dave Thompson wrote: On Wed, 12 Jan 2005 08:30:49 -0700, Chris Barts <ch************ @gmail.com> wrote:
Alex Fraser wrote:
"Chris Barts" <ch************ @gmail.com> wrote in message news:ZN***** *************** @onewest.net...
[snip: using pointers to struct type encapsulated in module]
Yes, it's perfectly legal and IMO often quite useful, since only changes in the interface require users of the interface to be recompiled, and even better, all changes to the members of the struct are made inside the library.
This was precisely my thinking: It enforces complete locality of everything within the opaque type, preventing programmers, and therefore the library's author, from becoming wedded to implementation details.
I'm a pretty strong believer in that sort of thing.
One warning: s/preventing/discouraging/ . Some people will still poke around in your source, or hex-dump the data, or disassemble your code, and find and use things you wanted to keep hidden. And some people will occasionally perhaps even accidentally do "dirty" things that cause dependencies -- like, say, memcpy'ing 32 bytes to a savearea and later memcpy'ing it back and assuming that's enough. (I speak from experience on both sides of this fence.)
People will always find ways around if they really want to. One just has
to try to provide functions to do all the things people are likely to
want to do, such as copying the structure if that is a sensible thing to
allow.
Using 'struct hidden *' is a very good start, but _also_ document clearly that you intend the type to be opaque -- and hope that you either persuade users you are right to do so, or are able to ignore the anguished screams that do arise.
Also state explicitly that anything bypassing your interface *will* be
broken by subsequent releases.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dave |
last post by:
Hello all,
In the code below, I use a pointer to an object under construction. Is the
usage below legal? I have come across similar code at work. It compiles,
but I'm not sure it's really legal...
Thanks,
Dave
struct D;
|
by: Arun Prasath |
last post by:
Hi all,
I have the following question regd pointer typecasting. Is the
following type of pointer typecasting valid?
#define ALLOC(type,num) ((type *)malloc(sizeof(type)*num))
/*begin code*/
|
by: Kieran Simkin |
last post by:
Hi,
I wonder if anyone can help me, I've been headscratching for a few hours
over this.
Basically, I've defined a struct called cache_object:
struct cache_object {
char hostname;
char ipaddr;
};
|
by: Sushil |
last post by:
Hi Gurus
I've tried to come up with a small logical example of my problem.
The problem is platform specific (MIPS) which I understand should not
be discussed here.
So here goes my example:
Code is doing malloc of variable sizes.
The last byte of malloc.ed memory is written a magic.
|
by: Alexei A. Frounze |
last post by:
Hi all,
I have a question regarding the gcc behavior (gcc version 3.3.4).
On the following test program it emits a warning:
#include <stdio.h>
int aInt2 = {0,1,2,4,9,16};
int aInt3 = {0,1,2,4,9};
| |
by: Johs32 |
last post by:
I have a struct "my_struct" and a function that as argument takes a pointer
to this struct:
struct my_struct{
struct my_struct *new;
};
void my_func(struct my_struct *new);
I have read that there is no difference between giving this function a
|
by: gcary |
last post by:
I am having trouble figuring out how to declare a pointer to an array
of structures and initializing the pointer with a value. I've looked
at older posts in this group, and tried a solution that looked
sensible, but it didn't work right. Here is a simple example of what
I'm trying to accomplish:
// I have a hardware peripheral that I'm trying to access
// that has two ports. Each port has 10 sequential
// registers. Create a...
|
by: Mike |
last post by:
Hi,
I am new to C and having problems with the following program.
Basically I am trying to read some files, loading data structures into
memory for latter searching.
I am trying to use structres and arrays of pointers to them.
I have gotten the program to compile with gcc on WinXP.
If the file i read doesnt have alot of records, it runs thru. But once
i add more, it dies. In this program i have 4 files setup to read. The
|
by: ramu |
last post by:
Hi,
Can you please let me know the output of the below code.
Regards
#include<stdio.h>
int main()
{
int *ptr=0;
ptr++;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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();...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |