-
March 11th, 2010, 01:05 AM
#1
Dynamic Allocation of User-Defined Structure Type
Hi everyone
I'm working on a video processing project using Visual Studios 2008 (on a Vista OS), and I've been timing each function in order to see where my code is running the slowest.
I've isolated it down to when I allocated memory dynamically using malloc for two structures which I have made respective typedefs for. The majority of the computation time is purely due to allocating memory to these two structures. I'm fairly new to C, so I'm probably overlooking some vital fact regarding efficient memory allocation.
Here is the code snippet for when memory is allocated:
*********************************************************************
RegularStructure *reg;
VirtualStructure *vir;
reg = ( RegularStructure* ) malloc( imHeight*imWidth * sizeof( RegularStructure ) );
vir = ( VirtualStructure* ) malloc( imHeight*imWidth * sizeof( VirtualStructure ) );
*********************************************************************
The type definitions are part of a header file, here is the relevant code:
Code:
*********************************************************************
typedef struct RegularStructure
{
int type;
float hue;
float saturation;
float value;
int sonType;
int regSonCount;
int virSonCount;
int regSonsRow[ 2000 ];
int regSonsCol[ 2000 ];
int virSonsRow[ 2000 ];
int virSonsCol[ 2000 ];
int parentType;
int regParentRow;
int regParentCol;
int virParentRow;
int virParentCol;
int regIntraCount;
int regIntraRow[ 300 ];
int regIntraCol[ 300 ];
int virIntraCount;
int virIntraRow[ 300 ];
int virIntraCol[ 300 ];
} RegularStructure;
typedef struct VirtualStructure
{
int type;
float hue;
float saturation;
float value;
int sonType;
int regSonCount;
int virSonCount;
int regSonsRow[ 2000 ];
int regSonsCol[ 2000 ];
int virSonsRow[ 2000 ];
int virSonsCol[ 2000 ];
int parentType;
int regParentRow;
int regParentCol;
int virParentRow;
int virParentCol;
int regIntraCount;
int regIntraRow[ 300 ];
int regIntraCol[ 300 ];
int virIntraCount;
int virIntraRow[ 300 ];
int virIntraCol[ 300 ];
} VirtualStructure;
*******************************************************************
If anyone can provide help or insight, it would be greatly appreciated.
Faithfully,
John.
Last edited by Marc G; March 11th, 2010 at 09:58 AM.
Reason: Added code tags
-
March 11th, 2010, 03:29 AM
#2
Re: Dynamic Allocation of User-Defined Structure Type
so I'm probably overlooking some vital fact regarding efficient memory allocation.
There is nothing more than malloc/free in C. Allocating data is slow, so do it as less as possible.
-
March 11th, 2010, 09:13 AM
#3
Re: Dynamic Allocation of User-Defined Structure Type
malloc/new in VS has been designed to be optimal for small allocations. you seem to be allocating very large amounts of memory, which is something new/malloc will do for you, but aren't as efficient at.
Try using VirtualAlloc() for allocating large (larger than several hundred KB) amounts of memory.
-
March 11th, 2010, 04:32 PM
#4
Re: Dynamic Allocation of User-Defined Structure Type
Thank you for your replies,
Just for clarification, allocating dynamic structures with smaller sized array members will be faster than what I currently have? Are there more efficient ways to allocate memory to structures?
Regards,
John
-
March 11th, 2010, 05:44 PM
#5
Re: Dynamic Allocation of User-Defined Structure Type
I don't think anyone can give you an absolute answer as to what will be faster. Only avenues you can explore which might be.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|