![]() |
| | #1 |
| Registered User Join Date: Sep 2008
Posts: 13
| C++ newbie question about pointers I would like to compare these 2 different types of instantiation: Code: Item* _queue; _queue= new Item[size]; Code: Item** _queue; _queue = new Item*[size]; Supposing that queue is going to receive data in form of Item objects, which one is the most correct form I should choose? An array of Items as in the first case or an array of pointers to Item? Maybe I'm just a bit confused but I really can't solve this |
| lafayette is offline | |
| | #2 | |
| The larch Join Date: May 2006
Posts: 3,222
| One allocates size Items, the other allocates size pointers to Item. In practice you should rarely need the array form of new[] (since there are standard containers for that), and it might also be better not to start names with underscore (may be reserved for compiler implementation).
__________________ I might be wrong. Quote:
| |
| anon is offline | |
| | #3 |
| Registered User Join Date: Sep 2008
Posts: 13
| perfect. So the second notation should also be corrected using "new[]" rather than "new"? |
| lafayette is offline | |
| | #4 | |
| The larch Join Date: May 2006
Posts: 3,222
| In the second case, I'd assume that you want a dynamically allocated "two-dimensional array", e.g: Code: Item** items = new Item*[3]; items[0] = new Item[10]; items[1] = new Item[20]; items[2] = new Item[30]; So if you want to store a bunch of Items, use the first. (The second is for a bunch of bunches of Items.)
__________________ I might be wrong. Quote:
| |
| anon is offline | |
| | #5 |
| Registered User Join Date: Sep 2008
Posts: 13
| very very clear, I think I've understood now thank you so much |
| lafayette is offline | |
| | #6 |
| (?<!re)tired Join Date: May 2006 Location: Portugal
Posts: 5,661
| As a final note, avoid creating objects starting with an underscore. The compiler usually requests those for internal use and you may conflict with existing names. Your example may be fine with an underscore followed by a lowercase letter, as you have above, when in local scope (inside a function or class definition) or under a namespace. But you shouldn't declare it in the global namespace, Meanwhile a name started with an underscore followed by a capital letter, or two underscores, is to be avoided anywhere in your code. Sorry for the off-topic.
__________________ Originally Posted by brewbuck: Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster. |
| Mario F. is offline | |
| | #7 |
| Registered User Join Date: Sep 2008
Posts: 13
| ok, even if a little OT (not so much I think) thank you because I think these little suggestions are pretty important, mostly when starting writing something |
| lafayette is offline | |
| | #8 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,372
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #9 |
| Registered User Join Date: Jan 2005
Posts: 7,252
| >> I think these little suggestions are pretty important, mostly when starting writing something Another suggestion alluded to by anon that I think is most important here is that you shouldn't use new[]/delete[] for this. You should be using the standard vector class for dynamically sized arrays. They are safer, more robust and easier to get right than the arrays you are going to use. I'd highly recommend taking a little bit of time to investigate vectors and use them in your program instead of this type of array. |
| Daved is offline | |
| | #10 | |
| Registered User Join Date: Sep 2008
Posts: 13
| Quote:
| |
| lafayette is offline | |
| | #11 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,785
| std::vector saves you the trouble to do this yourself. It's tested, it works and it's reliable. And never use malloc in C++ unless you have a very, very good reason. There is very little use for it in C++.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #12 |
| Registered User Join Date: Jan 2005
Posts: 7,252
| malloc won't call the constructor for the Item, which is a bad thing in C++. As to whether you can have a safe/bug free implementation with variant #1, sure it's possible. It's just much less likely and much more work than using a vector. For example, you have to remember to delete[] the array when you are done using the data, or the memory will leak and destructors won't be called. If you use vector, that will be done automatically (you just have to make sure the vector object is created in a place where it will not go out of scope until you are done). If you're using the array in a class, then vector makes it easier because it handles copying for you. When you use new[]/delete[], you have write a copy constructor and copy assignment operator in addition to the destructor to make sure that you delete the memory once and only once (otherwise you will often see crashes). Those are just a couple examples, there are other advantages. I have yet to hear any disadvantages to using vector over new[] except when instructors in classes require that you use the old method. |
| Daved is offline | |
| | #13 |
| Registered User Join Date: Sep 2008
Posts: 13
| ok, I think I have the whole question more clear now. Just as a final example, based on what you said in this discussion: declaring a class attribute as, for example: Code: char* member; Code: member = "string"; |
| lafayette is offline | |
| | #14 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,785
| No, string literals are const char*, so it is perfectly fine to assign them like that. But you cannot change the contents of the pointer. Defining them as const fixes that, because it prevents you from changing it. This is a common error and I'm going to link to a faq that says why. http://cpwiki.sourceforge.net/Common...kes_and_errors
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #15 |
| Registered User Join Date: Jan 2005
Posts: 7,252
| You should be using the C++ string class for many of the same reasons as I gave for using vector. There is no danger of assigning to a const string literal, or failing to use strcpy with strings. They also work much more intuitively. The =, + and == operators all work like you would expect, which is not how C style strings work. If you assign "string" to member instead of copy it with strcpy, then you will have problems if you try to change it later. Even if you use strcpy, you would need to allocate space for the string, which goes back to the same problems as an array with new[]. In this case the string class would be a better alternative than vector, since it is meant specifically for strings. |
| Daved is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC | tvsinesperanto | C Programming | 34 | 03-11-2006 12:13 PM |
| Question about the introduction to pointers lesson | Freemind11 | C++ Programming | 11 | 02-15-2006 11:45 PM |
| Question on pointers? | robasc | C++ Programming | 16 | 12-25-2005 01:15 AM |
| Quick Question Regarding Pointers | charash | C++ Programming | 4 | 05-04-2002 11:04 AM |
| very newbie question: how to copy files | webwesen | C Programming | 26 | 04-25-2002 03:01 PM |