Allocating Memory

Started by
1 comment, last by RuneLancer 18 years, 9 months ago
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.
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.
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!

This topic is closed to new replies.

Advertisement