RE: [Dev-C++] dynamic allocation and structs
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2002-10-25 08:50:59
|
The trick is that vector isn't programming. It is a standard - and very old - mathematical term for a one-dimensional matrix. There has been millions of vector implementations before STL did a template version, and the probability of a finding a STL construct in a C program is not that high :-) While code might syntactically switch around between (*a).x, a->x a[0].x the third form is definitely dangerous to use when a is only a pointer to a single element. If there is an element zero, the logical conclusion is that there must be a element one. While one goal of programming is to make the programs work, it shouldn't be neglected that comming generations of programmers inherriting old code does immediately see what the code does. When seeing a x[0] statement, the natural reaction is - or should be - to start hunt for the total size of the vector. /Per W On Fri, 25 Oct 2002, Ioannis Vranos wrote: > > -----Original Message----- > > From: Per Westermark [mailto:pw...@ia...] > > Sent: Friday, October 25, 2002 10:53 AM > > To: Ioannis Vranos > > Cc: Dev-C++ Mailing List > > Subject: RE: [Dev-C++] dynamic allocation and structs > > > > > > The term vector is valid, since the allocated information is > > only supposed > > to be used with one single index, i.e. one-dimensional. > > > I said avoid using the term "vector" for built-in arrays so as to not > confuse them with C++'s standard library vectors. Call them "built in > arrays". > > > > > It must always be observed - yes this is outside scope of Dev-C++ but > > standard C/C++ requirements - that a while a pointer might > > point to the > > forst element of a vector, i.e. > > mystruct *p; > > p[0].x = 1; > > p[1].x = 2; > > ... > > it is not required or guaranteed that there are more than one > > element the > > pointer is pointing at. Never assume a p[1] just because p[0] is fine. > > The code below uses vector[i]->id, not vector[i][0].id, to > > signify that > > this is a one-dimensional allocation, and that while the > > compiler would > > accept vector[i][0], vector[i][1], vector[i][1] would point outside > > allocated memory. > > > i think the second [] swits better notationally. It's a personal taste. > > > > Ioannis > > * Ioannis Vranos > * Programming pages: http://www.noicys.freeurl.com > * Alternative URL: http://run.to/noicys > > > ------------------------------------------------------- > This sf.net email is sponsored by: Influence the future > of Java(TM) technology. Join the Java Community > Process(SM) (JCP(SM)) program now. > http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0004en > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |