Jump to content
  • Advertisement
Sign in to follow this  
Nyte_Falcon

Allocating Memory

This topic is 4783 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!