473,327 Members | 2,112 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,327 software developers and data experts.

how to allocate memory to a member dynamically

Hello ,

This is Raghu. I have a problem in declaring a structure.
Consider
struct hai{
int id;
char sex;
int age;
};
here when a variable is instianted for this structure then immediately
for all members memory is allocated. But I need to allocate memory for
age only if sex is M else the memory should not allocate.
Think you understand the query.
and awaiting for your reply
bye take care
with smile
Raghu

Oct 24 '06 #1
14 2603
raghu <ra*********@gmail.comwrote:
Hello ,
This is Raghu. I have a problem in declaring a structure.
Consider
struct hai{
int id;
char sex;
int age;
};
here when a variable is instianted for this structure then immediately
for all members memory is allocated. But I need to allocate memory for
age only if sex is M else the memory should not allocate.
Then you can't use the structure like this - a structure _is_ the
set of its components and when you have the compiler create a new
instance of the structure it will give you memory for all its
members. The only way out is to have a pointer in the structure
that you assign memory to manually under certain conditions only,
like in (required error checking left out):

struct hai2 {
int id;
char sex;
int *age;
} person;

/* Assign some value to person.sex here */

if ( person.sex == 'M' ) {
person.age = malloc( sizeof *person.age );
*person.age = 17;
}

Of course, you also have to take care of deallocating the memory before
the structure goes out of scope.

Obvioulsy, this looks stupid in this example (the int pointer probably
requires at least as much memory as an integer), but there are several
situations where such an approach makes a lot of sense.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Oct 24 '06 #2

raghu wrote:
Consider
struct hai{
int id;
char sex;
int age;
};
here when a variable is instianted for this structure then immediately
for all members memory is allocated. But I need to allocate memory for
age only if sex is M else the memory should not allocate.
Think you understand the query.
and awaiting for your reply
I suppose you could do something like this (error handling omitted for
brevity) :-

char sex;
int age;
struct hai *hp;
....
<processing to get the data to work with>
....
if (sex == 'M') {
hp = malloc(sizeof(struct hai));
hp->id = <whatever>;
hp->sex = sex;
hp->age = age;
} else { /* I assume you've validate that sex is 'M' or 'F' */
hp = malloc(sizeof(struct hai) - sizeof(int));
hp->id = <whatever>;
hp->sex = sex;
}

It's ugly and almost guaranteed to cause you grief in later times, but
it does (in some sense) meet your requirement.

However, your requirement is nonsense. Saving 1 machine word per
"struct hai" is not worth bothering with unless you are hugely
memory-constrained, IMNSHO...

Oct 24 '06 #3

mark_blue...@pobox.com wrote:
..., your requirement is nonsense. Saving 1 machine word per
"struct hai" is not worth bothering with unless you are hugely
memory-constrained, IMNSHO...
As Jens points out, if there is more than just a single word, such as a
nested structure, the presence or absence of which is dependant on
other data in the structure, then a pointer-based solution may be
appropriate...

Oct 24 '06 #4
Okay but the problem is i have number of such type of members in a
structure. And instead of a byte I want to check the bit positions. If
i followed ur approach then i have to waste the memory of 4 bytes for
each member. But i don't want to happen like that.
-Raghu
Jens Thoms Toerring wrote:
raghu <ra*********@gmail.comwrote:
Hello ,
This is Raghu. I have a problem in declaring a structure.
Consider
struct hai{
int id;
char sex;
int age;
};
here when a variable is instianted for this structure then immediately
for all members memory is allocated. But I need to allocate memory for
age only if sex is M else the memory should not allocate.

Then you can't use the structure like this - a structure _is_ the
set of its components and when you have the compiler create a new
instance of the structure it will give you memory for all its
members. The only way out is to have a pointer in the structure
that you assign memory to manually under certain conditions only,
like in (required error checking left out):

struct hai2 {
int id;
char sex;
int *age;
} person;

/* Assign some value to person.sex here */

if ( person.sex == 'M' ) {
person.age = malloc( sizeof *person.age );
*person.age = 17;
}

Of course, you also have to take care of deallocating the memory before
the structure goes out of scope.

Obvioulsy, this looks stupid in this example (the int pointer probably
requires at least as much memory as an integer), but there are several
situations where such an approach makes a lot of sense.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Oct 24 '06 #5
raghu <ra*********@gmail.comwrote:
Okay but the problem is i have number of such type of members in a
structure. And instead of a byte I want to check the bit positions. If
i followed ur approach then i have to waste the memory of 4 bytes for
each member. But i don't want to happen like that.
One idea that buys you infinite flexibility at the cost of one void *,
one int, and 2^n-1 structure definitions (where n is the number of these
members that you don't want to automatically allocate memory for) -
making it a terribly silly idea, really - follows:

enum struct_types { FOO, BAR, FOO_AND_BAR };

struct my_struct {
int baz;
/* other fields */
int struct_type;
void *variable_struct;
};

struct foo {
int foo;
};

struct bar {
int bar;
};

struct foo_and_bar {
int foo;
int bar;
};

struct my_struct *allocate_my_struct( int type ) {
/* Error checking of malloc() calls omitted */
struct my_struct *ret=malloc( sizeof *ret );
switch(type) {
case FOO:
ret->variable_struct=malloc( sizeof(struct foo) );
break;
case BAR:
ret->variable_struct=malloc( sizeof(struct bar) );
break;
case FOO_AND_BAR:
ret->variable_struct=malloc( sizeof(struct foo_and_bar) );
break;
default:
ret->variable_struct=NULL;
}
ret->struct_type=type;
return ret;
}

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Oct 24 '06 #6

raghu wrote:
... the problem is i have number of such type of members in a
structure. And instead of a byte I want to check the bit positions. If
i followed ur approach then i have to waste the memory of 4 bytes for
each member. But i don't want to happen like that.
First point - don't top-post. It's greatly disliked in this group and
you'll lose such friends as you have.

Second point - if that is your problem, then you should have told us
about it first, rather than the trivial reduction of it. I think we
still need a clearer idea of what you are really trying to do before we
can give more than the most general guidance. Christopher's suggestion
seems fairly close though I'd also consider what could be achieved with
(for example) the use of unions...

Oct 24 '06 #7
Well, it's hardly worth the effort. If you're allocating with
malloc() there's typically a minium allocation of 8 to 16 bytes, so
saving the "age" field isn much of a savings. In fact often no savings
at all, if malloc does 16-byte alignment for speed.

Also, most people don't live more than 127 years, so you could store
the gender and age in one byte. Either with a union, or by some
convention, such as a signed char where -1 implies female, positive
implies male and gives their age.

Oct 24 '06 #8
What you're trying to do can be expressed in better-thought-out
languages such as Pascal, which have the concept of a "variant record",
your structure would look like:

hai = record
id: integer;
case
sex: boolean of
true: ( age: integer );
false: ( );
end;

But in C you don't have that option. You can fake it somewhat by
defining TWO structs, one with and the other without the age field,and
malloc the right one each time.

Oct 24 '06 #9
raghu wrote:
>
Okay but the problem is i have number of such type of members in a
structure. And instead of a byte I want to check the bit positions.
If i followed ur approach then i have to waste the memory of 4
bytes for each member. But i don't want to happen like that.
Don't top-post, or you will likely be ignored in this group. Your
answer belongs after, or possibly intermixed with, the _snipped_
material you quote. Also you should improve your spelling. The
first person pronoun is I, capitalized. 'your' is spelt as shown.
Using such abortions as i and ur only makes your post hard to read
and is offensive.

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

Oct 24 '06 #10
On Tue, 2006-10-24 at 04:21 -0700, raghu wrote:
<formalities snipped>
Consider
struct hai{
int id;
char sex;
int age;
};
here when a variable is instianted for this structure then immediately
for all members memory is allocated. But I need to allocate memory for
age only if sex is M else the memory should not allocate.
<formalities snipped>

There's no way to do this. You could pull it off using pointers, but
then you'd still have to allocate memory for the pointers itself
(which, chances are, is just as large as an int would be).

If you really need to do this, you should create separate structs
for men and women. In an OO language, you could do this a little
more elegantly than in C.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Oct 24 '06 #11
On Tue, 2006-10-24 at 07:04 -0700, Ancient_Hacker wrote:
Well, it's hardly worth the effort. If you're allocating with
malloc() there's typically a minium allocation of 8 to 16 bytes, so
saving the "age" field isn much of a savings. In fact often no savings
at all, if malloc does 16-byte alignment for speed.

Also, most people don't live more than 127 years, so you could store
the gender and age in one byte. Either with a union, or by some
convention, such as a signed char where -1 implies female, positive
implies male and gives their age.
Remember to quote context. Yours seems to be the cleverest solution to
the OP's problem, but few can remember what that problem was!

(It was, essentially, "can I only allocate the .age member of a struct
if the .sex member is 'M'"?)
--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Oct 24 '06 #12
"Ancient_Hacker" <gr**@comcast.netwrites:
What you're trying to do can be expressed in better-thought-out
languages such as Pascal, which have the concept of a "variant record",
your structure would look like:

hai = record
id: integer;
case
sex: boolean of
true: ( age: integer );
false: ( );
end;

But in C you don't have that option.
The "case" doesn't do anything in your example. It's "the same"
structure as in C99 would be:

#v+
struct hai {
int id;
char sex;
union {
int age;
};
};
#v-

"case" insinde of a "record" in Pascal is very similar to "union" and
still your record hai will consume at least sizeof(integer) +
sizeof(boolean) + sizeof(integer) bytes no mether the value of sex.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
Oct 26 '06 #13
Andrew Poelstra <ap*******@false.sitewrote:
On Tue, 2006-10-24 at 07:04 -0700, Ancient_Hacker wrote:
Well, it's hardly worth the effort. If you're allocating with
malloc() there's typically a minium allocation of 8 to 16 bytes, so
saving the "age" field isn much of a savings. In fact often no savings
at all, if malloc does 16-byte alignment for speed.

Also, most people don't live more than 127 years, so you could store
the gender and age in one byte. Either with a union, or by some
convention, such as a signed char where -1 implies female, positive
implies male and gives their age.

Remember to quote context. Yours seems to be the cleverest solution to
the OP's problem, but few can remember what that problem was!
Clever, but also wrong. The signed char option works, but is rather
brittle and hard to maintain. The union option simply does not work. You
can't store two things in a byte with a union. You can store _either_
one _or_ the other, and writing to either destroys the other.

What might work, or might not, but is at least correct C, is to use
bitfields. 1 bit for the sex, 7 bits for the age. Both unsigned, of
course. Unfortunately there's no guarantee that you can use unsigned
chars for bitfields (only signed and unsigned int, and in C99 _Bool -
and implementation-defined types, so you _may_ be able to use unsigned
char, but it's not portable), so there's no guarantee that you save any
memory this way, over a simple non-bitfield unsigned char for both.

Richard
Oct 26 '06 #14

Michal Nazarewicz wrote:
"case" insinde of a "record" in Pascal is very similar to "union" and
still your record hai will consume at least sizeof(integer) +
sizeof(boolean) + sizeof(integer) bytes no mether the value of sex.

Ah, not exactly. If the record has a "discriminated" variant part,
meaning there's a variable in the record that indicates which variant
this is, as in this example, then when you make a new instance of the
record with new(), you can specify which variant record size you want,
for instance in the above example: new( Rec, true ) would allocate
only enough space for a record with the sex=true option.

Oct 26 '06 #15

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

Similar topics

5
by: lixiaoyao | last post by:
hi all I use matrix & vector function to allocate the space myself in c, typedef struct matrix_array newdata; struct matrix_array{ float **sy,*sxx; }; newdata ndata;//new data struct...
3
by: Tony Johansson | last post by:
Hello! When you allocate object dynamically which mean on the heap I find that a problem when using exception. What is the prefer method to handle this kind of problem. //Tony
4
by: marora | last post by:
I have created class definition which contains a charater pointer as one of it's data memeber. The objective is to read some data from a file, and assign it to a data member; Size of data is...
7
by: Michael | last post by:
Hi, What's the benefit to dynamically allocate memory? using namespace std; int main() { char* ptr; ptr="abc";
2
by: xhunga | last post by:
I have try a new version of my work. I have put the sizes of the matrix into the matrix. A = number of rows A = number of columns The first element of the matrix is A instead of A. You...
5
by: raghu | last post by:
Hello , This is Raghu. I have a problem in declaring a structure. Consider struct hai{ int id; char sex; int age; }; here when a variable is instianted for this structure then immediately
20
by: ramasubramanian.rahul | last post by:
hi folks i have a peculiar problem. i have to allocate more than size_t consequtive bytes on a system . after i do a malloc .. i am unable to do a realloc because it takes size_t as a new size...
2
by: lovecreatesbea... | last post by:
If the built-in operator keyword new doesn't allocate memory on heap and it calls global operator new (::operator new) or class member operator new to do that. What are the two kinds of operator...
17
by: dtschoepe | last post by:
Hi, I have a homework project I am working on, so be forwarned, I'm new to C programming. But anyway, having some trouble with a memory allocation issue related to a char * that is a variable...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.