Solved

Problem with character strings in a delimeted buffer class

Posted on 2004-10-13
9
225 Views
Last Modified: 2012-06-21
I am working on a class that takes a person object, then packs it to a buffer and writes it to a file.  The problem is that when I try to read it back into the buffer from the file, I get a runtime error.  Here is a copy of the significant functions:

Any help would be appreciated


Here are the pack and unpack functions in my person class:


int Person::Pack (IOBuffer & Buffer) const
{// pack the fields into a FixedFieldBuffer,
 // return TRUE if all succeed, FALSE o/w
      int numBytes;
      Buffer . Clear ();
      numBytes = Buffer . Pack (FirstName);
      if (numBytes == -1) return FALSE;
      numBytes = Buffer . Pack (LastName);
      if (numBytes == -1) return FALSE;
      numBytes = Buffer . Pack (IDNumber);
      if (numBytes == -1) return FALSE;

      return TRUE;
}


int Person::Unpack (IOBuffer & Buffer)
{

      Clear ();
      int numBytes;
      numBytes = Buffer . Unpack (FirstName);
cout << numBytes;
      if (numBytes == -1) return FALSE;
      FirstName[numBytes] = 0;
      numBytes = Buffer . Unpack (LastName);
      if (numBytes == -1) return FALSE;
      LastName[numBytes] = 0;
      numBytes = Buffer . Unpack (IDNumber);
      if (numBytes == -1) return FALSE;
      IDNumber[numBytes] = 0;
      return TRUE;
}



Here is the unpack from  delimited buffer:

int DelimFieldBuffer :: Pack (const void * field, int size)
// set the value of the next field of the buffer;
// if size = -1 (default) use strlen(field) as Delim of field
// return number of bytes packed, -1 if error
{
      // look for delimeter in field!
      short len; // length of string to be packed
      if (size >= 0) len = size;
      else len = strlen ((char*)field);
      if (len > (short)strlen((char*)field)) // field is too short!
            return -1;
      int start = NextByte; // first character to be packed
      NextByte += len + 1;
      if (NextByte > MaxBytes) return -1;
      memcpy (&Buffer[start], field, len);
      Buffer [start+len] = Delim; // add delimeter
      BufferSize = NextByte;
      return len;
}

int DelimFieldBuffer :: Unpack (void * field, int maxBytes)
// extract the value of the next field of the buffer
// return the number of bytes extracted, -1 if error
{
      
      int len = -1; // length of packed string
      int start = NextByte; // first character to be unpacked
      for (int i = start; i < BufferSize; i++)
            if (Buffer[i] == Delim)
                  {len = i - start; break;}
      if (len == -1) return -1; // delimeter not found
      NextByte += len + 1;
      if (NextByte > BufferSize) return -1;
      // check for maxBytes

      memcpy (field,&Buffer[start] , len);      
      if (maxBytes > len || maxBytes == -1)
  ((char*)field) [len] = 0; // zero termination for string
      return len;
}



and here is the test:



      // Test writing
      //Buff . Print (cout);
      ofstream outfile (myfile,ios::out);
      result = Buff . WriteHeader (outfile);
      cout << "write header "<<result<<endl;
      person1 . Pack (Buff);
      //Buff . Print (cout);
      recaddr1 = Buff . Write (outfile);
      cout << "write at "<<recaddr1<<endl;
      person2. Pack (Buff);
      //Buff . Print (cout);
      recaddr2 = Buff . Write (outfile);
      cout << "write at "<<recaddr2<<endl;
      person3. Pack (Buff);
      //Buff . Print (cout);
      recaddr3 = Buff . Write (outfile);
      cout << "write at "<<recaddr3<<endl;
      outfile . close ();

      // test reading

      ifstream infile (myfile, ios::in);
      result = Buff . ReadHeader (infile);

      cout <<"read header "<<result<<endl;

Buff . DRead (infile,recaddr3);
      person . Unpack (Buff);
      person . Print (cout, "First record:");
      Buff . DRead (infile, recaddrw);
      person . Unpack (Buff);
      person . Print (cout, "Second record:");      
0
Comment
Question by:Tim_Heldberg
  • 4
  • 3
  • 2
9 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 12306253
What is the runtime error and at which line does it occur?

From the code snippets it is not clear ie how long Buffer is. There might be a write out of bounds in Pack.
If Buffer in Unpack is a file buffer why can you be sure that no record will start in one buffer but end in another?
0
 
LVL 86

Expert Comment

by:jkr
ID: 12308326
Have you tried running that under a debugger?
0
 
LVL 4

Author Comment

by:Tim_Heldberg
ID: 12321991
Here is where it is first crashing

    memcpy (field,&Buffer[start] , len);  in the unpack function
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 86

Expert Comment

by:jkr
ID: 12322073
OK, then let's take a look at that code:

    int len = -1; // length of packed string
    int start = NextByte; // first character to be unpacked  <---- where does 'NextByte' come from?
    for (int i = start; i < BufferSize; i++)
         if (Buffer[i] == Delim)
               {len = i - start; break;}
    if (len == -1) return -1; // delimeter not found
    NextByte += len + 1;
    if (NextByte > BufferSize) return -1;
    // check for maxBytes

    memcpy (field,&Buffer[start] , len);    // what are len and start at this point? What is 'field' pointing to?

0
 
LVL 4

Author Comment

by:Tim_Heldberg
ID: 12326297
NextByte is the index of the next byte to be unpacked

len is three, which is the length of the next field to be unpacked
and field is pointing to the next person object field to be unpacked into
0
 
LVL 86

Expert Comment

by:jkr
ID: 12329204
>> NextByte is the index of the next byte to be unpacked

Yes, but is it a member, a globa or a static variable? And, is it valid at that moment?
0
 
LVL 4

Author Comment

by:Tim_Heldberg
ID: 12329290
NextByte is an inheirited member from an iobuffer class that is used for different types of buffers, at this point it is four
0
 
LVL 13

Expert Comment

by:SteH
ID: 12336781
And what is the value of BufferSize. And how long is Buffer? (I guess BufferSize) Can you post the address of field and Buffer.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 12341641
Actually, it'd be more helpful to post the whole code so one can run it using a debugger :o)
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

810 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