简体   繁体   中英

extending array in c++

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?

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 .

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM