Sign in to follow this  
Nyte_Falcon

Allocating Memory

Recommended Posts

i know there is the "new" command in C++ to allocate memory, however i am making a program right now, where i need to store pictures in the memory is there a way to do that within C++ or using the "new" command in conjunction with storing pictures.

Share this post


Link to post
Share on other sites
Well, how do you plan to load the image? Most image loading libraries supply functions that allocate space for the image in memory for you.

Share this post


Link to post
Share on other sites
If you want to do the loading yourself, you can use the new command to reserve memory to hold your image data. Like SiCrane mentionned, though, most libraries and APIs handle images on their own.

"new" returns a pointer to a block of data you've initialised. For instance, if you want an array of 100 integers, you'd do...

int *integerArray = new int[100];

You could then use "integerArray" as though it were declared...

int integerArray[100];

The thing is, new allows you to use variable sizes. Since your images may be of different width/height, you could calculate how much space you want via (height x width x colorDepth) and use new to reserve that much memory. Then you'd just have to load the array it returns up with the image data, and you're done.

Don't forget to delete[] afterwards when you don't need your array anymore!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this