Solved

dynamic memory buffer

Posted on 2002-03-12
8
234 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

746 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

11 Experts available now in Live!

Get 1:1 Help Now