473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

opaque types without malloc?

I'd like to have something functionally equivalent to, say,
void make_it_happen(void)
{
struct T t;
for(start(&t); !done(&t); doit(&t));
}
The function has no business knowing what's inside struct T; it just
provides a "holder". Ideally, I'd say e.g.
struct T;
to pass an incomplete type, but that's not enough for instantiation of t.
A dynamic-memory-allocation-inspired scheme like
void make_it_happen(void)
{
struct T *p = start_newT();
for(; !done(p); doit(p));
free(p);
}
is not an option on a small (embedded) target, not to mention the overhead.

Is there a clever way not to drag around the complete definition of
struct T but still be able to create a holder and point to it as to
struct T* ?

Likewise, is there a clever compile-time constant equivalent to
sizeof(struct T), all without exposing complete definition.

[I suspect both are silly questions, but I'd take my chances.]

Thank you,
--
Ark
Sep 14 '07 #1
3 2168
Ark Khasin said:

<snip>
Is there a clever way not to drag around the complete definition of
struct T but still be able to create a holder and point to it as to
struct T* ?
Not as far as I'm aware, but there may be an easy way.

Your problem seems to be that you can't afford malloc. Well, you *have*
to be able to afford the object itself, so why not make it a static
object in the module that knows about struct Ts:

static struct T t;

struct T *get_T(void)
{
return &t;
}

Or, if you feel so inclined:

static struct T t[MAX_T];

with all the gubbins involved in providing controlled access to the
members of that array.
Likewise, is there a clever compile-time constant equivalent to
sizeof(struct T), all without exposing complete definition.
Not as far as I know.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 14 '07 #2
Ark Khasin wrote:
>
.... snip ...
>
Is there a clever way not to drag around the complete definition
of struct T but still be able to create a holder and point to it
as to struct T* ?
It's not especially clever, but it works. Use pointers, and let
the module that knows about the struct T do the mallocing. For an
example, see hashlib.zip, and the way of assigning the basic hash
table in hshinit(), at <http://cbfalconer.home.att.net/download/>

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 14 '07 #3
In article <eRpGi.4368$Z33.1218@trndny08>,
Ark Khasin <ak*****@macroexpressions.comwrote:
>I'd like to have something functionally equivalent to, say,
void make_it_happen(void)
{
struct T t;
for(start(&t); !done(&t); doit(&t));
}
The function has no business knowing what's inside struct T; it just
provides a "holder". Ideally, I'd say e.g.
struct T;
to pass an incomplete type, but that's not enough for instantiation of t.
It can't be an incomplete type, or you wouldn't be able to declare an
instance of it. Perhaps we can arrange for a complete type that is
nonetheless useless. So you need a way to declare something whose
memory is suitable for storing a struct T in. It needs to be the
right size, and have the right alignment.

Just declaring a struct containing char dummy[N], where N is the size
of the real struct, won't necessarily work because of the alignment.
Is a structure guaranteed to have the alignment of its first member?
(That's not a rhetorical question.) If so, you could declare the
"opaque" type as having a first member the same type as the real one,
and the right number of padding bytes after it.

Of course you'll have to count the determine the size by hand for each
platform if you don't want the "real" type to be visible, as it would
have to be to use it with sizeof().

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 14 '07 #4

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

Similar topics

19
by: Capstar | last post by:
Hi NG, I've read some time ago in this NG, that when you're writing a library it can be good practice to use an opaque type to point to your structures if the user doesn't need to modify the...
38
by: Capstar | last post by:
Hi NG, I've read some time ago in this NG, that when you're writing a library it can be good practice to use an opaque type to point to your structures if the user doesn't need to modify the...
18
by: chankl | last post by:
Can anyone explain what's an opaque pointer and how it's implemented in C? I read about this concept in the book "C interfaces and implementations". Here's an example from the book (list.h -...
1
by: bantunks | last post by:
Hello, I am trying to figure out the advantages and disadvantages of exposing interfaces through Opaque data types in C. I have figured/found out the following two advantages 1. Higher level of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.