Thread: C++ newbie question about pointers

  1. #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

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    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).

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    perfect. So the second notation should also be corrected using "new[]" rather than "new"?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    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).

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    very very clear, I think I've understood now

    thank you so much

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    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.

  7. #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

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

  10. #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?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  13. #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

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  2. Question about the introduction to pointers lesson
    By Freemind11 in forum C++ Programming
    Replies: 11
    Last Post: 02-15-2006, 11:45 PM
  3. Question on pointers?
    By robasc in forum C++ Programming
    Replies: 16
    Last Post: 12-25-2005, 01:15 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. very newbie question: how to copy files
    By webwesen in forum C Programming
    Replies: 26
    Last Post: 04-25-2002, 03:01 PM