How to demonstrate memory error using arrays in C ++

I am trying to come up with a method that demonstrates a memory error using Arrays and C ++, which is hard to detect. The goal is to motivate the use of the STL <> vector in combination with iterators.

Edit: The accepted answer is the answer I used to explain the advantages / disadvantages. I also used: this

+6
c ++ arrays memory-leaks
source share
8 answers

Memory leak? IMO, a vector in combination with iterators does not protect you from errors, such as going beyond or using an invalid iterator in general (unless you have VC ++ with iterator debugging); rather, it is convenient because it implements a dynamically changing array for you and takes care of memory management (NB! helps to make your code more safe for exceptions).

void foo(const char* zzz) { int* arr = new int[size]; std::string s = zzz; //... delete[] arr; } 

The above may occur if an exception occurs (for example, when creating a string). Not with a vector.

The vector also simplifies the explanation of the code because of its semantics of meaning.

 int* arr = new int[size]; int* second_ref = arr; //... delete [] arr; arr = 0; //play it safe :) //... second_ref[x] = y; //... delete [] second_ref; 

But, perhaps, the vector does not automatically satisfy 100% of cases of using dynamic arrays. (For example, there is also boost::shared_array and to-be std::unique_ptr<T[]> )

+6
source share

Incorrect connection of new / delete and new [] / delete [].

For example, using:

 int *array = new int[5]; delete array; 

instead:

 int *array = new int[5]; delete [] array; 

Although the C ++ standard does not allow this, some compilers support stacks that allocate an array:

 int stack_allocated_buffer[size_at_runtime]; 

This may be an unintended side effect of scope rules (for example, persistent, obscured by a member variable) ... and it works until someone skips "size_at_runtime" too big and removes the stack. Then lame errors appear.

+8
source share

I think the std :: vector utility really shows when you need dynamic arrays.

Do one example using std :: vector. Then one example using an array for realloc. I think this speaks for itself.

+4
source share

One obvious:

 for (i = 0; i < NUMBER_OF_ELEMENTS; ++i) destination_array[i] = whatever(i); 

against

 for (i = 0; i < NUMBER_OF_ELEMENTS; ++i) destination_vector.push_back(whatever(i)); 

indicating that you know that the second one works, but whether the first one depends on how destination_array was determined.

+3
source share
 void Fn() { int *p = new int[256]; if ( p != NULL ) { if ( !InitIntArray( p, 256 ) ) { // Log error return; } delete[] p; } } 

You would not BELIEVE how often I see it. A classic example of using any form of RAII ...

+3
source share

I would think that the simple ease of using vector instead of dynamic arrays is already convincing.

  • You do not need to forget to delete your memory ... it is not so simple, since attempts to delete it can be bypassed by exceptions and something else.
  • If you want to create dynamic arrays yourself, the safest way to do this in C ++ is to wrap them in a class and use RAII , But vectors do this for you. Actually this question.
  • Resizing is done for you.
  • If you need to support arbitrary types, you do not need to do any extra work.
  • Many algorithms are provided for handling containers for both inbound and other users.
  • You can still use functions that need arrays by passing in a vector base array if necessary; The memory is guaranteed to comply with the standard, with the exception of vector<bool> (google says as of 2003, see 23.2.4./1 specifications).
  • Using an array is probably a bad practice in general, as you will reinvent the wheel ... and your implementation will almost certainly be much worse than the existing one ... and harder for other people to use as they know about vector , but not about your strange thing.

With a dynamic array, you need to track the size yourself, grow when you want to insert new elements, delete it when you no longer need it ... this is an extra job. Oh, and a warning: vector<bool> is a dirty rotten hack and a classic example of premature optimization.

+3
source share

Why don't you motivate him based on the algorithms provided by STL?

+2
source share

In a raw array, the [] operator (if I can call it) is prone to an out-of-bounds index problem. With a vector, this is not (there is at least a run-time exception).

Sorry, I did not read the question carefully enough. index-out-of-bound is a problem, but not a memory error.

0
source share

All Articles