Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Memory allocation for structures

Status
Not open for further replies.

camelxl

Programmer
Feb 27, 2004
2
US
Hi all,

I need some help. Could somebody please tell me what is wrong with this code, or maybe suggest a better way to do this.

typedef struct {
long Size;
double Data[1];
} Struct2;
typedef Struct2 **Struct2Hdl;

typedef struct {
double f0;
double df;
Struct2Hdl Array;
} Struct1;

int main (void)
{
Struct1 *Data;

/* Allocate memory for Data structure
Data= (Struct1 *) malloc(sizeof(Struct1));
/* Allocate memory for Array handle
(*Data).Array = (Struct2**)malloc(sizeof(Struct2));
/* Allocate memory for Size element of Array hanle
(*(Power)->Array)->Size = (long*)malloc(sizeof(long));

When i compile i get the following error due to the last line:

error C2440: '=' : cannot convert from 'long *' to 'long'

I know malloc will return a 'long *' but how do i allocate memory to 'Size' and 'Data'

How do i allocate memory for the elements of the structures. If i don't allocate memory i get an 'Unhandled exception error'. Or is there a better way to allocate memory for these complicated structures. Do i have to allocate memory for each elemnt in the structure and what if one of the elements is itself a handle to another structure. How do i allocate memory for it

Thanks
 
The code looks very c-ish. If it actually is c++ I'd recommend c++ify it:
Initialize the structs through constructors.
Perhaps even use classes rather than structs.
Don't use c-style cast.
Use STL (a std::vector perhaps?).
Use new not malloc





/Per
[sub]
"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."[/sub]
 
Compiler is right:
Code:
/* Allocate memory for Size element of Array hanle (????)...
  (*(Power)->Array)->Size = (long*)malloc(sizeof(long));
It seems Size is long, but you assign a pointer to it...
That's all.
 
PerFunt:
Could you explain how to use 'new' in this case because i get the same thing when i try it.

ArkM:
If i do it like this:-
(*(Power)->Array)->Size = (long)malloc(sizeof(long));

i get an "Unhandled exception" error.
 
You shouldn't be using that last malloc at all. The only time you should be allocating memory is if you have a pointer to a data structure.

I think part of your problem is that you're declaring Struct2Hdl as Struct2 ** instead of Struct2 *. I don't see why you would need a double pointer.
 
camelxl:
I don't consider that allocation is OK at all. We don't know what's Power pointer, we allocate a slot for one long only, then save pointer as a size...
Struct2::Data must be a pointer, not an array. C-like allocation for array of n elements must be malloc(n*sizeof(ElementType)), etc...
Keep it simple...
 
typedef struct {
long Size;
double Data[1];
} Struct2;
typedef Struct2 *Struct2Hdl;

typedef struct {
double f0;
double df;
Struct2Hdl Array;
} Struct1;

int main (void)
{
Struct1 *Data;

/* Allocate memory for Data structure
Data = (Struct1 *)malloc(sizeof(Struct1));
/* Allocate memory for Array handle
Data->Array = (Struct2*)malloc(sizeof(Struct2));
/* element Size does not need allocation - it is allready allocated in previous statement
Data->Array->Size = SOME_SIZE;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top