473,386 Members | 1,830 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,386 software developers and data experts.

Optional elements in a structure declaration? ( URGENT)

Hi,
Consider a structure as follows :

struct dummy
{

int a;
int b;
int c;
};

My program has two types of applications - one that uses all 3
elements of the structure ( a,b and c) and the other application which
uses only elements a and b of the same structure.

My question is, is there any way to use the same structure for both
situations such that depending on some flag, I can choose to allocate
memory only for elements a and b ( type 2) or for all 3 elements ( type
1). I do not want to use two different structures for the same.

Thanks,

Mohd. Imaduddin

Mar 6 '06 #1
14 8590

zoltan wrote:
Hi,
Consider a structure as follows :

struct dummy
{

int a;
int b;
int c;
};

My program has two types of applications - one that uses all 3
elements of the structure ( a,b and c) and the other application which
uses only elements a and b of the same structure.

My question is, is there any way to use the same structure for both
situations such that depending on some flag, I can choose to allocate
memory only for elements a and b ( type 2) or for all 3 elements ( type
1). I do not want to use two different structures for the same.


Simple answer: use the same structure (as above), and just ignore the
third element.

If this is not good enough for your application, then let us know
exactly why. If your structure consists of more than just `int`s say so
as well. Without details it's more difficult to offer options.

One other option: If your data is more complex than an `int`, why not
make structure members pointers to data, and only allocate memory as
needed. You'd still carry three pointers around in both applications,
but see also:

OTH, if your two applications are independent, why not use something
like:

struct dummy
{

int a;
int b;
#ifdef APP_1
int c;
#endif
};

And #define APP_1 in the first, but not in the second.

--
BR, Vladimir

Mar 6 '06 #2
Thanks very much, but I already considered both approaches.

The thing is I will be using the structure for parsing a compressed
file. If the compressed data is of say TYPE A I shall use only two
elements. If it is of say TYPE B, I will be using all the three. For
reasons of efficiency, I don't want to use the third element. And I
will be using array of the same structure for storing the information.
So using #ifdef is out of question, since I will have to declare the
array in the beginning itself. Also, by using pointers I will still
have a third dummy variable which again is not needed for TYPE A.

Mar 6 '06 #3
Imad wrote:

Thanks very much, but I already considered both approaches.

The thing is I will be using the structure for parsing a compressed
file. If the compressed data is of say TYPE A I shall use only two
elements. If it is of say TYPE B,


Why only one type of data structure for two kinds of file data?

--
pete
Mar 6 '06 #4
Since it will be easier to manage, also both kinds of data will be
present in the same file, so it will be more convenient to view any
kind of data about the file since everything is present in a single
structure.

Mar 6 '06 #5
Imad wrote:
Thanks very much, but I already considered both approaches.

The thing is I will be using the structure for parsing a compressed
file. If the compressed data is of say TYPE A I shall use only two
elements. If it is of say TYPE B, I will be using all the three. For
reasons of efficiency, I don't want to use the third element. And I
will be using array of the same structure for storing the information.
So using #ifdef is out of question, since I will have to declare the
array in the beginning itself. Also, by using pointers I will still
have a third dummy variable which again is not needed for TYPE A.


Please quote what and who you're replying to. Otherwise, your posts may
make little to no sense for many people. Please, read
<http://cfaj.freeshell.org/google/> before posting again.

As for your problem, as you describe it, and the constraints, I'm
afraid there's no way to do that in C, AFAIK. Others may know better
though (but may not bother unless you follow posting guidelines above).

As an aside: if you have so much data that carrying and extra pointer
around is out of question, declaring an array to hold it may not be
good practice either. If you don't know how much data you'll get, an
array of static size may prove wastful in memory terms, possibly even
more than an extra pointer per element. If you do know how much data
you'll get, and you know you'll have enough memory for your array, then
you may be able to calculate with certainty whether you can afford an
extra element or not.

Mar 6 '06 #6
zoltan wrote:

Hi,
Consider a structure as follows :

struct dummy
{

int a;
int b;
int c;
};

My program has two types of applications - one that uses all 3
elements of the structure ( a,b and c) and the other application which
uses only elements a and b of the same structure.

My question is, is there any way to use the same structure for both
situations such that depending on some flag, I can choose to allocate
memory only for elements a and b ( type 2)
or for all 3 elements ( type
1). I do not want to use two different structures for the same.


A struct with all same type elements
can sometimes be replaced by an array.

int *dummy;

dummy = malloc(flag * sizeof *dummy); /* flag is 2 or 3 */

dummy[0]
dummy[1]

--
pete
Mar 6 '06 #7
zoltan wrote:
My question is, is there any way to use the same structure for both
situations such that depending on some flag, I can choose to allocate
memory only for elements a and b ( type 2) or for all 3 elements ( type
1). I do not want to use two different structures for the same.


Sorry but my only suggestion is three structures! Rough pseudo-code:

enum struct_type { twoValue, threeValue };

typedef struct {
int a;
int b;
} twoValue_s;

typedef struct {
int a;
int b;
int c;
} threeValue_s;

typedef struct {
enum struct_type struct_e;
void *ptr;
} fileValues;

fileValues myVals[];

then when you know how many values you have, you could do something
like:

myVals[0].struct_e = twoValue;
myVals[0].ptr = malloc( sizeof twoValue_s );

or:

myVals[0].struct_e = threeValue;
myVals[0].ptr = malloc( sizeof threeValue_s );

The struct_e tag tells you how to cast the void *.

switch( myVals[42].struct_e )
{
case twoValue:
func( (twoValue_s*)myVals[42].ptr );
break;
case threeValue:
func( (threeValue_s*)myVals[42].ptr );
break;
}

I know this is terrible, I don't have time to check the code, just
wanted to throw the idea into this discussion. What do people think of
this approach?

Mar 6 '06 #8
zoltan wrote:
Consider a structure as follows :

struct dummy
{

int a;
int b;
int c;
};

My program has two types of applications - one that uses all 3
elements of the structure ( a,b and c) and the other application which
uses only elements a and b of the same structure.

My question is, is there any way to use the same structure for both
situations such that depending on some flag, I can choose to allocate
memory only for elements a and b ( type 2) or for all 3 elements ( type
1). I do not want to use two different structures for the same.


you could play some nasty tricks with malloc()

[untested code fragment]
typedef struct
{
int type;
int stuff;
int more_stuff;
} File;
typedef struct
{
int type;
int stuff;
} Small_file;
File* create_file()
{
File* f

if ((f = malloc(sizeof File)) == NULL)
abort();

f->type = BIG_FILE;

return f;
}

File* create_small_file()
{
File* f;

if ((f = (File*)malloc(sizeof Small_file)) == NULL)
abort();

f->type = SMALL_FILE;

return f;
}
[end code]

but perhaps I shouldn't encourage this?
--
Nick Keighley

Mar 6 '06 #9

"Imad" <md*****@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
Thanks very much, but I already considered both approaches.

The thing is I will be using the structure for parsing a compressed
file. If the compressed data is of say TYPE A I shall use only two
elements. If it is of say TYPE B, I will be using all the three. For
reasons of efficiency, I don't want to use the third element. And I
will be using array of the same structure for storing the information.
So using #ifdef is out of question, since I will have to declare the
array in the beginning itself. Also, by using pointers I will still
have a third dummy variable which again is not needed for TYPE A.

If you've got an array of structs, you have to assume that the size of the
struct is wired into the pointer arithmetic (subscripting) code at compile
time. The only variable-size object you can do pointer arithmetic with is
the new variable-length array in C99. If you can't use those, just malloc a
block of ints and calculate your way around. It needn't be very messy. For
instance

const int a = 0;
const int b = 1;
const int c = 2;
const int d = 3;
int width = c; /* c or d as required */

Then replace
struct dummy *array = malloc (n * sizeof *array)
struct dummy *ps = array
with
int *array = malloc (n * width * sizeof *array)
int *pi = array
and
++ps
with
pi += width
and
ps->a
ps->b
ps->c
with
pi[a]
pi[b]
pi[c]
etc. It only gets messy if you need to replace explicit subscripts, e.g.
ps[i].a

If you can't afford any wasted struct members, you can't really use structs
at all anyway. They might be padded and come out bigger than you expect.

--
RSH


Mar 6 '06 #10
imad wrote
(in article
<11**********************@v46g2000cwv.googlegroups .com>):

Note to Google: I hate you.

Back to our regularly scheduled thread... please learn how to
use the google interface to include context.
Since it will be easier to manage, also both kinds of data will be
present in the same file, so it will be more convenient to view any
kind of data about the file since everything is present in a single
structure.
If it actually was easier, then you wouldn't be asking these
questions. "Less code" and "easier" are not the same thing
necessarily.

You said upthread: The thing is I will be using the structure for parsing a compressed
file. If the compressed data is of say TYPE A I shall use only two
elements. If it is of say TYPE B, I will be using all the three. For
reasons of efficiency, I don't want to use the third element. And I
will be using array of the same structure for storing the information. So using #ifdef is out of question, since I will have to declare the
array in the beginning itself. Also, by using pointers I will still
have a third dummy variable which again is not needed for TYPE A.


This sounds like some poorly thought out design decisions. If
you have two different types of file data, trying to mash them
into one may not be the best answer. If you really want to
carry both types around in the same containers, then you need a
conditional structure with a flag value to indicate if the "TYPE
B" data is present.

Going back to your original,

#define TYPE_A 0x00
#define TYPE_B 0x01

struct dummy
{
int a;
int b;
int c; /* valid only for TYPE B */
int my_type; /* indicates validity of c entry */
};

When you read in the data, you determine which TYPE it is, set
the appropriate values in the struct, and copy it into your
array. The problem is, every time you use the data, you have to
check/set the my_type entry. That is the cost of smashing two
different TYPES (not C types) into one container. If you reuse
array entries over time, you'll also have to make sure that the
my_type field is updated properly as needed.

On the other hand, if you know for a fact that values for 'c'
have a limited number of possible values, you might be able to
simplify it a bit by using a reserved value (that can not occur
in your data set) for c. This would allow you to set c =
MAGIC_NUMBER in the cases of a TYPE_A data read and eliminate
the extra struct entry, but you still have to manage it
carefully and check the value of c all of the time.
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Mar 6 '06 #11
"Imad" <md*****@gmail.com> writes:
Thanks very much, but I already considered both approaches.

The thing is I will be using the structure for parsing a compressed
file. If the compressed data is of say TYPE A I shall use only two
elements. If it is of say TYPE B, I will be using all the three. For
reasons of efficiency, I don't want to use the third element. And I
will be using array of the same structure for storing the information.
So using #ifdef is out of question, since I will have to declare the
array in the beginning itself. Also, by using pointers I will still
have a third dummy variable which again is not needed for TYPE A.


The original question was posted by "zoltan" <zo*********@gmail.com>;
this response is from "Imad" <md*****@gmail.com>. Are both of you the
same person? Using a consistent identity makes it easier for the rest
of us to follow the discussion.

And of course you need to read <http://cfaj.freeshell.org/google/>.

You still haven't shown us the actual data type that you're trying to
deal with.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 6 '06 #12
zoltan wrote:
.... snip ...
My question is, is there any way to use the same structure for both
situations such that depending on some flag, I can choose to allocate
memory only for elements a and b ( type 2) or for all 3 elements
(type 1). I do not want to use two different structures for the same.


You'll have to use Pascal for that ability.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Mar 7 '06 #13
On Tue, 07 Mar 2006 00:28:38 -0500, CBFalconer <cb********@yahoo.com>
wrote:
zoltan wrote:

... snip ...

My question is, is there any way to use the same structure for both
situations such that depending on some flag, I can choose to allocate
memory only for elements a and b ( type 2) or for all 3 elements
(type 1). I do not want to use two different structures for the same.


You'll have to use Pascal for that ability.


Or Ada. Or use _related_ (OO) types in C++ or Ada -- or Ftn 2005 (!)
-- which aren't strictly 'the same' but can be used as if they were.

Even in C you can just e.g.
malloc (offsetof (struct X, first_excluded_member))
although this is a pain to get correct on first coding and a _royal_
pain to keep correct (consistent) during maintenance. And you can only
do this for dynamic aka 'heap' allocation.

By my reading it is technically UB to do pseudobigptr->smallmember (or
pseudobig.smallmember) if you haven't allocated sufficient space for
all of big, since notionally the abstract machine 'designates' the
entire lvalue and then selects part of it, but I've never seen and
have trouble imagining a non-DS9k implementation where this would
actually fail.

- David.Thompson1 at worldnet.att.net
Mar 20 '06 #14
Dave Thompson <da*************@worldnet.att.net> wrote:
On Tue, 07 Mar 2006 00:28:38 -0500, CBFalconer <cb********@yahoo.com>
wrote:
zoltan wrote:
>

... snip ...
>
> My question is, is there any way to use the same structure for both
> situations such that depending on some flag, I can choose to allocate
> memory only for elements a and b ( type 2) or for all 3 elements
> (type 1). I do not want to use two different structures for the same.


You'll have to use Pascal for that ability.


Or Ada. Or use _related_ (OO) types in C++ or Ada -- or Ftn 2005 (!)
-- which aren't strictly 'the same' but can be used as if they were.

Even in C you can just e.g.
malloc (offsetof (struct X, first_excluded_member))
although this is a pain to get correct on first coding and a _royal_
pain to keep correct (consistent) during maintenance. And you can only
do this for dynamic aka 'heap' allocation.

By my reading it is technically UB to do pseudobigptr->smallmember (or
pseudobig.smallmember) if you haven't allocated sufficient space for
all of big, since notionally the abstract machine 'designates' the
entire lvalue and then selects part of it, but I've never seen and
have trouble imagining a non-DS9k implementation where this would
actually fail.


Such an "actual failure" is described in Eric Sosman's article:
http://groups.google.com/group/comp....e772cf0?hl=en&
From: Eric Sosman <XXX>
Newsgroups: comp.lang.c
Subject: Re: pointer conversion
Date: Tue, 07 Jun 2005 12:19:26 -0400
Message-ID: <d8**********@news1brm.Central.Sun.COM>

(I disagree only in one point of his analysis: I believe the complier
was deceived by the member access expression - which was disambiguated
through the pointer type - not by the pointer type itself. In any case,
the compiler got its revenge...)

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Mar 24 '06 #15

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

Similar topics

8
by: MENTAT | last post by:
Hi Guys, Newbie question here. Part of my xml looks like this. <node> <description/> <config/> <log/> <transition/> <node>
3
by: Douglas Buchanan | last post by:
Newbie to donnet This is an example of a structure given in vs.net help ============================ Private Structure Employee Public GivenName As String ' This employee's given name....
2
by: Cybertof | last post by:
Hello, I would like to understand the difference between declaring a structure/variable in a class body, and doing the same thing in a procedure body. Example : In the following code, what...
3
by: ANoobee | last post by:
What is the best approach to force atleast one of a few optional elements required in an XSD? This is what I'm tring to do: <email> <to> <cc> <bcc> </email>
0
by: Frank Cusack | last post by:
Is it possible to require one or more from a list of optional elements? If I have something like: <element name="parent"> <oneOrMore> <interleave> <optional> <element name="child1"> <text/>...
0
by: DopeyinIdaho | last post by:
We have a lot of legacy code written in C/C++ that maintains data in shared memory structures. In the C/C++ implementations, for a lot of functions, we get a pointer to the shared memory address then...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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.