Solved

dynamic memory buffer

Posted on 2002-03-12
8
235 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:el_rooky
8 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 6857734
>>I have limited memory and i don't know how long the
>>array is going to be

Read the file's length beforehand:

#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>

void main( void )
{
   struct _stat buf;
   int result;
   char* pBuf;

   /* Get data associated with "stat.c": */
   result = _stat( "file.bin", &buf );

   /* Check if statistics are valid: */
   if( result != 0 )
      perror( "Problem getting information" );
   else
   {
      pBuf = new char [ buf.st_size];

      // read bin file
   }
}

0
 

Author Comment

by:el_rooky
ID: 6857899
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
0
 
LVL 86

Expert Comment

by:jkr
ID: 6857924
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;
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6857932
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:lidorc
ID: 6859745
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.
0
 

Author Comment

by:el_rooky
ID: 6860917
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
0
 

Author Comment

by:el_rooky
ID: 6861069
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
0
 

Author Comment

by:el_rooky
ID: 6921336
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));
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now