Link to home
Start Free TrialLog in
Avatar of el_rooky
el_rooky

asked on

dynamic memory buffer

I need to read some data from a binary file into a memory buffer. I cannot use array of chars because I have limited memory and i don't know how long the array is going to be. Should I use a CString, cArrays, vectors or what? some example would be appreciated.

Thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of el_rooky
el_rooky

ASKER

I failed to mention that this application is for a winCE device and memory is very limited. I dont want to allocate memory for the entire file size since this file is large and I only need to read portions of the file.

I also should mention that this file contains a file
header with and index to 512 blocks of the data which are not necessarily sequential. After reading all this blocks of data I need to place them in an array of objects.

but if I dont have any other choice, I guess I could use a char* and estimate the lenght of the buffer.
thanks
still el_rooky
Well, if the data blocks are of fixed size, I'd go for a list, e.g.

#include <list>
using namespace std;

struct DataBlock {
 char data [ FIXED_SIZE];
};

list<DataBlock> lstBlocks;
I am not clear on what you want. There is data stored in a file and you need it read, and some objects/data initialized from it? If you don't know the size of the entire file, do you know the amount of data that needs to be read?

>> header with and index to 512 blocks of the data which are not necessarily sequential
Can't you use this header then to determine which portions to read

>> but if I dont have any other choice
To sson to tell.
Well, I'm not sure about it, cause I really don't know WinCE. Is there a memory maped file in WinCE? if there is, it will be more efficient than the other solutions but platform dependent.
I guess there are two parts to my question.
1)How can I load a bunch of data blocks from a file into a memory buffer. I know how to read this data. I am currently reading into a CString. But I rather read it into an array of chars. But since I have limited memory I wanted to use a dynamic array, like Carray which can grow, but I don’t know if that makes sense.

2) Assuming I have read the data into a memory buffer as per jkr’s example,  so all my data is now in an array of chars pBuf, how do can I load my array of parts, bellow, given the location of the first part and the number of parts. For example: the first part record starts at location 102 and there are 20 parts. The parts records are stored sequentially in pBuf.

struct  part
{
int modelNum;
int  partNum;
float cost;
};


part* Parts[MaxParts];


I should add the following:
1)I am new to C++ and the answer is probably very simple
2)I am using VC++6 with MFC
Thanks again for you help
I guess there are two parts to my question.
1)How can I load a bunch of data blocks from a file into a memory buffer. I know how to read this data. I am currently reading into a CString. But I rather read it into an array of chars. But since I have limited memory I wanted to use a dynamic array, like Carray which can grow, but I don’t know if that makes sense.

2) Assuming I have read the data into a memory buffer as per jkr’s example,  so all my data is now in an array of chars pBuf, how do can I load my array of parts, bellow, given the location of the first part and the number of parts. For example: the first part record starts at location 102 and there are 20 parts. The parts records are stored sequentially in pBuf.

struct  part
{
int modelNum;
int  partNum;
float cost;
};


part* Parts[MaxParts];


I should add the following:
1)I am new to C++ and the answer is probably very simple
2)I am using VC++6 with MFC
Thanks again for you help
I was finally able to get this working. THanks for all your help. What I ended up doing is loading all the data block in a memory buffer and then use memcpy do copy the data into a structured array.

char* dataBuff;
dataBuff = new char[myFile.GetLength()];
for (int n = 0; n < nPartsCount; n++){
  memcpy(*Parts[n],dataBuff,sizeof(part));
}