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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie needs some help with filling Array...

Status
Not open for further replies.

linden99

Programmer
May 25, 2004
5
DE
Hi everybody,

I made an array of 10 objects of a class I've written, and I send one object of my array (array[index]) plus a string full of chars I received from the COM-port to a function, which cuts the string into pieces and puts those pieces into the array-object which means the members of my class. This works very fine.
I increment the index, the string gets filled again with the next data from the COM, and when I send the next object of my array (index is incremented!!) with the newly filled string to the function, the entries of my array-object on the first position got overwritten with the data of the new string, and on the next position (which should have been filled this time) appears the same data as on the first position. When I work out this process x times, my array contains x times the same (the last send in) data. How could I stop this and keep the old data??

I am very thankful for every kind of help,

linden99
 
It's too abstract description. Get us some codes, please...
 
Okay - you asked me to...
I will give you a shortened version, hope you will find it not too confusing...

Code:
#define ANZAHL 50 

Data buffer[ANZAHL];  
/* Data is my own class, it contains objects from following classes: GGA, GSV, GSA, RMC, RME, RMM */

int count=0; 

/* now follows loads of code which opens the COM and collects incoming chars to a string called msg. I check the string (has some characteristic value at the end and the beginning), and if its all right, following happens: */

buffer[count]=check_message(msg, buffer[count]); 
/* the code of this function is downside listed; it writes the data of the string into the members of my array-object*/

count++; 
} 
/* now the string msg collects the next incoming data from COM and the session starts again */

Well, that's how the main is running, and I can watch at the Debugger, that the right item of my array is taken - but unfortunately the members of the array-objects have all the same contents - always the content of the last string collected

This is the code of the function, which fills the array-object:
Code:
Data check_message(char *string, Data daten) 
{ 
         
   int result; 
   int count; 
   int anzahl; 
   char *array[30]; 
   char delimiter[]=","; 
         
   result=-1; 
   /*I'm counting the delimiter-characters "," at my string*/
   anzahl = count_char(string, ','); 

   if (anzahl!=0) 
   { 
        /*I cut the string into pieces an fill them in an array */
        count= zerlegen(string, ',', array, anzahl+1); 
        /* this function fills the members with the content of the array*/ 
        
        daten.gga = fill_gga(array, count); 
        /*now the array-object is send to the main, filled with data*/
        return daten; 
        
        } 
        else 
        { 
                
            return daten;
            /*this one returns an empty array-object - not soo nice, needs some Errorhandling sometimes*/
        } 
}


This is the code how the members of the class Data get filled:
Code:
GGA fill_gga(char **array, int count) 
{ 
        GGA g1; 
        int length; 
        int i=0; 
        int k=0; 
        int j=0; 
        char delimiter[]=","; 
        char dummy[20]; 
         
        while (i<count) 
        { 
                length=strlen(array[i]); 
                switch (i) 
                { 
                case 1: 
                        g1.setggautc(array[i]); 
                        break; 
                case 2: 
                        g1.setggalatitude(array[i]); 
                        break; 
                } 

               /* and so on - all members are filled like this */
        } 
        return g1; 
}

This was shortened code, I hope I will not frighten anybody... Millions of thanks to anybody, who read this code and had some time to spend...

Greetings,

linden99
 
Hey,

I had the same problem...Each time you load your class, you will want to use the keyword new.

ie.

myclass woo;
myclass woo = new myclass;
woo.field1 = "hoo";
array[1] = woo;
woo = new myclass;
woo.field1 = "YAY";
array[2] = woo;

Obviously this isn't going to be working code, but the idea is the same regardless of the type of array you use. Further, you may have to typecast the array when you load it.

Good luck,

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts
 
Side note on above post

-this problem that I had was in C# and that is where the example came from...you may or may not have to modify some of that...the idea I'm trying to portray is that you are overwriting the data from the class every time you change a value without making a new instance. If you have a very small number of items in the array and you are so inclined, you could just create a separate instance of the class (new variable name) for each slot of the arry...though this is a bit wasteful of memory and kills your ability to do things dynamically, if you decided you wanted to. Given your example, and the use of a while loop, you'll definitely need to say "new".

Good luck,

Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts
 
Well, in fill_gga() i = 0 and never changed...
But the most interesting thing in that case: where is class Data assignment op and copy constructor? Are they implicit (generated by default)? If so, you may have problems with this class when its objects contain dynamic generated members.
Another possible problem area: in check_message() you initialize only GGA member. The same question about GGA assignment and copy constructor...
Yet another suspicious point:
Code:
buffer[count]=check_message(msg, buffer[count]);
We have two references to the Data object - as argument and as assignment target. It seems at this moment the object is empty and we want to initialize it - what for the 2nd reference to?
I think no any serious language specific problem in your code. Explain Data and GGA = and copy ctor (especially setgga...() logic) then let's go on...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top