Dynamic Allocation of User-Defined Structure Type
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Dynamic Allocation of User-Defined Structure Type

Hybrid View

  1. #1
    Join Date
    Mar 2010
    Posts
    11

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

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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.

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,761

    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.

  4. #4
    Join Date
    Mar 2010
    Posts
    11

    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

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center