Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pointer and "New" STL Objects

31 views
Skip to first unread message

MikeCopeland

unread,
Mar 15, 2015, 2:43:10 AM3/15/15
to
I have the following structures and declarations:
struct ResultStruct // Individual Event Finisher
{
int eventYear; // Year of result info
int eventOAll; // OverAll Finish position
int eventD_P; // Division Place
long eventTime; // Finish Time
string resEvent;
} resultWork;
typedef list<ResultStruct> ResultList;
ResultList resultList;
ResultList::iterator resIter;

struct FinisherStruct // Individual Finisher data
char finGender; // gender
int finCount; // # Finishes
long finLink; // unique Finisher (link) Id
string finName; // Finisher Name (Last, First)
string finDoB; // (derived) DoB via Age/Year
ResultList resList; // Finisher's events details
} finisherWork;
vector<FinisherStruct> finVect;
vector<FinisherStruct>::iterator flIter;

I populate the data like this:

finisherWork.finName = "Washington, George";
finisherWork.finDoB = 1940;
finisherWork.finLink = 17;
finisherWork.finCount = 0;
finisherWork.resList.clear(); // !!! Problem on reuse!!!
...
resultWork.eventTime = 3456;
resultWork.eventD_P = 3;
resultWork.eventOAll = 27;
resultWork.eventYear = 2010;
resultWork.resEvent = "Boston Marathon";
finisherWork.finCount++;
finisherWork.resList.push_back(resultWork);

I have discovered that the data in "resList" is incorrect when I
populate the object with different data...because reusing this data
value, even though I "clear()" it, is using the same memory address over
and over. I realize that I must use a pointer here and reallocate it,
but I don't know how to do it (e.g. "new something" and a redefinition
of "resList").
IOW, I must define the "ResultList resList" so that I can allocate
new memory data each time I populate the data object. Please advise.
TIA



---
This email has been checked for viruses by Avast antivirus software.
http://www.avast.com

Paavo Helde

unread,
Mar 15, 2015, 3:37:01 AM3/15/15
to
MikeCopeland <mrc...@cox.net> wrote in
news:MPG.2f6ed7daf...@news.eternal-september.org:
It looks like in your program you have only a single FinisherStruct
object (finisherWork) and thus there is only a single
finisherWork.resList object as well. When you modify this object, of
course it is modifying the same object in place. If you want multiple
objects, you need to create multiple objects.

I suggest first ditch the confusing C-style object definitions with
struct definitions, i.e., just have declarations:

struct ResultStruct // Individual Event Finisher
{
int eventYear; // Year of result info
int eventOAll; // OverAll Finish position
int eventD_P; // Division Place
long eventTime; // Finish Time
string resEvent;
};
typedef list<ResultStruct> ResultList;
struct FinisherStruct // Individual Finisher data
char finGender; // gender
int finCount; // # Finishes
long finLink; // unique Finisher (link) Id
string finName; // Finisher Name (Last, First)
string finDoB; // (derived) DoB via Age/Year
ResultList resList; // Finisher's events details
};

Now when you have got rid of the idea that there must be single "master"
object of each struct type, you can easily construct as many of them as
you need and wherever you need:

FinisherStruct finisherWork1, finisherWork2, finisherWork3;

or

size_t N = 100;
std::vector<FinisherStruct> finisherWorkList(N);

// as a demo, add some objects to the lists
ResultStruct a, b;

a.eventTime = 3456;
a.eventD_P = 3;
a.eventOAll = 27;
a.eventYear = 2010;
a.resEvent = "Boston Marathon";

b.eventTime = 3457;
b.eventD_P = 4;
b.eventOAll = 28;
b.eventYear = 2011;
b.resEvent = "Shanghai Marathon";

finisherWorkList[0].resList.push_back(a);
finisherWorkList[1].resList.push_back(b);

// etc...

Probably I misunderstood your actual problem, but getting rid of the C-
style struct variable definitions together with the struct declarations
will make your code better nevertheless.

hth
Paavo

MikeCopeland

unread,
Mar 15, 2015, 8:39:42 AM3/15/15
to
In article <XnsA45E61C9B889Em...@216.196.109.131>,
myfir...@osa.pri.ee says...
I don't know how large "N" is. That's why I chose a vector to hold
as many as I might encounter.
>
> // as a demo, add some objects to the lists
> ResultStruct a, b;
>
> a.eventTime = 3456;
> a.eventD_P = 3;
> a.eventOAll = 27;
> a.eventYear = 2010;
> a.resEvent = "Boston Marathon";
>

Yes, I see that. (And I apologize for the convoluted example code -
I should have developed a simpler problem statement....)
However, I can't do what you suggest, because I don't know how many
objects I need to manage. I have a large set of input data (in a file)
that has an unknown number of data objects I must present for a user's
selection. It could be 5, 12, or more than 50 sets of information, and
I must "hold" the data sets somewhere before processing.
I realize I should change the idea of the "master set", and that's
why I want to (somehow) allocate what I need with a "new" and pointers.
I just can't figure out how to write the C++ code that does that:
e.g. ResultList *resList;
How do I allocate and use such a thing (since I can't declare how many
objects I may actually need? 8<{{

Paavo Helde

unread,
Mar 15, 2015, 4:54:39 PM3/15/15
to
MikeCopeland <mrc...@cox.net> wrote in
news:MPG.2f6f2b6b9...@news.eternal-september.org:
> However, I can't do what you suggest, because I don't know how many
> objects I need to manage. I have a large set of input data (in a
> file) that has an unknown number of data objects I must present for a
> user's selection. It could be 5, 12, or more than 50 sets of
> information, and I must "hold" the data sets somewhere before
> processing.

All STL containers like std::vector and std::list can hold a changing
number of objects (they have push_back() member functions for example). My
suggestions do not have anything to do with fixed number of objects, I just
used a vector of some non-zero initial size to simplify the example. If you
are thinking my proposal was somehow related to use a container of a fixed
size, then I'm afraid you did not understand my post at all.

hth
Paavo

0 new messages