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:
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 08:58 AM.
Reason: Added code tags
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.
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?
Bookmarks