C++ newbie question about pointers

This is a discussion on C++ newbie question about pointers within the C++ Programming forums, part of the General Programming Boards category; hi all, and I ask pardon for this question (I've read the FAQ about pointers) but still can't get rid ...

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

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.

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.

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?

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

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.

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.

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.