2

Well, I searched on the internet about dynamically extending an array and found that it's not possible in C++. I have to use either malloc, realloc of C version or std::vector in C++. But the code below seems to work just fine.

#include<iostream>
using namespace std;
int main() {
    int len = 10;
    int *p = new int[len];
    for (int i = 0; i < len; i++){
        p[i] = i + 1;
    }
    *(p + len) = 1000; // extend here. 
    len++;
    for (int i = 0; i < len; i++){
        cout << p[i] << endl;
    }
    return 0;
}

Well, if I use this approach in big programs, is there any problem? why people said that it is not possible? If it's because the extended area might be already filled up with other information or anything else? I just want to know if the above approach is right. If not, then exactly why?

3
  • 2
    Check out std::vector. Commented Feb 7, 2014 at 13:42
  • 2
    If you want a dynamically extensible array in C++, just use std::vector. Commented Feb 7, 2014 at 13:42
  • 2
    If you need dynamic sizing, why not use std::vector? Commented Feb 7, 2014 at 13:43

4 Answers 4

4

This sometimes works because writing to memory you haven't allocated is undefined behavior. Eventually writing off the end of an array is going to get you a segmentation fault, when the OS kills you for being naughty.

Use a C++ std::vector if you need a resizable array. There are almost zero downsides. It handles all the complication of re-allocating and de-allocating for you. It will also do the right thing in resizing to make it so appending to the array is amortized O(1).

You could use realloc to resize an array, but there's the potential it might get copied to a new location. However you cannot use realloc on something you created with new! It must have been allocated with malloc and friends. new and new[] must go with delete and delete[], malloc and friends must go with free.

If you use the C functions to allocate memory in a C++ program, you run into other dangers. The C functions do not run the constructor on arrays allocated for user defined or standard library types.

In short, allocating and resizing arrays directly is fraught with pitfalls, it's just not worth it when we have a wonderful class that does the right thing automatically in std::vector.

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

2 Comments

cant't i just allocate memory from the end of an array? will overwriting be a problem then?
Not really. If there's no memory space available at the end of the array you already allocated, the OS is going to give you a brand new array anyway. There is literally no reason to handle this yourself. Use std::vector.
2

It works from time to time without crashing, and then it crashes ... It is simply dangerous to use / overwrite memory you do not have allocated yourself. This is an ideal source for bugs, highly exploited in the wild.

For your situation you have the 10 elements of the array guaranteed to be yours. The line *(p+len)=1000; is however pure evil. Because that piece of memory might be allocated to someone else.

I highly recommend for you to learn and use valgrind.

2 Comments

cant't i just allocate memory from the end of an array? will overwriting other information in that area be a problem then?
There is a way in c++ to allocate memory at a specific memory location, (placement new) however that does not fit your situation here. For that you already need to own that memory. Generally, memory handling is done at one level below C++ at the OS level.
0

No, the above approach is not right. *(p + len) = 1000; // extend here. line does overwrite a consecutive memory area which hasn't been allocated, so your program might crash immediately if you are lucky or it can continue working with the corrupted heap which can be far more troublesome. The other problem here is that you don't release the allocated resources, so if you will work with bigger data, you will encounter far bigger memory leaks. Maybe check 'vector' instead.

Comments

0

well, if I use this approach in big programs, is there any problem?

Yes, you'll leak memory. Every call to new() should have a corresponding delete call. Also note, you are writing to memory beyond what you've been allocating with new(), this is a call for letting your code behave unexpectedly.

Using a std::vector<> instead, will do the memory management properly for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.