- What problem does this self-assignment function have? - 3 Updates
- error compiling simple threading example... - 1 Update
- Want to readout the control file of aria2. - 1 Update
- size_t - 1 Update
- Why does this generate an error (Debug Assertion Failed) - 3 Updates
- Why won't this compile... - 3 Updates
- 'class' and 'struct' at link time - 1 Update
- decimal - 1 Update
- Help on 'delete[] data' in an example - 1 Update
fl <rxjwg98@gmail.com>: Jun 09 02:44PM -0700 Hi, There is an on-line post, which says that following code fails in case of self-assignment. I run it on MSVC, but do not find any failure yet. I have tried n=0 and n=10 two cases. What is wrong with my comprehension on this code? Thanks, //////////// class array { int tmp; int *data; size_t n; public: array(){ n=0; data=new int[n]; } array &array::operator =(const array &rhs); }; array &array::operator =(const array &rhs) { delete[] data; n = rhs.n; data = new int[n]; std::copy_n(rhs.data, n, data); return *this; } int main () { array a0; array b0; a0=a0; return 0; } |
Louis Krupp <lkrupp@nospam.pssw.com.invalid>: Jun 09 04:11PM -0600 >array &array::operator =(const array &rhs) >{ > delete[] data; At this point, you can't access anything data points to, so you might as well set it to, say, the null pointer: data = 0; Try self- assignment now, and see what happens. > a0=a0; > return 0; >} Louis |
fl <rxjwg98@gmail.com>: Jun 09 03:49PM -0700 On Tuesday, June 9, 2015 at 3:11:12 PM UTC-7, Louis Krupp wrote: > > return 0; > >} > Louis I find the failure by adding the following member function: void array::array_set(int tmp, int number){ data[number]=tmp; } int main() { a0.array_set(3, 2); a0=a0; } Then, a0.data array cannot keep its original value after self-assignment. I am still new to C++. I noticed that data is its private data, and don't know whether your insertion data = 0; is inside the member function or not. If you could give me more detail, that is better to know other view of the problem. Thanks, |
"Chris M. Thomasson" <nospam@nospam.nospam>: Jun 09 03:41PM -0700 > "Doug Mika" wrote in message > news:3d6437f0-f89e-4dcb-af75-82358e32fea4@googlegroups.com... [...] > Aborted (core dumped) > Which begs the question, where is the setting to enable threading, > and is there a flag that is needed in MinGW to enable threading? Try using the following: g++ -std=c++11 -pthread -o main *.cpp witch compiles your example. It runs fine with: http://www.tutorialspoint.com/compile_cpp11_online.php ;^) |
legalize+jeeves@mail.xmission.com (Richard): Jun 09 10:37PM [Please do not mail me a copy of your followup] Victor Bazarov <v.bazarov@comcast.invalid> spake the secret code >> If you want people to do your work for them, it is customary to pay them. >. ^^^^^^^^^^ >I think you meant "for you". ;-) Yeah, saw that after I posted, but cancelling an article on usenet was never well supported when usenet was well maintained and it's virtually impossible now. So I simply let the mistakes fly and if someone is confused by what I said, I correct it. But yes, you're correct in correcting me :-). The bottom line is that it's generally considered rude to show up in a forum and ask other people to do your work. See <http://www.catb.org/esr/faqs/smart-questions.html> for general behavior guidelines on asking questions. -- "The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline> The Computer Graphics Museum <http://computergraphicsmuseum.org> The Terminals Wiki <http://terminals.classiccmp.org> Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com> |
legalize+jeeves@mail.xmission.com (Richard): Jun 09 10:31PM [Please do not mail me a copy of your followup] slp53@pacbell.net spake the secret code >You know, C is still a subset of C++; and it always will be, purists >like yourself notwithstanding. Not really. nullptr is not a keyword in C, but it is a keyword in C++. We're talking about POSIX, which is neither a part of the C standard nor is it part of the C++ standard, so the whole idea of "C being a subset of C++" is irrelevant. -- "The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline> The Computer Graphics Museum <http://computergraphicsmuseum.org> The Terminals Wiki <http://terminals.classiccmp.org> Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com> |
fl <rxjwg98@gmail.com>: Jun 09 01:59PM -0700 Hi, When I try to run a snippet code from the web, it has a Debug Assertion Failed error in MSVC. Specifically, when it is stepped into b0=a0; it jumps to the assign function array &array::operator =(const array &rhs); At line: delete[] data; the Debug Assertion Error pops. I do not understand why it has such an error. Could you explain it to me? Thanks, ///////////// class array { int tmp; int *data; size_t n; public: array(){ data=&tmp; } array &array::operator =(const array &rhs); }; array &array::operator =(const array &rhs) { delete[] data; n = rhs.n; data = new int[n]; std::copy_n(rhs.data, n, data); return *this; } int main () { array a0; array b0; b0=a0; return 0; } |
fl <rxjwg98@gmail.com>: Jun 09 02:12PM -0700 On Tuesday, June 9, 2015 at 1:59:37 PM UTC-7, fl wrote: > b0=a0; > return 0; > } The original code snippet does not have definition int tmp; inside class array. I added it to make delete[] data; work. It is possible that data shouldn't be used in such a way. Then, how to use data in the class array? Thanks, |
fl <rxjwg98@gmail.com>: Jun 09 02:22PM -0700 On Tuesday, June 9, 2015 at 2:12:13 PM UTC-7, fl wrote: > It is possible that data shouldn't be used in such a way. Then, how to use > data in the class array? > Thanks, Oh, I find the answer. The constructor can be: public: array(){ n=0; data=new int[n]; } Thanks, |
Paavo Helde <myfirstname@osa.pri.ee>: Jun 09 02:57PM -0500 Doug Mika <dougmmika@gmail.com> wrote in > So what would be the rule, when do I need typename C and when do I > just write C? It should be obvious that inside the <> brackets is > always a type, what else could it be? The last typename above applies to 'iterator', not to 'C'. The compiler knows C is a type, but it does not have such knowledge about 'C::iterator'. hth |
Doug Mika <dougmmika@gmail.com>: Jun 09 01:05PM -0700 On Tuesday, June 9, 2015 at 2:57:20 PM UTC-5, Paavo Helde wrote: > The last typename above applies to 'iterator', not to 'C'. The compiler > knows C is a type, but it does not have such knowledge about 'C::iterator'. > hth Yes, I sort of figured that, it is nice however that someone confirms it. (It's not explicitly written in my book) Thanks |
Marcel Mueller <news.5.maazl@spamgourmet.org>: Jun 09 10:51PM +0200 On 09.06.15 19.55, Doug Mika wrote: > The following for example will not work: (but why do we need typename C here and just C previously?) > //this won't compile, "typename" is needed, but why is it needed here but > //not previously? template<typename C> vector<C> create_container(int size) ... Here C is replaced by the individual type at instantiation of the template. C is always a type name because this is restricted in the template pattern, maybe C evaluates to 'typename myclass'. template<typename C, typename V> vector<typename C::iterator> find_all(C& c, V v) This is a different story. The 'typename' applies to the nested iterator type rather than to C itself. And 'iterator' could be anything within the type C. Marcel |
Chris Vine <chris@cvine--nospam--.freeserve.co.uk>: Jun 09 08:52PM +0100 On Tue, 09 Jun 2015 20:26:57 +0200 > class (Clause 9) declared using the class or struct class-key. " > should tell us that, unlike enum and union, "either [...] class or > struct" can be used to refer to a non-union class. I have come across this outside VS when specializing std::hash for a type which might be used as the key for an unordered container. The standard does not specify whether the std::hash<T> class template type is defined using the 'class' or 'struct' keyword. §20.8.12 of the C++11 standard uses the 'struct' keyword when specifying the specializations required by the standard, but only requires that any given specialization must comprise a 'class template'. clang exhibits the annoying behaviour of emitting a bogus warning if you provide a hash specialization using a different keyword from the one which happens to be used by the C++ standard library in question for std::hash<T>, which means that if you use clang with a recent version of libstdc++ you get the warning when using the 'struct' keyword instead of the 'class' keyword for a specialization. I am glad to say that gcc does not do this. Chris |
drew@furrfu.invalid (Drew Lawson): Jun 09 03:38PM In article <ml4pfp$4ne$1@dont-email.me> >1) Getting data from a database that represents money >2) Comes into C++ as a string >3) Convert it double to do calculations There is your problem. Never use floating point for money. In addition to the practical problems you've seen, it is a conceptual error. Monetary systems have discrete minimum values (frequently a mil) and rules about how they are treated. This is the sort of problem that is recreated every time programmers try to replace centuries of accounting practices. -- |Drew Lawson | Of all the things I've lost | | | I miss my mind the most | |
woodbrian77@gmail.com: Jun 09 08:06AM -0700 On Tuesday, June 9, 2015 at 12:01:00 AM UTC-5, Paavo Helde wrote: > Beware that in real life the array-style new[] and delete[] have about zero > usage cases. These come up almost only in silly learning examples like > this. I use them in my code - http://webEbenezer.net/build_integration.html Brian Ebenezer Enterprises - "Free at last; free at last. Thank G-d Almighty we are free at last." Martin Luther King Jr. http://webEbenezer.net |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment