473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

delete [] var_name; //sometimes does not work

Please help. I have been on this problem for a week now.
I am using Windows 98SE, Microsoft Visual C++ 6.0
The following program only works when the function is not
called, BUT the function does EXACTLY what is in the
main() function. I am completely at a loss.

//==============code
#include <iostream.h>
#include <stdlib.h>

int fill_a(__int64 *list) {
int n, i;
n=8;
delete [] list;
list = new __int64 [n];
for (i=0;i<n;i++) {
list[i]=5;
}
return n;
}

int main() {
__int64 *list;
char buffer[20];
int n, i;
list = new __int64;

//n = fill_a(list);

n=8;
delete [] list;
list = new __int64 [n];
for (i=0;i<n;i++) {
list[i]=5;
}

for (i=0;i<n;i++) {
_i64toa(list[i], buffer, 10);
cout << buffer << endl;
}
delete [] list;
return 0;
}
//==============end of code

When:

n = fill_a(list);

/*n=8;
delete [] list;
list = new __int64 [n];
for (i=0;i<n;i++) {
list[i]=5;
}*/

An error arises:

================================================== ==========
Microsoft Visual C++ Debug Library

Program: ...ES\MICROSOFT VISUAL
STUDIO\MYPROJECTS\TEST13\DEBUG\TEST13.EXE
File: dbgdel.cpp
Line: 47

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

For information on how your program can use assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)

[Abort] [Retry] [Ignore]
================================================== ==========

Thank you,
Valerij

Dec 8 '06 #1
6 2394
"valerij" <va**************@gmail.comwrote in message
news:11*********************@f1g2000cwa.googlegrou ps.com...
Please help. I have been on this problem for a week now.
I am using Windows 98SE, Microsoft Visual C++ 6.0
The following program only works when the function is not
called, BUT the function does EXACTLY what is in the
main() function. I am completely at a loss.

//==============code
#include <iostream.h>
#include <stdlib.h>

int fill_a(__int64 *list) {
This is accepting a pointer value. The address the pointer is pointing to
is copied into the local variable list
int n, i;
n=8;
delete [] list;
Okay, you deleted the memory that list was pointing to
list = new __int64 [n];
Note: this only changed the local pointer, list is a local variable. The
pointer inside main is unchanged
for (i=0;i<n;i++) {
list[i]=5;
}
return n;
}

int main() {
__int64 *list;
char buffer[20];
int n, i;
list = new __int64;
list points to some memory here
//n = fill_a(list);
fill_a(list) deletes the memory that list was pointing to, but the list
local to main remains unchanged, it still points to the memory that is now
freed
n=8;
delete [] list;
list = new __int64 [n];
for (i=0;i<n;i++) {
list[i]=5;
}

for (i=0;i<n;i++) {
_i64toa(list[i], buffer, 10);
And here you are trying to access memory that has already been freed.
cout << buffer << endl;
}
delete [] list;
return 0;
}
//==============end of code

When:

n = fill_a(list);

/*n=8;
delete [] list;
list = new __int64 [n];
for (i=0;i<n;i++) {
list[i]=5;
}*/

An error arises:
<snip>

The solution to this is to pass a pointer to a pointer to the function, then
the function can change the actual pointer itself.

int fill_a(__int64** list) {
// ...
delete [] *list;
*list = new __int64 [n];
// ...


Dec 8 '06 #2
wow ... thanks

Dec 8 '06 #3
Jim Langston wrote:
"valerij" <va**************@gmail.comwrote in message
news:11*********************@f1g2000cwa.googlegrou ps.com...
>int fill_a(__int64 *list) {
>int n, i;
n=8;
delete [] list;
Okay, you deleted the memory that list was pointing to
>list = new __int64 [n];
>for (i=0;i<n;i++) {
list[i]=5;
}
return n;
}
>int main() {
__int64 *list;
char buffer[20];
int n, i;
list = new __int64;
list points to some memory here
>//n = fill_a(list);
fill_a(list) deletes the memory that list was pointing to, but the
list local to main remains unchanged, it still points to the memory
that is now freed
Careful tho: the 'list' pointer was allocated with 'new' but 'fill_a'
will try to free the memory with 'delete[]'. This is a No-No! Always use
'delete[]' if you got the memory from 'new[]' and always use 'delete' if you
got the memory from 'new'!

regards
--
jb

(reply address in rot13, unscramble first)
Dec 8 '06 #4

valerij wrote:
Please help. I have been on this problem for a week now.
I am using Windows 98SE, Microsoft Visual C++ 6.0
The following program only works when the function is not
called, BUT the function does EXACTLY what is in the
main() function. I am completely at a loss.

//==============code
#include <iostream.h>
#include <stdlib.h>

int fill_a(__int64 *list) {
int n, i;
n=8;
delete [] list;
list = new __int64 [n];
for (i=0;i<n;i++) {
list[i]=5;
}
return n;
}

int main() {
__int64 *list;
char buffer[20];
int n, i;
list = new __int64;

//n = fill_a(list);

n=8;
delete [] list;
You shouldn't reuse that pointer, nothing forces the compiler/platform
to release the allocation at this point.
list = new __int64 [n];
__int64 *p_list = new __int64 [n];
for (i=0;i<n;i++) {
list[i]=5;
}

for (i=0;i<n;i++) {
_i64toa(list[i], buffer, 10);
cout << buffer << endl;
}
delete [] list;
return 0;
}
//==============end of code

When:

n = fill_a(list);
As already explained, the list pointer above is copied into a local.
>
/*n=8;
delete [] list;
list = new __int64 [n];
for (i=0;i<n;i++) {
list[i]=5;
}*/

An error arises:

================================================== ==========
Microsoft Visual C++ Debug Library

Program: ...ES\MICROSOFT VISUAL
STUDIO\MYPROJECTS\TEST13\DEBUG\TEST13.EXE
File: dbgdel.cpp
Line: 47

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

For information on how your program can use assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)

[Abort] [Retry] [Ignore]
================================================== ==========

Thank you,
Valerij
Instead of using primitive arrays why not use std::vector instead.
If you really must use arrays and to bypass having to use dumb
pointers:

#include <iostream>
#include <ostream>
#include <algorithm>
#include <iterator>
#include <stdexcept>

template< typename T, const std::size_t Size >
class Array
{
T array[Size];
public:
Array() { }
Array(const Array& copy)
{
std::copy( copy.array,
copy.array + Size + 1,
array );
}
T& at(std::size_t idx)
{
if(idx < 0 || idx Size)
throw std::range_error("array out of range");
return array[idx];
}
void fill(const T& t)
{
std::fill( array,
array + Size + 1,
t );
}
friend std::ostream&
operator<<(std::ostream& os, const Array& r_a)
{
std::copy( r_a.array,
r_a.array + Size - 1,
std::ostream_iterator< T >(os, ", ") );
return os << r_a.array[Size] << std::endl;
}
};

int main()
{
Array< double, 5 darray;
darray.fill( 11.1 );
std::cout << darray;
try {
// double d = darray.at(6); // throws
double d = darray.at(5);
std::cout << d << std::endl;

} catch (const std::exception& r_e)
{
std::cerr << "Error: " << r_e.what();
std::cerr << std::endl;
}
}

/*
11.1, 11.1, 11.1, 11.1, 11.1
11.1, 11.1, 11.1, 11.1, 11.1
*/

Dec 8 '06 #5
Salt_Peter wrote:
valerij wrote:
>list = new __int64;

//n = fill_a(list);

n=8;
delete [] list;
You shouldn't reuse that pointer, nothing forces the compiler/platform
to release the allocation at this point.
>list = new __int64 [n];
Could you elaborate on that? I do not see anything wrong with reusing
that pointer.

regards
--
jb

(reply address in rot13, unscramble first)
Dec 8 '06 #6
I have worked on it a bit longer, and came up with the following. In
Microsoft Visual C++ 6 it works:

//code
#include <iostream.h>
#include <stdlib.h>

void fill_a(__int64** list, int n) {
delete [] list[0];
list[0]= new __int64 [n];
for (int i=0;i<n;i++) {
list[0][i]=i;
}
}

int main() {
__int64** ptr_list;
__int64 *list;
char buffer[20];
int n=20, i;
list = new __int66];
ptr_list=&list;
fill_a(ptr_list, n);
for (i=0;i<n;i++ ) {
_i64toa(list[i], buffer, 10);
cout << buffer << endl;
}
delete [] list;
return 0;
}
//end of code

Dec 9 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: de Beers | last post by:
mysql_query("DELETE FROM cart WHERE ItemId=$ItemId"); There is the code but the result in my databse is that the ID number changes from, lets say, 77 to 78 with 78's contents being empty. ...
2
by: Hamish Dean | last post by:
Hi, I have created a DLL which the user is complaining: "Even when it works ok, my application stays instable (like something is allocated or deleted in the wrong way)." What does this...
8
by: Capstar | last post by:
Hi NG, I was looking through the code of boost::shared_ptr, and found the following piece of code in some referenced header file. As far as I understand it, it is used to delete the object the...
4
by: pedicini | last post by:
I work with many dynamically allocated variables in my program including double **, int *, char *. For ex: double **d; d = new (double *) ; for(int i = 0; i < 10; i++) { d = new double; }
11
by: Squid Seven | last post by:
I create a pointer to an item: CardSession *cardSession; Then, later, I use new to create an instance of the item and assign it to that pointer: cardSession = new CardSession(); In...
2
by: LaurenW | last post by:
After many years of working with Access, I am FINALLY required to delete a single field from a table in a linked back-end data base and I must do it fro the FRONT end! There MUST be a way to do...
5
by: Jeff User | last post by:
Hello ..NET 1.1, VS 2003, C# & asp.net I have tried to follow msdn instructions and samples but I can not get an event to fire for this button on the datagrid. There has to be something obvious...
1
by: Dave | last post by:
I have a table with 3 fields of which the primary key is a autonumber. I have created my dataadapter, dataset and datagridview using a wizard. However, in the datagrid, the update and delete...
4
by: gerardianlewis | last post by:
Any help appreciated. (VB.NET under XP and Vista, SP1 installed) My code, inherited from a VB6 version of an app that ran under W98, loads an image from a file into a PictureBox. The user may...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.