473,791 Members | 3,122 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

style question

When I write a medium to large sized C application I tend to create
structures first, then makeStruct and freeStruct functions that dynamically
allocate/free the struct and then I create a bunch of functions that operate
on the struct. Example:

typedef struct ccHashTab
{
.....
} ccHashTab;

ccHashTab * ccMakeHash(unsi gned int size, hash h, comp c);
void ccFreeHashTab(c cHashTab *table);
void *ccHashFindSym( ccHashTab *table, void *symbol);
......

This is a bit the object-oriented way, because I never touch the struct
members directly, only inside the hash functions.
Now I'm wondering if this is a style that other C programmers use in their
work or if they're using something different. I just want to know some
opinions, maybe I'll get some new insights.
Nov 13 '05
23 2754

"Serve La" <ik@veranderhet al.com> wrote in message
Of course the burning question, then is, why not use C++ instead of > >
C then?
Huh?
Because OO is not bound to C++

C++ classes are only really useful when you have inheritance relationships
between them. If you are not going to base your design on a class hierarchy,
a C implementation will generally be cleaner and easier to understand and
maintain.
Nov 13 '05 #11
Serve La wrote:
E. Robert Tisdale wrote:
ccHashTab ccHashTab_creat e(unsigned int size, hash h, comp c);

places *no* "burden" on the application program.
It works well no matter how complicated the data structure.

People have been doing this sort of thing for a very long time.
Take a look, for example, at The ANSI C Numerical Class Library

http://www.netwood.net/~edwin/svmtl/

It allows you to construct vector and matrix objects from either
automatic or free storage. You might also look at
The GNU Scientific Library (GSL)

http://sources.redhat.com/gsl/
I can't unzip those.


They are compressed UNIX tape archives.
You can probably use Stuffit

http://www.stuffit.com/win/expander/index.html

to uncompress and extract the directory.
Could you give an example?

Here's a fairly complicated struct that I now allocate in a function:

typedef struct Inner {
ITypeInfo *info;
TYPEATTR *attr;
BSTR name;
int index;
} Inner;

typedef struct SomeStruct {
GUID libid;
ITypeLib *lib;
Inner *info;
UINT ntypeinfo;
BSTR filename;
BSTR helpstring;
BSTR helpfilename;
UINT currentiter;
} SomeStruct;

#ifdef NCL_REVEAL
/* Reveal type definitions after applications are thoroughly tested. */

typedef struct { /* submatrix class definition */
/* private: */
ncl_dchandle H;
ncl_offset O;
ncl_extent N1;
ncl_stride S1;
ncl_extent N2;
ncl_stride S2;
ncl_______ _;
} ncl_dcsubmatrix ;
/* A submatrix does not own the array storage that it references. */
/* It does not allocate any array storage when it is constructed */
/* nor does it deallocate any array storage when it is destroyed. */

#else /* NCL_REVEAL */
/* Conceal type definitions until applications are thoroughly tested. */

#define NCL_DCSUBMATRIX _SIZE NCL_SUBMATRIX_S IZE
typedef int ncl_dchidden_su bmatrix[NCL_DCSUBMATRIX _SIZE/sizeof(int)];

typedef struct { /* submatrix class definition */
/* private: */
ncl_dchidden_su bmatrix M;
} ncl_dcsubmatrix ;
/* A submatrix does not own the array storage that it references. */
/* It does not allocate any array storage when it is constructed */
/* nor does it deallocate any array storage when it is destroyed. */

#endif /* NCL_REVEAL */

inline static
ncl_dcsubmatrix /* automatic storage constructor */
(ncl_dcsubm_cre ate)(
ncl_dchandle h, ncl_offset o,
ncl_extent m, ncl_stride s2,
ncl_extent n, ncl_stride s1) {
ncl_dcsubmatrix M;
ncl_dcsubm_init (&M, h, o, m, s2, n, s1);
return M; }

inline static
void /* automatic storage destructor */
(ncl_dcsubm_des troy)(ncl_dcsub matrix* pM) {
pM->_ = ~ncl_other; }

inline static
ncl_dcsubmatrix * /* free storage constructor */
(ncl_dcsubm_new )(
ncl_dchandle h, ncl_offset o,
ncl_extent m, ncl_stride s2,
ncl_extent n, ncl_stride s1) {
ncl_dcsubmatrix * pM
= (ncl_dcsubmatri x*)malloc(sizeo f(ncl_dcsubmatr ix));
if (pM)
ncl_dcsubm_init (pM, h, o, m, s2, n, s1);
return pM; }

inline static
void /* free storage destructor */
(ncl_dcsubm_del ete)(ncl_dcsubm atrix* pM) {
ncl_dcsubm_dest roy(pM);
free((void*)pM) ; }

Nov 13 '05 #12
On Fri, 12 Sep 2003 22:40:33 +0200, "Serve La" <ik@veranderhet al.com>
wrote in comp.lang.c:
When I write a medium to large sized C application I tend to create
structures first, then makeStruct and freeStruct functions that dynamically
allocate/free the struct and then I create a bunch of functions that operate
on the struct. Example:

typedef struct ccHashTab
{
....
} ccHashTab;


One style comment. I personally don't like the practice of using a
struct tag and a typedef for the same structure. One or the other,
please, but not both.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #13
Jack Klein wrote:
Serve La wrote:
When I write a medium to large sized C application,
I tend to create structures first then makeStruct and freeStruct
functions that dynamically allocate/free the struct and then
I create a bunch of functions that operate on the struct. Example:

typedef struct ccHashTab {
// ...
} ccHashTab;


One style comment.
I personally don't like the practice
of using a struct tag and a typedef for the same structure.
One or the other, please, but not both.


Can you give us a clue why you don't like it?

Nov 13 '05 #14
Jack Klein wrote:
On Fri, 12 Sep 2003 22:40:33 +0200, "Serve La" <ik@veranderhet al.com>
wrote in comp.lang.c:
When I write a medium to large sized C application I tend to create
structures first, then makeStruct and freeStruct functions that
dynamically allocate/free the struct and then I create a bunch of
functions that operate on the struct. Example:

typedef struct ccHashTab
{
....
} ccHashTab;


One style comment. I personally don't like the practice of using a
struct tag and a typedef for the same structure. One or the other,
please, but not both.


Hmmm, disagreeing with Jack Klein for a second consecutive article. This is
getting worrying.

Jack -- as you say, it's a style point. I use structure tags for all structs
nowadays, partly in the usual cluon economy drive, so that I don't have to
remember when I need a tag and when I don't, but also to reduce by a tiny
amount the maintenance work involved in making a struct self-referential,
at the expense of a slightly larger amount of work up front!

On the other hand, I like typedefs. Again, I use them for all structs
nowadays. I like the idea of being able to abstract away the structness of
a struct. I am of the opinion that it makes my programs more pleasant to
read, for many people at least, and perhaps even for most people.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #15

"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote in message
news:bk******** **@newsg4.svr.p ol.co.uk...

"Serve La" <ik@veranderhet al.com> wrote in message
Of course the burning question, then is, why not use C++ instead of >

C then?

Huh?
Because OO is not bound to C++

C++ classes are only really useful when you have inheritance relationships
between them. If you are not going to base your design on a class

hierarchy, a C implementation will generally be cleaner and easier to understand and
maintain.


I agree with that and I'll even add inheriting to the list. Only if you want
to override functions then C is not easy to use anymore. Handling virtual
function tables yourself is not easy and maintainable anymore.
Nov 13 '05 #16

"Jack Klein" <ja*******@spam cop.net> wrote in message
news:im******** *************** *********@4ax.c om...
typedef struct ccHashTab
{
....
} ccHashTab;


One style comment. I personally don't like the practice of using a
struct tag and a typedef for the same structure. One or the other,
please, but not both.


I was expecting that somebody would say that :)
But I have chosen this style because I don't see any problems with it only
advantages.

Why was C designed this way? That you have to declare a struct as a struct,
enum as an enum.....
Nov 13 '05 #17
On Fri, 12 Sep 2003 20:40:33 UTC, "Serve La" <ik@veranderhet al.com>
wrote:
When I write a medium to large sized C application I tend to create
structures first, then makeStruct and freeStruct functions that dynamically
allocate/free the struct and then I create a bunch of functions that operate
on the struct. Example:


That's the way I work since the first days of programming I've ever
done (and that was long before C were existent.
With C there is more help to encapsulate things. Think OO, but write
procedual.

1. a header file that holds the external interfaces.
- an abstract type name
- function prototypes
2.. a translation unit that holds the data descriptions and the
functions used to get anything done.

--
Tschau/Bye
Herbert

eComStation 1.1 Deutsch Beta ist verügbar
Nov 13 '05 #18

"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:3F******** ******@jpl.nasa .gov...
typedef struct { /* submatrix class definition */
/* private: */
ncl_dchandle H;
ncl_offset O;
ncl_extent N1;
ncl_stride S1;
ncl_extent N2;
ncl_stride S2;
ncl_______ _;
} ncl_dcsubmatrix ;


Ah, ok, but this struct has only valuetypes that can be easily copied. What
you said was "It works well no matter how complicated the data structure"
What if a struct contains pointers or other structs that contain pointers?
Nov 13 '05 #19

On Sun, 14 Sep 2003, Serve La wrote:

"Jack Klein" <ja*******@spam cop.net> wrote...
typedef struct ccHashTab
{
....
} ccHashTab;
One style comment. I personally don't like the practice of using a
struct tag and a typedef for the same structure. One or the other,
please, but not both.


I was expecting that somebody would say that :)
But I have chosen this style because I don't see any problems
with it only advantages.


(Personally, I don't often use typedefs. But in the rare case I do,
I try to give the struct tag and the typedef-name *different*
identifiers; perhaps 'typedef struct Foo_tag { } Foo;', for example.
It's less confusing than having the same identifier mean different
things in different namespaces, IMHO.)

Why was C designed this way? That you have to declare a struct
as a struct, enum as an enum.....


I do believe you've just answered your own question. :)

-Arthur

Nov 13 '05 #20

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

Similar topics

12
3835
by: David MacQuigg | last post by:
I have what looks like a bug trying to generate new style classes with a factory function. class Animal(object): pass class Mammal(Animal): pass def newAnimal(bases=(Animal,), dict={}): class C(object): pass C.__bases__ = bases dict = 0
15
1734
by: Christopher Benson-Manica | last post by:
If you had an unsigned int that needed to be cast to a const myClass*, would you use const myClass* a=reinterpret_cast<const myClass*>(my_val); or const myClass* a=(const myClass*)myVal; ?
33
2509
by: amerar | last post by:
Hi All, I can make a page using a style sheet, no problem there. However, if I make an email and send it out to my list, Yahoo & Hotmail totally ignore the style tags. It looks fine in Netscape though..... Question: I've tried linking & embedding the style tags with no luck. How can I use them inline? I've read that inline style sheets is the way to go if you want them to work in most email clients.......
1
1411
by: amerar | last post by:
Hi All, I posted a question about style sheets, and why certain email clients were ignoring them. Someone suggested placing them inline. I did this and get better results, but not what I wanted. The page still appears properly, and it shows in Netscape Messenger just fine, but on Hotmail and Yahoo, each <DIV> tag does not appear where it is supposed to appear.
4
6601
by: KvS | last post by:
Hi all, I'm pretty new to (wx)Python so plz. don't shoot me if I've missed something obvious ;). I have a panel inside a frame, on which a Button and a StaticText is placed: self.panel = wx.Panel(self,-1) self.button = wx.Button(self.panel,-1,"Klikkerdeklik") self.button.SetPosition((200,40)) self.Bind(wx.EVT_BUTTON, self.VeranderLabel, self.button)
83
15630
by: rahul8143 | last post by:
hello, what is difference between sizeof("abcd") and strlen("abcd")? why both functions gives different output when applied to same string "abcd". I tried following example for that. #include <stdio.h> #include <string.h> void main() { char *str1="abcd";
39
2222
by: jamilur_rahman | last post by:
What is the BIG difference between checking the "if(expression)" in A and B ? I'm used to with style A, "if(0==a)", but my peer reviewer likes style B, how can I defend myself to stay with style A ? style A: .... .... int a = 1; if(0==a) {
18
2126
by: pocmatos | last post by:
Hi all, While I was programming 5 minutes ago a recurring issue came up and this time I'd like to hear some opinions on style. Although they are usually personal I do think that in this case as also to do with making the code easier to read. Imagine a function returning void (for example) and it's body is a big if with lots of special cases:
3
1957
Claus Mygind
by: Claus Mygind | last post by:
I want to move some style setting into a javaScript function so I can make them variable but I get "invalid assignment left-hand side" error? see my question at the bottom of this message. It works fine when I stream it out in my html page like this: <DIV ID="popSearch" STYLE=" position:absolute; visibility:hidden;
6
1996
by: MatthewS | last post by:
I've seen the question raised several times here, but apparently never answered. Since PyInstance_Check returns False for new-style class instances, is there a standard procedure for testing this using the C- Api? I would greatly appreciate some help with this. /Matthew
0
9512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10419
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, 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...
0
10201
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
10147
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
9023
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
7531
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...
1
4100
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3709
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2910
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.