-1

I finished a beginner course on udemy, but I always struggle with understanding pointers, I started many times and pointers crashed my curiosity for programming every time. Now I finally want to pass this border. The point of this question, while the instructor was creating a pointer of an object he allocated them like described here:

person* k = new person[3] ;

for (i=0;i<3;i++){
// Why did he create a new person and copy the object from a pointer? 
// isn't this wastage of space or has it a good reason.
    person a_person = k[i] ;
    char *name = "Superman" ;
    a_person.set_name(name, strlen(name)) ;
    a_person.set_age(30) ;
    a_person.describe() ;

// isn't this better? Directly using the pointer to access the memory
// our pointer is pointing and change the variables there?
    char *surname = "Spiderman" ;
    (k+i)->set_name(surname, strlen(name)) ;
    (k+i)->set_age(10) ;
    (k+i)->describe();

}

class person {

public:
    person();
    ~person();
    int length() ;
    void get_addresses();
    int getid() ;
    void set_name(char *ptr_name, size_t bytes) ;
    char* get_name() ;
    int get_age() ;
    void describe() ;
    void set_age(int number) ;

private:
    char* name ;
    int age ;
    int id ;
    size_t bytes = 30 ;
    int get_unique() ;
    int setid() ;
};

E: The course had other code, but somehow I have to try it, so I built this person class with some functions and char*.

E2: yes, in the advanced c++ are all these structures, vectors, lists, maps and many c++11 features mentioned

  • 2
    [Best practice for C++ and resource-owning pointers](https://dl.dropboxusercontent.com/u/6101039/Modern%20C%2B%2B.pdf). – WhozCraig Feb 07 '17 at 08:40
  • 6
    This is terrible code, both for C and C++. Whatever instructor you have, quit now and never listen to anything they say ever again. Then [read a book](http://stackoverflow.com/a/388282/3484570). – nwp Feb 07 '17 at 08:40
  • learning by example: http://stackoverflow.com/questions/41930685/passing-pointer-from-function-to-function/41930944#41930944 – sameerkn Feb 07 '17 at 08:43
  • Creating a new person object as the first part of the for-loop does not modify the person objects at locations k, k+1, k+2... – jensa Feb 07 '17 at 08:44
  • 1
    In C++ none of the example use cases provided require pointers. True, they're implemented using pointers, but don't have to be. Pointers are to be avoided. There are cases where this is impossible, but none of these are such cases. – Flip Feb 07 '17 at 08:45
  • Since the first option does nothing to the content of `k[]`, the most immediate answer is simply that your instructor doesn't have a clue what they're actually doing. Optionally (and dare I say likely), you posted the wrong code and it actually read `person &a_person = k[i];` (note the reference `&`). That would make the two fragments functionally equivalent (save for the names and the numbers used). – WhozCraig Feb 07 '17 at 08:46
  • `char *name = "Superman"` is invalid C++ BTW (missing `const`). – Jarod42 Feb 07 '17 at 08:48
  • 1
    @Flip Leaving the poor code in this example aside, programmers should still learn pointers. Should I as a student just say "No, I'm not going to learn pointers because they should be avoided"? Don't think so... – jensa Feb 07 '17 at 08:49
  • @jensa Granted, but I'd prefer that they learn about them in the context of where they are required. Otherwise they might actually start writing code like that. – Flip Feb 07 '17 at 08:56
  • @WhozCraig, no it's like I wrote, C++ Tutorial for Complete Beginners, "Creating Particles" at time 20:55 – PS_OracleNerd Feb 07 '17 at 08:56
  • It's pedagogical code. In fact pointers are still needed as graph edges and as low-level structures for implementing containers and allocators themselves. But it is easier to present a name, which in modern C++ should be an std::string. – Malcolm McLean Feb 07 '17 at 09:58
  • This code is totally useless in order to learn C++. The mere suggestion to use a char * to store a string is horrifying. This is really, really bad. You'd better forget everything you "learned" and start again from zero using a good C++ book. – Daniel Daranas Feb 07 '17 at 10:19
  • 1
    The best practice in C++ for working with pointers is actually: "put if off as long as possible, learn about them, then avoid using them". Learn about standard containers. Once you understand them, learn about iterators. Once you understand iterators, learn about pointers (since, functionally, a pointer is a special type of iterator). Then, once you have learned about pointers, avoid using them whenever humanly possible. – Peter Feb 07 '17 at 11:00

2 Answers2

0

In first case you r creating a new object and copying data from object in array. In such way you r working with 'a_person' object, not with object in array. In second case you are working with objects in array.

  • Thank you, so except of the poor code I might understand it good, as you wrote. Maybe the Advanced C++ course is going to explain the other possibilities like described by others. – PS_OracleNerd Feb 07 '17 at 09:04
0
  • Remember to manage the ownership of your pointer carefully. Pair your new with delete, and new[] withdelete[], or you may leak the resource.

  • Let smart pointer to help you to manage the ownership of pointer, check std::unique_ptr or std::shared_ptr.

  • Use const char* to hold literal string and so does in parameter type instead of char*.

  • That k[i] returned is an object, not pointer. k is initialized as an array of person in length 3. k[i] means to dereference the element of array at i position. I guess the instructor lost a & to bind the k[i] like person& a_person = k[i];

  • Try other learning resource.

Chen OT
  • 3,486
  • 2
  • 24
  • 46
  • first point in the list should be: dont use (raw) pointers if there is no need to do so. In OPs example there is no need for a single pointer – 463035818_is_not_an_ai Feb 07 '17 at 09:33
  • I think OP is still learning the pointer, so I just give some tips when using the raw pointer. – Chen OT Feb 07 '17 at 09:39
  • 1
    yes I know, and the first tip should be not to use them ;). Seriously the worst thing I can imagine is code full of new and delete when there was never the need for a pointer in the first place – 463035818_is_not_an_ai Feb 07 '17 at 09:41
  • Thank you, but somehow on examples a teacher has to describe how to use pointers to access addresses, I understand there are better ways, maybe the advanced c++ course will show them, but it's all useless if I don't understand even the basics of pointers or definitions like I often check [click](http://en.cppreference.com/w/cpp/io/basic_ofstream) Thank you, it's nice to get so much answers. – PS_OracleNerd Feb 07 '17 at 10:10