Structure memory allocation

U

uday

Hi All,
I need one clarification regarding memory allocation for
structure. The details are as given below :

let us consider one structure

struct {
uit32 len0;
uint8 *pointer0;
uit32 len1;
uint8 *pointer1;
);

When i want to allocate memory using malloc() for this structure is
there any way i can allocate memory for only for len0 and pointer0 or
len1 and pointer1 .

Regards,
udaykumar
 
I

Ian Collins

uday said:
Hi All,
I need one clarification regarding memory allocation for
structure. The details are as given below :

let us consider one structure

struct {
uit32 len0;
uint8 *pointer0;
uit32 len1;
uint8 *pointer1;
);

When i want to allocate memory using malloc() for this structure is
there any way i can allocate memory for only for len0 and pointer0 or
len1 and pointer1 .
No, why would you want to?
 
U

uday

No, why would you want to?

I need to combine 2 function which uses the same structure .... i.e
for Ipv6 and Ipv6oEth i want to use the same struct .... so i will abe
able to combine the functionality
 
P

p_cricket_guy

Hi All,
I need one clarification regarding memory allocation for
structure. The details are as given below :

let us consider one structure

struct {
uit32 len0;
uint8 *pointer0;
uit32 len1;
uint8 *pointer1;
);

When i want to allocate memory using malloc() for this structure is
there any way i can allocate memory for only for len0 and pointer0 or
len1 and pointer1 .


I assume you do not intend to use {len0, pointer0} and {len1,
pointer1}
simultaneously. In that case, an union may be what you want.

-p_c_g.
 
A

Army1987

Hi All,
I need one clarification regarding memory allocation for
structure. The details are as given below :

let us consider one structure

struct {
uit32 len0;
uint8 *pointer0;
uit32 len1;
uint8 *pointer1;
);

When i want to allocate memory using malloc() for this structure is
there any way i can allocate memory for only for len0 and pointer0 or
len1 and pointer1 .

The latter is impossible. For the former you could try
malloc(offsetof(struct unnamed, len1)), I think it has undefined
behavior if you do enough language lawyering on the Standard, but
it is very likely to work.
 
A

Army1987

uday wrote: [snip]
let us consider one structure
struct {
uit32 len0;
uint8 *pointer0;
uit32 len1;
uint8 *pointer1;
);
When i want to allocate memory using malloc() for this structure is
there any way i can allocate memory for only for len0 and pointer0 or
len1 and pointer1 .
No, why would you want to?
I need to combine 2 function which uses the same structure .... i.e
for Ipv6 and Ipv6oEth i want to use the same struct .... so i will abe
able to combine the functionality

A union of two structs, maybe?
 
K

Keith Thompson

uday said:
I need to combine 2 function which uses the same structure .... i.e
for Ipv6 and Ipv6oEth i want to use the same struct .... so i will abe
able to combine the functionality

I'm not entirely clear on what you're trying to do, but it would
probably make sense to use nested structures. For example:

struct s0 {
uint32 len0;
uint8 *pointer0;
};
struct s1 {
uint32 len1;
uint8 *pointer1;
};
struct both {
struct s0 member0;
struct s1 member1;
};

You can then allocate the entire outer structure or just one of the
inner structures.
 
P

pete

uday said:
Hi All,
I need one clarification regarding memory allocation for
structure. The details are as given below :

let us consider one structure

struct {
uit32 len0;
uint8 *pointer0;
uit32 len1;
uint8 *pointer1;
);

When i want to allocate memory using malloc() for this structure is
there any way i can allocate memory for only for len0 and pointer0 or
len1 and pointer1 .

struct {
uit32 len;
uint8 *pointer;
);
 
T

Tor Rustad

uday said:
I need to combine 2 function which uses the same structure .... i.e
for Ipv6 and Ipv6oEth i want to use the same struct .... so i will abe
able to combine the functionality

Hmm.. what with:

struct ip
{
uint32 len;
uint8 *ptr;
};

union ip_v6
{
struct ip v6;
struct ip v6Eth;
};

?
 
U

uday

I'm not entirely clear on what you're trying to do, but it would
probably make sense to use nested structures. For example:

struct s0 {
uint32 len0;
uint8 *pointer0;
};
struct s1 {
uint32 len1;
uint8 *pointer1;
};
struct both {
struct s0 member0;
struct s1 member1;
};

You can then allocate the entire outer structure or just one of the
inner structures.

--
Keith Thompson (The_Other_Keith) (e-mail address removed) <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

hi thanks for the possible solution ... In my case i think the use of
union dont serve my purpose but defining 2 struct within one struct
will serve my purpose ...
Regards,
udaykumar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top