I'm fairly new to C++ programming and I would like to ask you a (maybe strange) question...
The code below compiles correctly with both:
- the latest MinGW-compiler
- Borland C++ Builder Compiler 5.5
(from the free Command Line tools)
However if I run the program (after compiling with the Borland Compiler I get a Windows error message: "... has encountered a problem and needs to close. We are sorry for the inconvenience."
This problem doesn't seem to appear while using the MinGW-compiler...
This is the output of the Borland Compiler:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
test.cpp:
Warning W8004 test.cpp 29: 'ptr_to_data' is assigned a value that is never used
in function redim(int *,int)
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
Has this to do with a compiler switch I need to pass or isn't it recommended using this good old Borland compiler nowadays??
The code snippet below is the code I'm working with:
#include <iostream>
using namespace std;
void redim(int *ptr_to_data, int newdim);
int main(void)
{
int * testptr = new int[1]; // reserve some memory
testptr[1] = 23;
cout << testptr[1] << endl;
redim(testptr, 5); // change the dimension
testptr[5] = 203;
cout << testptr[5] << endl;
delete[] testptr; /* If you remove this instruction ('delete[] testptr;') and compile this source with Borland, the program doesn't crash, it shuts down properly, but it doesn't clean up the allocated memory ... */
return 0;
}
void redim(int *ptr_to_data, int newdim)
{
delete[] ptr_to_data;
ptr_to_data = new int[newdim];
}
Thanks in advance!
P.S.: Sorry if my English is bad (my native language is Dutch)