Sign in to follow this  
AdamGL

Linked lists + new command

Recommended Posts

I am trying to make a texture handler for my game. I want to have linked lists so that I don't have a maximum cap of textures(other than the system memory cap). This would be rather easy to implement, but I hit something. The tga loader I use frees up some memory for the image data within the tga structure. Is it possible to create memory using "new", then create memory for a single part of the structure you just freed up memory for? Does it waste memory? Tell me if I'm not clear enough...

Share this post


Link to post
Share on other sites
I'm not quite sure what you're asking. Are you trying to free the tga structure after loading it, but keep the image data around without having to move it in memory?

Share this post


Link to post
Share on other sites
I don't think so. It's like this:


struct tga
{
stuff in tga
unsigned char *data;
tga *next;
};

texture = new tga; <------ make room for this structure

loadtga(tga *texture)
{
opens file
texture->data = new unsigned char[imageSize];
load info into data
};



Now, as you can see, i free up memory twice. Once for the whole structure, and the second time for the single member. Is this ok? Is there another way to do it?

Share this post


Link to post
Share on other sites
yes that would work

however, since you are using C++ I highly, highly suggest you use a STL class, and constructors and destructors rather than roll your own. Something like: (disclaimer, written off top of head)


#include <list>

struct tga {
stuff in tga
unsigned char* data;

tga(const char* filename) {
opens file
data = new unsigned char[imageSize]
load into data
}

~tga() {
delete[] data;
}
};

tga image1("C:\\image.tga");

std::list<tga> my_textures;
my_textures.push_back(image1);



This code will be much easier to debug and a lot less likely to leak memory.

Share this post


Link to post
Share on other sites

You aren't exactly freeing memory twice. That would be quite fatal. You are freeing two different parts of memory.

It is totally ok to first allocate a class and then allocate memory inside the class. As far as I can see there is no other way and in my opinion, this is the right and the only way.

From a point of view of an graphical engine, it doesn't matter whether the image is tga or jpg or what ever since the graphical engine only cares for the data it is processing. You aren't telling about the environment you are working in, but I'd go for more generalized texture handling such as :

class Texture
{
public:
// Handle to graphics api object

char *data; //may not be needed if API handles the data

loadtga(file ...);
loaddds(file ...);

//or just load(file ...); and let the class figure out the format from the extension.

};

Share this post


Link to post
Share on other sites
Quote:
Original post by etothex
yes that would work

however, since you are using C++ I highly, highly suggest you use a STL class, and constructors and destructors rather than roll your own. Something like: (disclaimer, written off top of head)

*** Source Snippet Removed ***

This code will be much easier to debug and a lot less likely to leak memory.


QFE, QFT etc.

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