Allocating memory for structs

W

Win Sock

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 .
 
S

santosh

Win 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 .

Yes, place each pair in a sub-structure and place both the
sub-structures in a union.
 
M

Malcolm McLean

Win Sock 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 .
union. If you have either a len0 and pointer0, or len1 and pointer1, and you
want to squeeze both into the same space, unions are designed for the task.
 
K

Keith Thompson

Win Sock said:
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 .

Given that declaration, no, not really.

Others have suggested using a union, but what are you really trying to
accomplish? Presumably allocating memory for only a subset of a
structure is a means to an end, not an end in itself. If you tell us
what your actual goal is, we might be able to help (or we might advise
you that it's not worth doing).
 
D

Default User

Win 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 .


This doesn't make very much sense to me. What problem are you trying to
solve?



Brian
 
C

CBFalconer

Win said:
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 .

You probably need a union. However, the types you are using in the
struct seem rather silly. I would suggest replacing uit32 with
size_t, and uint8 with unsigned long. Also don't forget to include
a label for the struct, i.e.:

struct name {
size_t len0;
unsigned long *pointer0;
...
 
K

Keith Thompson

CBFalconer said:
You probably need a union. However, the types you are using in the
struct seem rather silly. I would suggest replacing uit32 with
size_t, and uint8 with unsigned long.
[...]

I agree that size_t is probably a better type for the len0 and len1
members, but how on Earth did you conclude that pointer0 and pointer1
should be pointers to unsigned long rather than to uint8?

(There's no standard type called uint8; perhaps the OP meant uint8_t?)
 
C

CBFalconer

Keith said:
CBFalconer said:
You probably need a union. However, the types you are using in the
struct seem rather silly. I would suggest replacing uit32 with
size_t, and uint8 with unsigned long.
[...]

I agree that size_t is probably a better type for the len0 and len1
members, but how on Earth did you conclude that pointer0 and pointer1
should be pointers to unsigned long rather than to uint8?

Frankly, I have no idea. Thus proving I can write garbage with the
best of them. At the moment pointer to unsigned char seems more
reasonable.
 
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 .
Am I having a dejà-vu, or was this identical question already
posted here a few weeks ago?

What are you trying to do?
I think that you could allocate space for len0 and pointer0 by
doing malloc(offsetof(struct foo, len1)), but I don't have the
courage to try it on my DS9K. And anyway, there is probably a
*much* better way to do what you ultimately need.
As for len1 and pointer1, in some cases
unsigned char *realptr = malloc(sizeof struct unnamed
- offsetof(struct unnamed, len1));
struct unnamed *fakeptr = realptr - offsetof(struct unnamed, len1);
could work, but there will be big problems if e.g. realptr happens
to be at the beginning of a segment, or whatever, so *don't do
that*.
I repeat, what you are trying to do? It is *very* likely that you
picked the wrong path towards that goal.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top