Click here to Skip to main content
Licence CPOL
First Posted 27 Apr 2007
Views 15,245
Bookmarked 9 times

Effective Coding: Reducing memory allocation/deallocation

By Vahid Kazemi | 27 Apr 2007
This article describes how you can effectively allocate a multi-dimensional array in C++.
6 votes, 50.0%
1
4 votes, 33.3%
2

3
1 vote, 8.3%
4
1 vote, 8.3%
5
1.71/5 - 12 votes
1 removed
μ 2.05, σa 2.29 [?]

Introduction

Writing effective code is one of the most important and challenging responsibilities of a programmer. High-level programming languages like C++ hide a lot of things from a programmer, that you can hardly guess what exactly will be your compiled code. A simple call to a function in C++ could be dozens of lines of machine code, but you don't need to care because your computer can process billions of lines of machine code in a second; but when it comes to video game programming, everything changes, and that's because you have limited time and a lot of objects to process.

Memory Allocation

A simple optimization technique that I want to talk about here is about memory allocation and deallocation of fixed multi-dimensional arrays. Here is a simple code to allocate and free a two dimensional array:

// allocate

T **data = new T*[sizeX];
for(int i=0;i<sizeX;i++){
 data[i]=new T[sizeY]
}
// free

for(int i=0;i<sizeX;i++){
 delete [] data[i]
}
delete [] data;

But if your array is fixed and you don't plan to change its size, then it's not the most efficient way to do it. Consider you want to allocate a 100*10 array. To allocate and free this amount of memory, you should call new and delete operators 101 times, and that takes a lot of CPU time; but with a little change to your code, you can optimize it so that one call to new and delete does all the job. Here is my implementation:

class FixedArray
{
public:
    template<class T>
    static T **New(int sizeX, int sizeY)
    {
        int sizeAddress = sizeof(T*)*sizeX;
        int sizeData = sizeof(T)*sizeX*sizeY;

        char *data = new char[sizeAddress+sizeData];

        for(int i=0;i<sizeX;i++){
            ((T**)data)[i] = &((T*)(data+sizeAddress))[i*sizeY];
        }

        return (T **)data;
    }

    static void Delete(void *p)
    {
        delete [] p;
    }
};

You've got the idea, but if you are looking for a universal code, check Boost MultiArray.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Vahid Kazemi

Software Developer
OculusAI
Sweden Sweden

Member
I have got my BSc in Computer Science from SBU, and I am currently a master student of Systems, Controls and Robotics at KTH, University of Technology. I am currently doing research and development in Computer Vision area.
 
My homepage at KTH. I am currently looking for a PhD position.
 
GameProgrammer.org, My website containing tutorials about Direct3D and OpenGL and more.
 
"Quest of Persia", An adventure game which I've worked on as programmer.
 
contact: vkazemi [at] gmail.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWhy not use Boost.MultiArray? Pinmemberstdedd1:20 28 Apr '07  
AnswerRe: Why not use Boost.MultiArray? PinmemberVirtual Coder2:26 29 Apr '07  
GeneralRe: Why not use Boost.MultiArray? Pinmemberstdedd5:22 29 Apr '07  
GeneralRe: Why not use Boost.MultiArray? PinmemberVahid Kazemi8:21 30 Apr '07  
QuestionDoes this work for object types too? PinmemberK-ballo15:25 27 Apr '07  
AnswerRe: Does this work for object types too? Pinmemberdabs16:01 27 Apr '07  
GeneralRe: Does this work for object types too? PinmemberK-ballo16:25 27 Apr '07  
GeneralRe: Does this work for object types too? PinmemberVirtual Coder0:13 28 Apr '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 27 Apr 2007
Article Copyright 2007 by Vahid Kazemi
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid