If a have an array of pointers to an object, which way of filling the array is cleaner, (1) or (2), in the code segment below? Are
string ** list = new string*[5];
string* s = new string("foo");
*list[0] = *s; //(1)
list[0] = s; //(2)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The first method will result in undefined behaviour.
list[0]doesn’t point at a validstringobject, so dereferencing it and assigning to it will be bad.The second method looks ok to me (in the sense that it won’t have undefined behaviour), but I guess it depends on how you go on to use it.