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

how to allocate memory to a member at run time

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.

awaiting for your reply
bye take care
with smile
Raghu

Oct 24 '06 #1
5 1771

raghu wrote:
Hello ,

This is Raghu. I have a problem in declaring a structure.
yes, you said - why ask twice?

Oct 24 '06 #2
struct hai
{
int id;
char info[1];
}
If sex is M then p = (struct hai*)malloc(sizeof(struct hai)+1).
p->info[0] value is M,and p->info[1] is age.
If sex is F then p= (struct hai*)malloc(sizeof(struct hai)).p->info[0]
value is F.
raghu 写é“:
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.

awaiting for your reply
bye take care
with smile
Raghu
Oct 24 '06 #3
th******@126.com wrote:
struct hai
{
int id;
char info[1];
}
If sex is M then p = (struct hai*)malloc(sizeof(struct hai)+1).
p->info[0] value is M,and p->info[1] is age.
If sex is F then p= (struct hai*)malloc(sizeof(struct hai)).p->info[0]
value is F.
1. Don't cast the return value of malloc(). Search this group's
archives and the FAQ.

2. OP stated that age is an int, not a char:
struct hai{
int id;
char sex;
int age;
};
The struct hack is really not particularly helpful in OP's situation
anyway.

--
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 #4
On Tue, 2006-10-24 at 06:10 -0700, th******@126.com wrote:
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.

struct hai
{
int id;
char info[1];
}
If sex is M then p = (struct hai*)malloc(sizeof(struct hai)+1).
p->info[0] value is M,and p->info[1] is age.
If sex is F then p= (struct hai*)malloc(sizeof(struct hai)).p->info[0]
value is F.
Firstly, snip signatures and don't top-post.
Secondly, write your malloc()'s more robustly:

p = malloc(sizeof *p + 1);
p = malloc(sizeof *p);
--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Oct 24 '06 #5
Andrew Poelstra wrote:
>
.... snip ...
>
Firstly, snip signatures and don't top-post.
Secondly, write your malloc()'s more robustly:

p = malloc(sizeof *p + 1);
p = malloc(sizeof *p);
Clearer is:

p = malloc(1 + sizeof *p)

which makes it absolutely clear that sizeof operates on *p alone.

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

Oct 25 '06 #6

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

Similar topics

4
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
12
by: gc | last post by:
I am writing a function that given nx1 vector a and a nx1 b solves a system of equations f(a,c)=b for a nx1 c. While writing the function: 1] Should I allocate the memory for c within the...
8
by: M. Moennigmann | last post by:
Dear all: I would like to write a function that opens a file, reads and stores data into an 2d array, and passes that array back to the caller (=main). The size of the array is not known before...
6
by: Peter Hickman | last post by:
I have a program that requires x strings all of y length. x will be in the range of 100-10000 whereas the strings will all be < 200 each. This does not need to be grown once it has been created....
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...
14
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
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...
11
by: Bryan Parkoff | last post by:
I want to know how much static memory is limited before execution program starts. I would write a large array. The large array has 65,536 elements. The data size is double word. The static...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
1
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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

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.