?
Solved

dynamic memory buffer

Posted on 2002-03-12
8
Medium Priority
?
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 600 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

765 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