В общем грубо говоря такой код:
void Test2()
{
double d1=45.434;
std::auto_ptr<double> apd1( &d1);
}
падает на выходе из функции, я так понимаю обьект d1 удаляется 2 раза.
В моей задаче есть сам обьект и его дальнейшее использование в функции через auto_ptr. Подскажите это нормальное поведение и если да, то наверно есть какие то стандарные способы решения такой проблемы? (На форумах с наскока подобной темы не нашел)
Здравствуйте, MaxiMal_TSG, Вы писали:
MM_>В общем грубо говоря такой код: MM_>void Test2() MM_>{ MM_> double d1=45.434; MM_> std::auto_ptr<double> apd1( &d1); MM_>} MM_>падает на выходе из функции, я так понимаю обьект d1 удаляется 2 раза. MM_>В моей задаче есть сам обьект и его дальнейшее использование в функции через auto_ptr. Подскажите это нормальное поведение и если да, то наверно есть какие то стандарные способы решения такой проблемы? (На форумах с наскока подобной темы не нашел)
Посмотрите доку на auto_ptr. Там чётко прописано, что он делает delete хранимому указателю. А вы килляете объект на стеке. Результат немного предсказуем. Так что опишите, что вы в реальности хотите сделать.
LU разложение реализовано не мной и на входе принимает apmY , вот собственно и вопрос как мне передать этот параметр не создавая в функции переменной m_mY, и если я создам в другой функции m_mY (разделю код) сдается мне что от проблемы двойного удаления это не избавит.
Здравствуйте, MaxiMal_TSG, Вы писали:
MM_>Хорошо в реальности код следующий: MM_> _mY m_mY; //исходная матрица проводимостей.
MM_> std::auto_ptr< _mY > apL ( new _mY ); MM_> std::auto_ptr< _mY > apU ( new _mY ); MM_> std::auto_ptr< _mY > apmY( &m_mY );
MM_> //треангуляция MM_> std::auto_ptr< _LU > apLU( new _LU( *apmY, *apL, *apU ) );
MM_>LU разложение реализовано не мной и на входе принимает apmY , вот собственно и вопрос как мне передать этот параметр не создавая в функции переменной m_mY, и если я создам в другой функции m_mY (разделю код) сдается мне что от проблемы двойного удаления это не избавит.
Чего то я не понял, а зачем тебе apmY? m_mY как я понял, живет на стеке...
Certain sets of names and function signatures are always reserved to the implementation:
•Each name that contains a double underscore (_ _) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the implementation for any use.
•Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.165
165) Such names are also reserved in namespace ::std (17.4.3.1).
7.1.3 Reserved identifiers
Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers.
•All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
•All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.
•Each macro name in any of the following subclauses (including the future library directions) is reserved for use as specified if any of its associated headers is included; unless explicitly stated otherwise (see 7.1.4).
•All identifiers with external linkage in any of the following subclauses (including the future library directions) are always reserved for use as identifiers with external linkage.154
•Each identifier with file scope listed in any of the following subclauses (including the future library directions) is reserved for use as a macro name and as an identifier with file scope in the same name space if any of its associated headers is included.
No other identifiers are reserved. If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.
If the program removes (with #undef) any macro definition of an identifier in the first group listed above, the behavior is undefined.
154) The list of reserved identifiers with external linkage includes errno, math_errhandling, setjmp, and va_end.
Здравствуйте, MaxiMal_TSG, Вы писали:
MM_>В общем грубо говоря такой код: MM_>void Test2() MM_>{ MM_> double d1=45.434; MM_> std::auto_ptr<double> apd1( &d1); MM_>}
При вызове деструктора auto_ptr будет delete для стековой переменной.
Плюс потенциальная проблема — при передаче apd1 куда-то за пределы функции получится ссылка на локальную переменную.
Ну и вообще auto_ptr довольно специфичный смарт-поинтер, лучше по несколько раз перечитать документацию для него.