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)

Perhaps the error message is correct.

Actually, almost every line of your program is wrong. It's quite a surprise that it gets that far.

> int * testptr = new int[1];
OK so far.

> testptr[1] = 23;
Oops, you stepped off the end. You only allocated 1 int, so the only valid subscript is zero.

> cout << testptr[1] << endl;
It prints what you stored, but it's stored in someone else's memory. Maybe they'll want it back soon...

> redim(testptr, 5); // change the dimension
Look up call by value and call by reference.
Sure you can free the memory (you have a copy of the pointer), but you can't change the pointer HERE (you don't have a reference to it).

Anything you do AFTER this point in the code with testptr is really bad news. Even if everything else was fixed, the subscript would still be out of bounds (see earlier comment).

> delete[] testptr;
The function freed it.
But you didn't get back a new pointer, so you try and free the old one again (this is a bad thing).

> ptr_to_data = new int[newdim];
And now the line with the error message.
ptr_to_data at this point is just a local variable. Changing it here doesn't change anything in the caller.
So you allocate some memory, then immediately lose the pointer to it when the function returns (and thus leaking memory in the process).

> P.S.: Sorry if my English is bad (my native language is Dutch)
Nobody here would have been able to tell otherwise :)
Your English is fine.

First of all, thank you for replying so quickly...

Ok, I've changed my code and actually it compiles with the Borland compiler... :)
But could you please take a look at it to ensure the code below is correct?

#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[0] = 23;
	
	cout << testptr[0] << endl;
	
	redim(&testptr, 5); // change the dimension
	
	testptr[5] = 203;
	
	cout << testptr[5] << endl;
	
	delete[] testptr;
	
	return 0;
}

void redim(int **ptr_to_data, int newdim)
{
	delete[] *ptr_to_data;
	*ptr_to_data = new int[newdim];
}

Thanks.

Thank you for the link, that's exactly what I was looking for!

Ok, the last bug is fixed (I hope so)...

Here's the source for the last (?) time:

#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[0] = 23;
	
	cout << testptr[0] << endl;
	
	redim(&testptr, 5); // change the dimension
	
	testptr[4] = 203;
	
	cout << testptr[4] << endl;
	
	delete[] testptr;
	
	return 0;
}

void redim(int **ptr_to_data, int newdim)
{
	delete[] *ptr_to_data;
	*ptr_to_data = new int[newdim];
}
commented: Looks good, and well done on the code tags. +29
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.