C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-30-2008, 05:53 AM   #1
Registered User
 
Join Date: Sep 2008
Posts: 13
C++ newbie question about pointers

hi all, and I ask pardon for this question (I've read the FAQ about pointers) but still can't get rid of this doubt.

I would like to compare these 2 different types of instantiation:

Code:
Item* _queue;
_queue= new Item[size];
And:

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   Reply With Quote
Old 09-30-2008, 06:54 AM   #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:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Quoted more than 1000 times (I hope).
anon is offline   Reply With Quote
Old 09-30-2008, 07:15 AM   #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   Reply With Quote
Old 09-30-2008, 07:21 AM   #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];
Again, in real code you should practically never have to use something like that (instead you could have vector<vector<Item> >.

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:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Quoted more than 1000 times (I hope).
anon is offline   Reply With Quote
Old 09-30-2008, 07:46 AM   #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   Reply With Quote
Old 09-30-2008, 07:55 AM   #6
(?<!re)tired
 
Mario F.'s Avatar
 
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   Reply With Quote
Old 09-30-2008, 08:30 AM   #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   Reply With Quote
Old 09-30-2008, 09:37 AM   #8
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,372
Quote:
Meanwhile a name started with an underscore followed by a capital letter, or two underscores, is to be avoided anywhere in your code.
Do not only avoid giving names that start with two underscores, but also avoid those names that contain two consecutive underscores.
__________________
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   Reply With Quote
Old 09-30-2008, 10:59 AM   #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   Reply With Quote
Old 09-30-2008, 11:46 AM   #10
Registered User
 
Join Date: Sep 2008
Posts: 13
Quote:
Originally Posted by Daved View Post
>> 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.
I've read something about std::vector, but I thought my implementation (now changed to the variant #1, could be safe and almost bug-free. Or should I call a malloc() for every array item, with the size of the structure? or leaving this job to the compiler is safe?
lafayette is offline   Reply With Quote
Old 09-30-2008, 11:54 AM   #11
Mysterious C++ User
 
Elysia's Avatar
 
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 09-30-2008, 12:05 PM   #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   Reply With Quote
Old 09-30-2008, 12:14 PM   #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;
and then initializing it within the constructor as

Code:
member = "string";
and not with strcpy(), could lead to errors? I ask it because I think it's the same problem of using an array of pointers to Item, and then assigning things to them
lafayette is offline   Reply With Quote
Old 09-30-2008, 12:28 PM   #14
Mysterious C++ User
 
Elysia's Avatar
 
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 09-30-2008, 12:34 PM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:43 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22