如何重用已删除的指针? [英] How to reuse a deleted pointer?

查看:65
本文介绍了如何重用已删除的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




有人能指出我下面的代码有什么问题吗?我是

试图重用已删除的指针,但它不会编译。我的理由

可能是错的,但我认为重用指针会省去麻烦

声明指向字符串数组的指针。


我也怀疑会出现释放内存的问题

我正在使用数组指针并且正在使用

向量< std ::串GT;代替。无论如何,感谢任何指示。


-------------- snip --------------- ------

std :: map< int,std :: string *>注释;

std :: string * bi_val = new std :: string [2];

bi_val [0] =" W";

bi_val [1] =" E";

notes.insert(std :: pair< int,std :: string *>(4,bi_val));

bi_val = new std:string [2]; //错误在这里

bi_val [0] =" S";

bi_val [1] =" N";

notes。 insert(std :: pair< int,std :: string *>(2,bi_val));

bi_val = new std:string [2]; //同样的错误

bi_val [0] =" SE";

bi_val [1] =" NW";

notes .insert(std :: pair< int,std :: string *>(-2,bi_val));

bi_val = new std:string [2]; //错误!

bi_val [0] =" SW";

bi_val [1] =" NE";

notes。 insert(std :: pair< int,std :: string *>(6,bi_val));

解决方案

2004年1月24日星期六04:37:37 -0800,Damon写道:



有人能指出我下面的代码有什么问题吗?我正在尝试重用已删除的指针,但它不会编译。我的理由可能是错误的,但我认为重用指针会省去
声明指向字符串数组的麻烦。

我也怀疑会出现问题以某种方式释放内存
我正在使用数组指针并且正在使用
向量< std :: string>代替。无论如何,感谢任何指示。

-------------- snip ------------------- -
std :: map< int,std :: string *>注意;
std :: string * bi_val = new std :: string [2]; bi_val [0] =" W";
bi_val [1] =" E";
notes.insert(std :: pair< int,std :: string *>(4,bi_val )); bi_val =
new std:string [2]; //错误在这里




你错过了冒号,即上面的行应该是


bi_val = new std :: string [2];


顺便说一下,如果数组元素的数量总是两个,你可以用/或
用std替换数组或向量:: pair< std :: string,std :: string> ;.

否则我会选择std :: vector。


On 24 2004年1月04:37:37 -0800, so********@excite.com ( Damon)写道:



有人能指出我下面的代码有什么问题吗?我正在尝试重用已删除的指针,但它不会编译。我的理由可能是错的,但我认为重用指针可以省去声明指向字符串数组的指针的麻烦。

我也怀疑会出现问题以某种方式释放内存
我正在使用数组指针并且正在使用
向量< std :: string>代替。无论如何,感谢任何指示。

-------------- snip ------------------- -
std :: map< int,std :: string *>注释;
std :: string * bi_val = new std :: string [2];
bi_val [0] =" W";
bi_val [1] =" E" ;;
notes.insert(std :: pair< int,std :: string *>(4,bi_val));
bi_val = new std:string [2]; //错误这里
bi_val [0] =" S" 
bi_val [1] =" N";
notes.insert(std :: pair< int,std :: string *>(2,bi_val));
bi_val = new std:string [2]; //这里同样的错误
bi_val [0] =" SE";
bi_val [1] =" NW";
notes.insert(std :: pair< int,std: :string *>(-2,bi_val));
bi_val = new std:string [2]; //错误!
bi_val [0] =" SW";
bi_val [1] =" NE";
notes.insert(std :: pair< int,std :: string *>(6,bi_val));




_syntax_错误是你正在使用的:而不是:: ...


_logical_错误是你没有删除指针_

uses_,所以每当你新时在你删除之前的

值之前,你正在泄漏一些无法恢复的内存

在程序运行期间...

-leor


Leor Zolman

BD软件
le ** @ bdsoft.com
www.bdsoft.com - C / C ++,Java,Perl& A的现场培训Unix

C ++用户:下载BD Software的免费STL错误消息

Decryptor at www.bdsoft.com/tools/stlfilt.html




Simon Saunders < SI ************ @ net.ntl.com>在留言中写道

news:pa **************************** @ net.ntl.com .. 。

| On Sat,2004年1月24日04:37:37 -0800,Damon写道:

|

| >

| >

| >有人能指出我下面的代码有什么问题吗?我正在尝试

| >重用已删除的指针,但它不会编译。我的理由可能是

| >错了,但我想重用指针会省去

|的麻烦>声明指向字符串数组的指针。

| >

| >我还怀疑会有一个释放内存的问题

| >我正在使用数组指针并使用

|来玩弄>矢量<的std :: string>代替。无论如何,感谢任何指示。

| >

| > -------------- snip ---------------------

| > std :: map< int,std :: string *>笔记;

| > std :: string * bi_val = new std :: string [2]; bi_val [0] =" W";

| > bi_val [1] =" E";

| > notes.insert(std :: pair< int,std :: string *>(4,bi_val)); bi_val =

| > new std:string [2]; //错误在这里

|

|你错过了一个冒号,即上面的行应该是

|

| bi_val = new std :: string [2];


此外,由于OP未使用''
这一事实导致内存泄漏''删除[]'',然后重新分配

资源。


干杯。

Chris Val

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::string> 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,std::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,std::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,std::string*>( -2, bi_val ) );
bi_val = new std:string[2]; // error!
bi_val[0] = "SW";
bi_val[1] = "NE";
notes.insert( std::pair<int,std::string*>( 6, bi_val ) );

解决方案

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::string> 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,std::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.


On 24 Jan 2004 04:37:37 -0800, so********@excite.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::string> 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,std::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,std::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,std::string*>( -2, bi_val ) );
bi_val = new std:string[2]; // error!
bi_val[0] = "SW";
bi_val[1] = "NE";
notes.insert( std::pair<int,std::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



"Simon Saunders" <si************@net.ntl.com> wrote in message
news:pa****************************@net.ntl.com...
| 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::string> 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,std::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


这篇关于如何重用已删除的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆