-1

Say we have this for instance:

string* p;
p = new string[2];
p[0] = "Aa";
p[1] = "Bb";

What I understand: I need to create a temp dynamic array to store what p has, delete p, and then create a new p with a row 1 size bigger than temp, and then copy temp into p.

What I do not understand: How would I increase the size of p WITHOUT knowing the original rowsize (in this case 2) and still keep the original data. I've read about doing tricks such as (sizeof(array)/sizeof(dataType)), but since I have a dynamic array, I have no idea how to find it's size. realloc would format my original data, plus it acts funny with strings.

Just a heads up: I do not want to use any form of linked lists, vectors, or things of that nature.

Lzak
  • 373
  • 1
  • 8
  • 7
    Do yourself a favor, use `vector` instead. Answering your question - what good is an array that you don't know the size of, in the first place? Forget about expanding it - how are you going to use it if you don't know its size? You need to keep track of the current size, one way or another. – Igor Tandetnik Sep 21 '14 at 04:18
  • You can't determine this using just the pointer. You need to store the array dimension somewhere else, or use some sentinel entry to mark the last element. – cdhowie Sep 21 '14 at 04:18
  • "*in this case 2*" - Which "rowsize" are you talking about? Are you referring to the fact that each string is 2 characters long? This does *not* mean that the row length is 2 characters. In fact, assuming `string` is `std::string`, you're basically already getting `std::vector` semantics with auto-resizing and moving. Please clarify exactly what you are trying to accomplish. – MooseBoys Sep 21 '14 at 04:19
  • 1
    While allocating memory to dynamic array store its size in some variable. – Hemant Gangwar Sep 21 '14 at 04:22
  • Please read [question: How does C++ delete know it's an array?](http://stackoverflow.com/questions/703691/how-does-delete-know-its-an-array) and also [this C++ FAQs page on array delete operator](http://www.parashift.com/c%2B%2B-faq-lite/num-elems-in-new-array.html) to learn the basics of C++ dynamic arrays. The language standard doesn't provide a way - you have to store the size somewhere else; to circumvent it would require implementation-specific hacks, and would certainly break when you move to a different compiler or version. If you have time also read about new/delete operator overload. – rwong Sep 21 '14 at 18:37
  • -1: for saying " I do not want to use any form of" without an generally acceptable and believable reason. – rwong Sep 21 '14 at 18:39

1 Answers1

0

You can do the following:

string* p;
p = new string[2];
p[0] = "Aa";
p[1] = "Bb";

string* reallocated = new string[3];
std::copy(p,p+2,reallocated); // Save your original values
delete[] p; // release formerly allocated memory
p = reallocated; // Assign newly allocated memory
p[2] = "Cc"; // Add some additional element

Anyhow, I can't see a point, to do this in preference of simply using a std::vector<std::string>>.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • I appreciate the quick response, and I understand that normally people would use vectors. My main concern is how to obtain how many elements are in a dynamic array WITHOUT storing its size, so you wrote: string* reallocated = new string[3] but keep in mind the question is how would the computer understand to use 3. – Lzak Sep 21 '14 at 05:28
  • @Lzak Computers can't guess. They're dumb as toast. – fredoverflow Sep 21 '14 at 07:03
  • 1
    There's a bug in your code: use "delete[]" instead of "delete" to free allocated arrays. – Sunius Sep 21 '14 at 17:15