Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Items of my array/buffer were overwritten

Status
Not open for further replies.

linden99

Programmer
May 25, 2004
5
DE
Hello,

I am quite a C++ newbie and arrived at a point of desperation...

First of all I receive Data from the serial interface, 6 different data sets which are sent from an external device. I split those data sets and put them into classes, furthermore I designed one class "Data" which contains one instance of each of those 6 classes

Then I've written a class Buf, which is an circular buffer (FIFO). I want to store 50 objects of the class Data. I fill a Object of the class Data, and when it's full, I want to append it to my Buf. That works, but when the object of the class Data is filled again with the next package of data sets, the actual data set overwrites the already appended object of my Buf - I gain every time the same data in my Buf.

I'm sure that I made supposably just a little mistake, which has this great consequence, but by now I'm quite mixed and haven't got any idea left what to do.

I'm really greatful for any kind of help and posted some of my code - hope that somebody out there could find my fault...

Thanks a lot for your thoughts,

linden99

//class Buf (circular buffer)
# define BUFSIZE 50
class Buf
{
public:
Buf();
virtual ~Buf();
bool Append(void* newthing);
void *First(void);
void *Last(void);
int Length(void) const;
int Full(void) const;
int Empty(void) const;


private:
void *data[BUFSIZE];
int bufapp;
int bufget;
int bufcount;

};

inline int Buf::Length(void) const { return bufcount; }
inline int Buf::Full(void) const { return bufcount == BUFSIZE; }
inline int Buf::Empty(void) const { return bufcount == 0; }


bool Buf::Append(void *newthing)
{
if (bufcount {
printf("%d", bufapp);
data[bufapp] = newthing;
bufapp++;
if(bufapp == BUFSIZE)
bufapp = 0;
bufcount++;
return (true);
}
else
return (false);
}

void *Buf::First(void)
{
void* temp = data[bufget];
bufget++;
if(bufget == BUFSIZE)
bufget = 0;
bufcount--;
return temp;
}

void *Buf::Last(void)
{
void *temp=NULL;
if (bufapp>0)
{
temp = data[bufapp-1];
return temp;
}
else
return temp;
}

///////////////////////////////////
// part of main()
////////////////////////////////
Buf buffer;
Data *newitem=NULL;
Data *lastitem=NULL;
Data *appitem=NULL;
int lasttime;
int newtime;
char lastmin[5];
char newmin[5];
//if all data sets are completely filled
if (bgga==true && bgsa==true && bgsv==true && brmc==true && brme==true && brmm==true)
{
appitem=newitem;
int len=buffer.Length();
if (len>0)
{
lastitem=new Data();
lastitem=(Data*) buffer.Last();
midstr(lastmin, lastitem->gga.getggautc(), 2, 2);//cuts the minutes out of the string
midstr(newmin, newitem->gga.getggautc(), 2, 2);
newitem=NULL;
newitem=new Data();

lasttime=atoi(lastmin);
newtime=atoi(newmin);

switch(lasttime)
{
case(59): //just in case one hour was transgressed
if (newtime==0)
{
if(buffer.Append(appitem))
printf("\n\n\ok!\n\n\n");
else
printf("\n\n\not ok!\n\n\n");
}
default:
if (newtime==lasttime+1)
{
if(buffer.Append(appitem))
printf("\n\n\ok!\n\n\n");
else
printf("\n\n\not ok!\n\n\n");
}
} // end switch

}// End if len>0
else //if there hasn*t been any data inside the circular buffer yet, it has to be appended
{
if(buffer.Append(appitem))
printf("\n\n\od!\n\n\n");
else
printf("\n\n\n not ok!\n\n\n");
appitem=NULL;

newitem=NULL;
newitem=new Data();

}
bgga=false;
bgsa=false;
bgsv=false;
brmc=false;
brme=false;
brmm=false;
}
 
Please use the [ignore]
Code:
[/ignore] tags

Your code
int main ( ) {
printf("hello");
return 0;
}

Formatted code
Code:
int main ( ) {
    printf("hello");
    return 0;
}


--
 
Sorry for not having used those code tags, but I am
a) new
b) able to learn ;-)

Once again:

Hello,

I am quite a C++ newbie and arrived at a point of desperation...

First of all I receive Data from the serial interface, 6 different data sets which are sent from an external device. I split those data sets and put them into classes, furthermore I designed one class "Data" which contains one instance of each of those 6 classes

Then I've written a class Buf, which is an circular buffer (FIFO). I want to store 50 objects of the class Data. I fill a Object of the class Data, and when it's full, I want to append it to my Buf. That works, but when the object of the class Data is filled again with the next package of data sets, the actual data set overwrites the already appended object of my Buf - I gain every time the same data in my Buf.

I'm sure that I made supposably just a little mistake, which has this great consequence, but by now I'm quite mixed and haven't got any idea left what to do.

I'm really greatful for any kind of help and posted some of my code - hope that somebody out there could find my fault...

Thanks a lot for your thoughts,

linden99
Code:
//class Buf (circular buffer) 
# define BUFSIZE 50 
class Buf 
{ 
   public: 
   Buf(); 
   virtual ~Buf(); 
   bool Append(void* newthing); 
   void *First(void); 
   void *Last(void); 
   int Length(void) const; 
   int Full(void) const; 
   int Empty(void) const; 


private: 
   void *data[BUFSIZE]; 
   int bufapp; 
   int bufget; 
   int bufcount; 
}; 

inline int Buf::Length(void) const { return bufcount; } 
inline int Buf::Full(void) const { return bufcount == BUFSIZE; } 
inline int Buf::Empty(void) const { return bufcount == 0; } 


bool Buf::Append(void *newthing) 
{ 
   if (bufcount 
   { 
      data[bufapp] = newthing; 
      bufapp++; 
      if(bufapp == BUFSIZE) 
         bufapp = 0; 
      bufcount++; 
      return (true); 
   } 
   else 
      return (false); 
} 

void *Buf::First(void) 
{ 
   void* temp = data[bufget]; 
   bufget++; 
   if(bufget == BUFSIZE) 
      bufget = 0; 
   bufcount--; 
   return temp; 
} 

void *Buf::Last(void)
{
   void *temp=NULL;
   if (bufapp>0)
   {
      temp = data[bufapp-1];
      return temp; 
   }
   else 
      return temp;
}

///////////////////////////////////
// part of main()
////////////////////////////////
Buf buffer;
Data *newitem=NULL;
Data *lastitem=NULL;
Data *appitem=NULL; 
int lasttime;
int newtime;
char lastmin[5];
char newmin[5];
//if all data sets are completely filled
if (bgga==true && bgsa==true && bgsv==true && brmc==true && brme==true && brmm==true) 
{ 
   appitem=newitem; 
   int len=buffer.Length(); 
   if (len>0) 
   { 
      lastitem=new Data(); 
      lastitem=(Data*) buffer.Last(); 
      midstr(lastmin, lastitem->gga.getggautc(), 2, 2);//cuts the minutes out of the string
      midstr(newmin, newitem->gga.getggautc(), 2, 2); 
      newitem=NULL; 
      newitem=new Data(); 

      lasttime=atoi(lastmin); 
      newtime=atoi(newmin); 

      switch(lasttime) 
      { 
      case(59): //just in case one hour was transgressed
         if (newtime==0) 
         { 
            if(buffer.Append(appitem)) 
                printf("\n\n\ok!\n\n\n"); 
            else 
               printf("\n\n\not ok!\n\n\n"); 
         } 
         default: 
            if (newtime==lasttime+1) 
            { 
               if(buffer.Append(appitem)) 
                  printf("\n\n\ok!\n\n\n"); 
               else 
                  printf("\n\n\not ok!\n\n\n"); 
            } 
      } // end switch 

}// End if len>0 
else //if there hasn*t been any data inside the circular buffer yet, it has to be appended
{ 
   if(buffer.Append(appitem)) 
      printf("\n\n\od!\n\n\n"); 
   else 
      printf("\n\n\n not ok!\n\n\n"); 
   appitem=NULL; 

   newitem=NULL; 
   newitem=new Data(); 

}
bgga=false; 
bgsa=false; 
bgsv=false; 
brmc=false; 
brme=false; 
brmm=false; 
}
 
You have many problems in your code:
- you want to store 50 Data objects in what member or pointer of Buf? Your buf->data member is only 50 bytes long. You will be able to store at most one object in it.
- if you know the number of objects you shoul have a meber of type theBuffer[x] where x = number of objects* sizeof(object)
- if you do not know it you have to dinamically alocate it in constructor adn dealocate it in destructor with new/delete and keep an member of the Buf class like Data* pTheBuffer

I think you now understand why your data gets overwritten.
You have to think og buf as a zone of mwemory large enough to store ALL you 50 or more instances of data.

HTH,

s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top