473,811 Members | 2,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to reuse a deleted pointer?

Hi,

Can someone point out to me what's wrong with the code below? I'm
trying to reuse the deleted pointer but it won't compile. My reason
may be wrong, but I thought reusing the pointer would save the trouble
of declaring a pointer to a string array.

I also suspect there will be a problem of releasing memory in the way
I'm using the array pointer and is toying with using a
vector<std::str ing> instead. Anyway, thanks for any pointers give.

--------------snip---------------------
std::map<int, std::string*> notes;
std::string *bi_val = new std::string[2];
bi_val[0] = "W";
bi_val[1] = "E";
notes.insert( std::pair<int,s td::string*>( 4, bi_val ) );
bi_val = new std:string[2]; // error here
bi_val[0] = "S";
bi_val[1] = "N";
notes.insert( std::pair<int,s td::string*>( 2, bi_val ) );
bi_val = new std:string[2]; // same error here
bi_val[0] = "SE";
bi_val[1] = "NW";
notes.insert( std::pair<int,s td::string*>( -2, bi_val ) );
bi_val = new std:string[2]; // error!
bi_val[0] = "SW";
bi_val[1] = "NE";
notes.insert( std::pair<int,s td::string*>( 6, bi_val ) );
Jul 22 '05 #1
11 3532
On Sat, 24 Jan 2004 04:37:37 -0800, Damon wrote:
Hi,

Can someone point out to me what's wrong with the code below? I'm trying
to reuse the deleted pointer but it won't compile. My reason may be
wrong, but I thought reusing the pointer would save the trouble of
declaring a pointer to a string array.

I also suspect there will be a problem of releasing memory in the way
I'm using the array pointer and is toying with using a
vector<std::str ing> instead. Anyway, thanks for any pointers give.

--------------snip---------------------
std::map<int, std::string*> notes;
std::string *bi_val = new std::string[2]; bi_val[0] = "W";
bi_val[1] = "E";
notes.insert( std::pair<int,s td::string*>( 4, bi_val ) ); bi_val =
new std:string[2]; // error here


You've missed out a colon, i.e. the line above should be

bi_val = new std::string[2];

By the way, if the number of array elements is always two you could
replace the array or vector with std::pair<std:: string, std::string>.
Otherwise I would go with std::vector.
Jul 22 '05 #2
On 24 Jan 2004 04:37:37 -0800, so********@exci te.com (Damon) wrote:
Hi,

Can someone point out to me what's wrong with the code below? I'm
trying to reuse the deleted pointer but it won't compile. My reason
may be wrong, but I thought reusing the pointer would save the trouble
of declaring a pointer to a string array.

I also suspect there will be a problem of releasing memory in the way
I'm using the array pointer and is toying with using a
vector<std::st ring> instead. Anyway, thanks for any pointers give.

--------------snip---------------------
std::map<int, std::string*> notes;
std::string *bi_val = new std::string[2];
bi_val[0] = "W";
bi_val[1] = "E";
notes.insert( std::pair<int,s td::string*>( 4, bi_val ) );
bi_val = new std:string[2]; // error here
bi_val[0] = "S";
bi_val[1] = "N";
notes.insert( std::pair<int,s td::string*>( 2, bi_val ) );
bi_val = new std:string[2]; // same error here
bi_val[0] = "SE";
bi_val[1] = "NW";
notes.insert( std::pair<int,s td::string*>( -2, bi_val ) );
bi_val = new std:string[2]; // error!
bi_val[0] = "SW";
bi_val[1] = "NE";
notes.insert( std::pair<int,s td::string*>( 6, bi_val ) );


The _syntax_ error is that you're using : instead of :: ...

The _logical_ error is that you're not deleting the pointer _between
uses_, so each time you "new" before you've deleted the previous
value, you're leaking a bit more memory that cannot be recovered
during the program's run...
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #3

"Simon Saunders" <si************ @net.ntl.com> wrote in message
news:pa******** *************** *****@net.ntl.c om...
| On Sat, 24 Jan 2004 04:37:37 -0800, Damon wrote:
|
| > Hi,
| >
| > Can someone point out to me what's wrong with the code below? I'm trying
| > to reuse the deleted pointer but it won't compile. My reason may be
| > wrong, but I thought reusing the pointer would save the trouble of
| > declaring a pointer to a string array.
| >
| > I also suspect there will be a problem of releasing memory in the way
| > I'm using the array pointer and is toying with using a
| > vector<std::str ing> instead. Anyway, thanks for any pointers give.
| >
| > --------------snip---------------------
| > std::map<int, std::string*> notes;
| > std::string *bi_val = new std::string[2]; bi_val[0] = "W";
| > bi_val[1] = "E";
| > notes.insert( std::pair<int,s td::string*>( 4, bi_val ) ); bi_val =
| > new std:string[2]; // error here
|
| You've missed out a colon, i.e. the line above should be
|
| bi_val = new std::string[2];

Additionally, there is a memory leak due to the fact that
the OP is not using 'delete[]', prior to re-allocating
recourses.

Cheers.
Chris Val
Jul 22 '05 #4
On Sat, 24 Jan 2004 13:03:25 +0000, Leor Zolman wrote:
On 24 Jan 2004 04:37:37 -0800, so********@exci te.com (Damon) wrote:
Hi,

Can someone point out to me what's wrong with the code below? I'm trying
to reuse the deleted pointer but it won't compile. My reason may be
wrong, but I thought reusing the pointer would save the trouble of
declaring a pointer to a string array.

I also suspect there will be a problem of releasing memory in the way
I'm using the array pointer and is toying with using a
vector<std::s tring> instead. Anyway, thanks for any pointers give.

--------------snip---------------------
std::map<int, std::string*> notes;
std::string *bi_val = new std::string[2]; bi_val[0] = "W"; bi_val[1]
= "E";
notes.insert( std::pair<int,s td::string*>( 4, bi_val ) ); bi_val =
new std:string[2]; // error here bi_val[0] = "S"; bi_val[1] = "N";
notes.insert( std::pair<int,s td::string*>( 2, bi_val ) ); bi_val =
new std:string[2]; // same error here bi_val[0] = "SE"; bi_val[1] =
"NW";
notes.insert( std::pair<int,s td::string*>( -2, bi_val ) ); bi_val =
new std:string[2]; // error! bi_val[0] = "SW"; bi_val[1] = "NE";
notes.insert( std::pair<int,s td::string*>( 6, bi_val ) );


The _syntax_ error is that you're using : instead of :: ...

The _logical_ error is that you're not deleting the pointer _between
uses_, so each time you "new" before you've deleted the previous value,
you're leaking a bit more memory that cannot be recovered during the
program's run...
-leor



No. The pointers are stored in a map so it would be incorrect to delete
each one immediately after inserting it (the map would contain junk). Of
course, in order to prevent memory leaks it is necessary to delete each of
the stored arrays when the map is no longer needed.

Jul 22 '05 #5
On Sun, 25 Jan 2004 00:13:34 +1100, Chris ( Val ) wrote:

"Simon Saunders" <si************ @net.ntl.com> wrote in message
news:pa******** *************** *****@net.ntl.c om...
| On Sat, 24 Jan 2004 04:37:37 -0800, Damon wrote:
|
| > Hi,
| >
| > Can someone point out to me what's wrong with the code below? I'm trying
| > to reuse the deleted pointer but it won't compile. My reason may be
| > wrong, but I thought reusing the pointer would save the trouble of
| > declaring a pointer to a string array.
| >
| > I also suspect there will be a problem of releasing memory in the way
| > I'm using the array pointer and is toying with using a
| > vector<std::str ing> instead. Anyway, thanks for any pointers give.
| >
| > --------------snip---------------------
| > std::map<int, std::string*> notes;
| > std::string *bi_val = new std::string[2]; bi_val[0] = "W";
| > bi_val[1] = "E";
| > notes.insert( std::pair<int,s td::string*>( 4, bi_val ) ); bi_val =
| > new std:string[2]; // error here
|
| You've missed out a colon, i.e. the line above should be
|
| bi_val = new std::string[2];

Additionally, there is a memory leak due to the fact that
the OP is not using 'delete[]', prior to re-allocating
recourses.


If by that you mean there should be a line "delete[] bi_val" after each
line that begins "notes.insert(. .." you are wrong, because you would end
up with a map containing junk pointers. Obviously those pointers must be
deleted at some point before the map goes out of scope to avoid memory
leakage.
Jul 22 '05 #6

"Simon Saunders" <si************ @net.ntl.com> wrote in message
news:pa******** *************** *****@net.ntl.c om...
| On Sun, 25 Jan 2004 00:13:34 +1100, Chris ( Val ) wrote:
|
| >
| > "Simon Saunders" <si************ @net.ntl.com> wrote in message
| > news:pa******** *************** *****@net.ntl.c om...
| > | On Sat, 24 Jan 2004 04:37:37 -0800, Damon wrote:
| > |
| > | > Hi,
| > | >
| > | > Can someone point out to me what's wrong with the code below? I'm trying
| > | > to reuse the deleted pointer but it won't compile. My reason may be
| > | > wrong, but I thought reusing the pointer would save the trouble of
| > | > declaring a pointer to a string array.
| > | >
| > | > I also suspect there will be a problem of releasing memory in the way
| > | > I'm using the array pointer and is toying with using a
| > | > vector<std::str ing> instead. Anyway, thanks for any pointers give.
| > | >
| > | > --------------snip---------------------
| > | > std::map<int, std::string*> notes;
| > | > std::string *bi_val = new std::string[2]; bi_val[0] = "W";
| > | > bi_val[1] = "E";
| > | > notes.insert( std::pair<int,s td::string*>( 4, bi_val ) ); bi_val =
| > | > new std:string[2]; // error here
| > |
| > | You've missed out a colon, i.e. the line above should be
| > |
| > | bi_val = new std::string[2];
| >
| > Additionally, there is a memory leak due to the fact that
| > the OP is not using 'delete[]', prior to re-allocating
| > recourses.
| >
|
| If by that you mean there should be a line "delete[] bi_val" after each
| line that begins "notes.insert(. .." you are wrong, because you would end
| up with a map containing junk pointers. Obviously those pointers must be
| deleted at some point before the map goes out of scope to avoid memory
| leakage.

Oop's, you're right.

I didn't realise the container wasn't making a copy here.

Thanks.
Chris Val
Jul 22 '05 #7
On 24 Jan 2004 04:37:37 -0800 in comp.lang.c++, so********@exci te.com
(Damon) was alleged to have written:
bi_val = new std:string[2]; // error here
No fair asking about an error without quoting the error message.
As others have noted, missing ":".
std::map<int, std::string*> notes;
std::string *bi_val = new std::string[2];
bi_val[0] = "W";
bi_val[1] = "E";
notes.insert( std::pair<int,s td::string*>( 4, bi_val ) );


Pardon me for saying, isn't this some rather complicated code to do
a simple thing? (Which would be my worry any time I see "new" in
application level code.) What are you trying to accomplish, and
wouldn't it be easier with a const data table built at compile time?
Thereby avoiding all the memory leak issues?

Jul 22 '05 #8
"Damon" <so********@exc ite.com> wrote in message
news:15******** *************** ***@posting.goo gle.com...
Hi,

Can someone point out to me what's wrong with the code below? I'm
trying to reuse the deleted pointer but it won't compile. My reason
may be wrong, but I thought reusing the pointer would save the trouble
of declaring a pointer to a string array.

I also suspect there will be a problem of releasing memory in the way
I'm using the array pointer and is toying with using a
vector<std::str ing> instead. Anyway, thanks for any pointers give.

--------------snip---------------------
std::map<int, std::string*> notes;
std::string *bi_val = new std::string[2];
bi_val[0] = "W";
bi_val[1] = "E";
notes.insert( std::pair<int,s td::string*>( 4, bi_val ) );
bi_val = new std:string[2]; // error here
bi_val[0] = "S";
bi_val[1] = "N";
notes.insert( std::pair<int,s td::string*>( 2, bi_val ) );
bi_val = new std:string[2]; // same error here
bi_val[0] = "SE";
bi_val[1] = "NW";
notes.insert( std::pair<int,s td::string*>( -2, bi_val ) );
bi_val = new std:string[2]; // error!
bi_val[0] = "SW";
bi_val[1] = "NE";
notes.insert( std::pair<int,s td::string*>( 6, bi_val ) );


Your design is very awkward. C arrays and unnecessary use of operator new
are both to be avoided if possible. Here it is not only possible, it's easy:

class ds // direction strings
{
private:
std::string m_dir;
std::string m_opp;
public:
ds() {}
ds(const std::string &i_dir, const std::string &i_opp) :
m_dir(i_dir), m_opp(i_opp) {}
const std::string &dir() const {return m_dir;}
const std::string &opp() const {return m_opp;}
};

std::map<int, ds> notes;
notes[4] = ds("W", "E");
notes[2] = ds("S", "N");
notes[-2] = ds("SE", "NW");
notes[6] = ds("SW", "NE");

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #9
Damon wrote:
Hi,

Can someone point out to me what's wrong with the code below? I'm
trying to reuse the deleted pointer but it won't compile. My reason
may be wrong, but I thought reusing the pointer would save the trouble
of declaring a pointer to a string array.

I also suspect there will be a problem of releasing memory in the way
I'm using the array pointer and is toying with using a
vector<std::str ing> instead. Anyway, thanks for any pointers give.

--------------snip---------------------
std::map<int, std::string*> notes;
std::string *bi_val = new std::string[2];
bi_val[0] = "W";
bi_val[1] = "E";
notes.insert( std::pair<int,s td::string*>( 4, bi_val ) );
bi_val = new std:string[2]; // error here
bi_val[0] = "S";
bi_val[1] = "N";
notes.insert( std::pair<int,s td::string*>( 2, bi_val ) );
bi_val = new std:string[2]; // same error here
bi_val[0] = "SE";
bi_val[1] = "NW";
notes.insert( std::pair<int,s td::string*>( -2, bi_val ) );
bi_val = new std:string[2]; // error!
bi_val[0] = "SW";
bi_val[1] = "NE";
notes.insert( std::pair<int,s td::string*>( 6, bi_val ) );


What others said. I especially like Cy's solution. This might give
better performance, though.

#include <utility>

typedef char const* String_pair[ 2 ];

struct Note_map
{
String_pair const& operator [ ] ( int ) const;
private:
static String_pair const m_string_pairs[ ];
};

String_pair const Note_map::m_str ing_pairs[ ] =
{
{ "SE", "NW" }, // -2
{ 0, 0 },
{ 0, 0 }, // 0
{ 0, 0 },
{ "S", "N" }, // 2
{ 0, 0 },
{ "W", "E" }, // 4
{ 0, 0 },
{ "SW", "NE" } // 6
};

#include <iostream>

int main( )
{
Note_map notes;

std::cout << notes[ 4 ][ 0 ] << '\n'
<< notes[ 2 ][ 0 ] << '\n'
<< notes[ -2 ][ 0 ] << '\n'
<< notes[ 6 ][ 0 ] << '\n';
}

String_pair const& Note_map::opera tor [ ] ( int i ) const
{
return m_string_pairs[ i + 2 ];
}

Jul 22 '05 #10

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

Similar topics

3
6903
by: DPfan | last post by:
What's exactly the meaning of "code reuse" in C++? Why such kind of reuse have more advantages over the counterpart in other language like in C? How is "code reuse" realized in C++? By composition mainly? What're others? Thanks in advance for your comments!
2
1160
by: Till Crueger | last post by:
Hi, I was wondering about a problem, that might occur when you delete a class in a method which was called by the deleted class. This is not actually a problem I ran accross (I would probably restructure if possible). It is rather one of those "What if" Questions. Here is some code to explain what I mean: class B;
6
2881
by: B. Penn | last post by:
Hi, I was testing pointers and found that I could still dereference a pointer and access the value/variable it pointed to after deleting it, which confused me for "the variable it pointed to is deleted and it now points to nowhere". Did I do anything wrong? I checked a couple of references but couldn't explain it. Could anyone kindly clear it for me please? Here is my code, which compiled with both GNU C++ and Visual Studio ..Net:
7
2839
by: hank | last post by:
Hi All In the Circular Logging when the Primary Log file fill up, the database manager will creat a secondary log files for the transaction; when this transaction finished, the secondary log files still allocated in log directory; when all application disconnect from database or database reactive or database restart the secondary log files will be deleted from log directory. For each transaction log request, database manager always...
4
5172
by: yf | last post by:
A KB article "http://support.microsoft.com/default.aspx?scid=kb;en-us;209599" tells that the maximum number of records that a table may hold if the PRIMARY key data type is set to AUTONUMBER is 4,294,967,295. Suppose the PRIMARY key data type is set to "RANDOM" AutoNumber. Suppose an application (a) successfully INSERTS "X" records, then (b) successfully DELETES "Y" records (X >= Y), then
48
5858
by: avasilev | last post by:
Hi all, my question is: if i allocate some memory with malloc() and later free it (using free()), is there a possibility that a consequent malloc() will allocate memort at the same starting address and will return the same pointer as the previous malloc(). I would like to have confirmation on whether this is practically a concern when pointers are used to uniquely identify data structure instances - like in this example:
4
3194
Digital Don
by: Digital Don | last post by:
Hi, I have been using C++ for the past 6 months and always had a problem of using the same IFSTREAM variable to read multiple files. do//Repeat while quit not entered { ipf.clear(); cout<<"\nGive file name (Q for Quit):"; cin>>ipf;
14
17209
by: Mark | last post by:
Hi, I would like to check if my object has been deleted or not, but my program keeps crashing. Here's the simplified code (in infinite loop) delete tetromino; //if(tetromino==NULL) tetromino = new TetrominoI; This continuously replaces the tetromino as expected, but if I take
13
3981
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help is much appreciated. JD
0
10663
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10401
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9217
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7676
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5567
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5704
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4357
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3881
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.