unique_ptr syntax

Mar 29, 2014 at 7:11am
Hi,
I'm having problems with the syntax of unique_ptr.

If in my header file I have declare a unique_ptr:
 
    unique_ptr<float[]> ptr;


How do I allocate an array of float on my source file? i.e. something that looks like this:
 
ptr = new float [123];


thanks in advance



Mar 29, 2014 at 7:30am
AFAIK you should not use unique_ptr<> or any other smart ptr
to hold dynamic arrays

U can use vectors instead
Mar 29, 2014 at 7:33am
To assign it an array of 123 floats you either want to use the constructor or reset method.

1
2
3
4
std::unique_ptr<float[]> ptr(new float[123]);
//or
std::unique_ptr<float[]> ptr;
ptr.reset(new float[123]);


http://www.cplusplus.com/reference/memory/unique_ptr/reset/
http://www.cplusplus.com/reference/memory/unique_ptr/unique_ptr/
http://www.cplusplus.com/reference/memory/unique_ptr/

[edit] you probably should use a stl container though
[edit2] This might be helpful also:
http://www.cplusplus.com/reference/memory/unique_ptr/operator=/
Last edited on Mar 29, 2014 at 7:38am
Mar 29, 2014 at 4:04pm
thanks!, that was exactly what I was looking for
Topic archived. No new replies allowed.