Linked lists + new command

Started by
4 comments, last by Zahlman 17 years, 11 months ago
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...
bi-FreeSoftware
Advertisement
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?
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 structureloadtga(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?
bi-FreeSoftware
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.

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.

};
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.

This topic is closed to new replies.

Advertisement