0

I'm trying to implement a dynamic array of strings for educational purpose. The problem that I ran into is the program crashing whenever I try to add strings to the empty array in my constructor.

Array::Array(string dir, string dim)
{
   size = 0;
   ptr = new string[size + 1];
   ptr[size] = dir;
   size++;
   ptr[size] = dim;
   size++;
}

I have int size and string *ptr declared in my header file. I originally thought this to be a out-of-bounds problem, but after looking at this post, I fixed the initial allocation to size + 1, but the persisting problem seems to prove otherwise.

1
  • I'm trying to implement a dynamic array of strings for educational purpose. -- Your best bet is to actually see a simple one first that works and then learn from such an example. Otherwise you will be easily in danger of doing the wrong things and not understanding why something doesn't work. Attempting to do this without the proper guidance first will just lead into frustration. Commented Jan 15, 2018 at 5:53

3 Answers 3

3

Changing the value of size does not change the size of the array.

You allocate an array of size 1. Then you assign something to the first (only) element of that array. Then you assign something to the second element of that array - but the array only has one element.

Also note that using new does not allocate a dynamic array. Once allocated, the size can't change.

Sign up to request clarification or add additional context in comments.

3 Comments

How would I go about changing the size of the array? The only other thing I can think of is to deallocate and reallocate memory every time an element is added, but that seems highly inefficient.
@serencha -- but that seems highly inefficient. -- Look at the example I linked to in the comment section above. You are missing a "capacity" or not utilizing the concept of capacity. The capacity controls when the array actually has to allocate memory.
@serencha In summary, you allocate more space than you need, so you don't do it every time. If you do this correctly you don't have many allocations compared to the number of elements, so on average it isn't inefficient.
3

As mentioned by Sid S, "changing the value of size does not change the size of the array."

And for your "inefficient" concern, a common trick that reflect to PaulMcKenzie and Daniel H's idea, is to use the doubling strategy. See the following C code for an simple idea:

#include <stdlib.h>

struct MyArray {
    int capacity;
    int size;
    int *data;
}

/* any other functions you would use, eg: create, destroy of MyArray */     

void push(struct MyArray myarray, int n) {

    if (size == capacity) {
        capacity *= 2;
        data = realloc(data, capacity*sizeof(int));
    }

    /* add the element to the end of data and increase size */
}

In this way, instead of doing realloc every time there is an element added, you would have a lower runtime in average.

A detailed amortized analysis about doubling strategy can be found here.

1 Comment

Make sure you check if realloc succeeds - it may very well fail and return NULL, in which case you shouldn't be overwriting data nor increase capacity.
0

Instead of string use pointer and allocate memory dynamically every time how much they need not 0 and then ++.

 Array :: Array(char *dir,char *dim)
    {
            int l1,l2;
            l1=strlen(dir);
            l2=strlen(dim);
            /**assume  n1 & n2 are data member of "Array" class.**/
            n1=new char[l1+1];// allocating memory dynamically
            n2=new char[l2+1];
    }

I hope it helps.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.