C++ 合法性和道德,如果不同范围的';新';和';删除';

C++ 合法性和道德,如果不同范围的';新';和';删除';,c++,scope,new-operator,delete-operator,C++,Scope,New Operator,Delete Operator,我正在函数中创建一个动态数组。代码(张贴在下面)运行没有任何问题。我想知道我所写的方法是否是正确的方法,或者它是否会在将来在更复杂的代码中产生问题。我知道我的程序(下面)试图实现的特定任务在使用字符串或向量时效果更好。但是我创造了这个人工的例子来让大家明白我的问题。但是,如果您强烈认为应该避免使用动态数组,请随时分享您的观点和理由 我之前的研究结果:我无法找到一个关于使用new[]创建动态数组并随后在不同范围内删除它们的合法性和道德性的一致讨论 谢谢你的想法和见解 我的示例代码如下: =====

我正在函数中创建一个动态数组。代码(张贴在下面)运行没有任何问题。我想知道我所写的方法是否是正确的方法,或者它是否会在将来在更复杂的代码中产生问题。我知道我的程序(下面)试图实现的特定任务在使用字符串或向量时效果更好。但是我创造了这个人工的例子来让大家明白我的问题。但是,如果您强烈认为应该避免使用动态数组,请随时分享您的观点和理由

我之前的研究结果:我无法找到一个关于使用new[]创建动态数组并随后在不同范围内删除它们的合法性和道德性的一致讨论

谢谢你的想法和见解

我的示例代码如下:

==========================

#include<iostream>
#include<string>
#include<cctype>
using namespace std;

void getNonPunct(string _str, char* &_npcarr, int &_npsize);

int main()
{
    string input_string;    
    char* npchar_arr;
    int npsize;

    cout << "Enter any string: ";
    getline(cin, input_string);

    getNonPunct(input_string, npchar_arr, npsize);    

    // Now display non-punctuation characters in the string
    cout << "string with non-punctuation characters removed:\n";

    for (int n = 0; n <= npsize - 1; n++)
        cout << npchar_arr[n];
    cout << "\n(" << npsize << ") non-punctuation characters\n";    

    // Now return the memory allocated with 'new' to heap

    delete [] npchar_arr;
    // Is it okay to 'delete' npchar_arr eve if it was created in the function
    // getNonPunct() ?

    return(0);
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void getNonPunct(string _str, char* &_npcarr, int &_npsize)    
//This void function takes an input array of strings containing arbitrary
//characters and returns a pointer to an array of characters containing only
//the non-punctuation characters in the input string. The number of
//non-punctuation characters are stored in size. Prior to the function call,
//int_arr and size are undefined. After the function call, char_arr points to
//the first location of an array of the non-punctuation character array.
//'size' is equal to the number of non-punctuation characters in the input
//string. 
{

    // First get the number of non-punctuation characters in the string

    int str_len, npcount = 0;

    str_len = static_cast<int>( _str.length() );

    _npsize = 0;
    for (int i = 0; i <= str_len - 1; i++)
    {
        if ( !ispunct(_str[i]) )
            _npsize++;        
    }

    // Now that you know how many non-punctuation characters are in the string,
    // create a (dynamic) character array of _npsize.

    _npcarr = new char [_npsize];


    for (int k = 0; k <= str_len - 1; k++)
    {
        if ( !ispunct(_str[k]) )
            _npcarr[npcount++] = _str[k];
    }    

    return;
}
#包括
#包括
#包括
使用名称空间std;
void getnonpunt(字符串str、char*&&npcarr、int&&npsize);
int main()
{
字符串输入_字符串;
char*npchar\u arr;
int-npsize;
cout它是否有效?是的。
npchar\u arr
指向的数组存在,直到您销毁它,并且可以在另一个函数中使用
delete[]
表达式销毁它

这是一个好主意吗?不是。你最好使用一个自动管理对象生命周期的智能指针,这样你就不用自己手动删除指针了

考虑使用<代码> STD::UnQuyGyPTR 如果编译器和标准库支持<代码> UNQuYYPPTR <代码>,或<代码> STD::AutoPyPTR < /C> >或 STD::SyrdYPPTR < /C> >如果不能使用<代码> UNQuYYPPTR <代码>(<代码> SydDypPTR 也可以在Boost和C++ Tr1中找到).

它有效吗?是的。
npchar\u arr
指向的数组存在,直到您销毁它,并且可以在另一个函数中使用
delete[]
表达式销毁它

这是一个好主意吗?不是。你最好使用一个自动管理对象生命周期的智能指针,这样你就不用自己手动删除指针了


考虑使用<代码> STD::UnQuyGyPTR 如果编译器和标准库支持<代码> UNQuYYPPTR <代码>,或<代码> STD::AutoPyPTR < /C> >或 STD::SyrdYPPTR < /C> >如果不能使用<代码> UNQuYYPPTR <代码>(<代码> SydDypPTR 也可以在Boost和C++ Tr1中找到).

在一个范围内分配,在另一个范围内删除是使用动态分配而不是自动局部变量的主要原因之一

正如James所说,我们更喜欢使用智能指针来跟踪动态分配的对象


在一个作用域中分配和在另一个作用域中解除分配时,我能想到的唯一约束是多组件应用程序,其中不同的组件使用不同的编译器(或者同一编译器的不同版本)构建。在Windows上,每个编译器都提供自己的内存管理库,您不应该从分配给它的组件以外的组件释放内存。或者,您可以使用Windows提供的内存管理例程,这些例程对所有编译器都是通用的(例如,
HeapAlloc
/
HeapFree
).在Linux上,每个人都使用glibc提供的
malloc
免费
new[]
delete[]
,所以这不是一个问题。对于其他操作系统,您需要进行调查。

在一个范围内分配,在另一个范围内删除是使用动态分配而不是自动局部变量的主要原因之一

正如James所说,我们更喜欢使用智能指针来跟踪动态分配的对象


在一个作用域中分配和在另一个作用域中解除分配时,我能想到的唯一约束是多组件应用程序,其中不同的组件使用不同的编译器(或者同一编译器的不同版本)构建。在Windows上,每个编译器都提供自己的内存管理库,您不应该从分配给它的组件以外的组件释放内存。或者,您可以使用Windows提供的内存管理例程,这些例程对所有编译器都是通用的(例如,
HeapAlloc
/
HeapFree
).在Linux上,每个人都使用glibc提供的
malloc
免费的
新的[]
,和
删除[]
,因此这不是一个问题。对于其他操作系统,您需要进行调查。

这个示例非常简单,但请想象一下您对代码的以下修改:

int main()
{
    string input_string;    
    char* npchar_arr;
    int npsize;

    cout << "Enter any string: ";
    getline(cin, input_string);

    getNonPunct(input_string, npchar_arr, npsize);    

    // Now display non-punctuation characters in the string
    cout << "string with non-punctuation characters removed:\n";

    for (int n = 0; n <= npsize - 1; n++)
        cout << npchar_arr[n];
    cout << "\n(" << npsize << ") non-punctuation characters\n";    

    return(0); // WHOOPS! I JUST RETURNED AND DIDN'T FREE THE MEMORY

    delete [] npchar_arr;
    // Is it okay to 'delete' npchar_arr eve if it was created in the function
    // getNonPunct() ?

    return(0);
intmain()
{
字符串输入_字符串;
char*npchar\u arr;
int-npsize;

cout这个例子非常简单,但是想象一下您对代码的以下修改:

int main()
{
    string input_string;    
    char* npchar_arr;
    int npsize;

    cout << "Enter any string: ";
    getline(cin, input_string);

    getNonPunct(input_string, npchar_arr, npsize);    

    // Now display non-punctuation characters in the string
    cout << "string with non-punctuation characters removed:\n";

    for (int n = 0; n <= npsize - 1; n++)
        cout << npchar_arr[n];
    cout << "\n(" << npsize << ") non-punctuation characters\n";    

    return(0); // WHOOPS! I JUST RETURNED AND DIDN'T FREE THE MEMORY

    delete [] npchar_arr;
    // Is it okay to 'delete' npchar_arr eve if it was created in the function
    // getNonPunct() ?

    return(0);
intmain()
{
字符串输入_字符串;
char*npchar\u arr;
int-npsize;

请注意,在这个特定示例中,
npchar\u arr
也可以是一个
std::string
,整个算法可以写成
std::remove\u copy\u if(source.begin(),source.end(),std::back\u inserter(result),(int)(*)(int)std::ispunt)
。请注意,在这个特定示例中,
npchar\u arr
也可能只是一个
std::string
,整个算法可以写成
std::remove\u copy\u if(source.begin()、source.end()、std::back\u inserter(result),(int)(*)(int)std::ispunt)杰姆斯,我对C++很有新意。所以请耐心等待我的基本问题。STD::UNQuYYPTR和其他STL中预先指定的指针类型,或者是它们创建的东西吗?基本问题没有错。我们都是初学者。在某个点上::代码> AutoPPTR <代码>,<代码> SyddYPPTR < /C>,和<代码> UNIQ。UEYPTPT/<代码>都是C++标准库的一部分(在<代码> <代码> >中。<代码> UNQUIGYPTR 和 SyddYPPT/<代码>是比较新的,