• Advertisement

Archived

This topic is now archived and is closed to further replies.

[C++] Dynamic Arrays as Class Members?

This topic is 5059 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 am writing an application, that requires a dynamic array to be a class member, and I go about defining it by
TYPE *member;
    
Then, in the constructor:
member = new TYPE[determinedSize];
    
... where determinedSize is the size needed for the array. This seems to be giving me problems... I can always access the first element of the array just fine, but then when i access the second or third, or so on, it gives me invalid data... Is there something wrong with my above code... Am I missing something? I'm almost positive that the rest of my code is just fine, as it does place the correct data into the first element of the array. [edited by - xempest on April 14, 2004 1:50:48 AM]

Share this post


Link to post
Share on other sites
Advertisement
I don''t see anything immediately wrong with that. You''ll probably have to show more code.

Oh, and I''ll might as well say it, because if I don''t someone else will: you could always try using a std::vector<TYPE> instead of manually managing the dynamic array. That way you don''t have to worry if your allocation was done correctly.

Share this post


Link to post
Share on other sites
Hmm... I guess I'll give it a whirl with vectors... and I'll post any additional questions / problems I have.

[edited by - xempest on April 14, 2004 1:56:08 AM]

Share this post


Link to post
Share on other sites
hmm...
TYPE* member;
member = new TYPE[determinedSize];

looks right, without seeing any other code I''d have to say that problem lies else where. A

-----------------------------------------------------
Writer, Programer, Cook, I''m a Jack of all Trades
Current Design project: Ambitions Slave

Share this post


Link to post
Share on other sites
Well, what I''m doing is writing a class, based on a texture pack file format. However, since the texture pack can contain more than one texture, I read the given size in from the file (it''s in the header), and I create the array to that size. I begin to read the specific texture data, including name, dimensions, etc. This all works perfectly for the first texture, however, it does not for any further textures. For instance, texture 1 will contain the proper name, yet texture 2 will contain a name made up of incorrect characters. (The same for further textures.) I''m almost certain that I am indeed reading the file correctly, because I have checked many things, including all of the offsets in the file. However, I still end up with bad data in the array.

To deal with the file reading, I use the FILE type from stdlib, and I use ordered segments of ''fread''ing + converting BYTES, WORDS and DWORDS (looped the amount of textures, since the the file format lists the texture information that way) to load the texture information into the array. I''m certain that I am doing this correctly, as again, it works perfectly for the first texture.

I''m sorry that I can''t post the whole code, it''s made up of classes and types defined by me, which are pretty long.

Is there something I''m missing about the FILE type, or is something flawed in my methodology?

Share this post


Link to post
Share on other sites
I'm pretty sure I understand your problem, it sounds similar to one I had recently and the cause was pointers. But to make sure. let say you have.

class Tex
{
public:
Tex(char*);
~Tex();
setName(char*);
char* getName();
private:
char* name_;

}


void read(string fileName)
{
ifstream fin(fileName.c_str(), ios_base::in | ios_base::binary);
if (!fin)
{
cout << "Unable to open for reading.\n";
}
else
{

int size;
fin.read(size,sizeof int);
member=new Tex[size];
Tex* temp = new Tex();
for(int i=0; i<size;i++)
{
fin.read((char*) &temp,sizeof Tex);
member[i]=temp; //This should be a deep copy and not a shallow copy.

}

fin.close();
delete temp;
}


hmm, I just realized that doesn't use FILE it use ifstream. But I assume you that your doing something similar using FILE?

-----------------------------------------------------
Writer, Programer, Cook, I'm a Jack of all Trades
Current Design project: Ambitions Slave


[edited by - TechnoGoth on April 14, 2004 2:38:11 AM]

Share this post


Link to post
Share on other sites
I''ve found the problem! I was reading in these values from the file, which were BYTES (0 to 255, same thing as an unsigned char), while reading can only read CHARS (-127 to 127). This would end me up with an obviously incorrect value. Is there any way to read an unsigned character? (Or an unsigned character array, in the case of read.)

Share this post


Link to post
Share on other sites

  • Advertisement