Reserve space in the stack for an unitialised object

Hi folks,
what's the best way to reserve some space in the stack for a uninitialised object. For example:

1
2
3
4
5
6
7
8
9
10
11
12
uint8_t* data = m_prefix; 
MyUnitialisedObject _object; // MyUnitialisedObject is a template param without default ctor
while(...){
  ... a pile of code here ...

  if(condition){
    _object = MyUnitialisedObject::init(...)
    data = _object.data()
  }

  ... use data here ...
}


The whole purpose of _object is to keep the space pointed by the variable `data' alive when the condition arises.


p.s. the preview button for posts is broken in my browser, same for you?
Last edited on
Oky, so the classic solution is:

1
2
    uint8_t _object_space[sizeof(MyUnitialisedObject)];
    MyUnitialisedObject _object = *reinterpret_cast<MyUnitialisedObject*>(_object_space); // reserve space in the stack 


which is, admittedly, ugly as a yellow renault 4. I am hoping for something better from you!
Last edited on
I have really no idea what problem you are trying to solve with that code? Some optimization?

Oky, so the classic solution is:
That makes no sense. It will copy garbage to _object.

p.s. the preview button for posts is broken in my browser, same for you?
It is a problem of the first post on this site.
@coder777

it doesn't matter if it copies garbage, the whole point is that the scope of the variable _object remains visible as long as the variable `data'. To understand the issue, suppose that MyUnitialisedObject was something like:
1
2
3
4
5
6
class MyUnitialisedObject {
uint8_t buffer[64]; 
...
public:
const uint8_t* data() const { return m_data; };
}


If the variable were initialised upon first usage, that is:
1
2
3
4
  if(condition){
    MyUnitialisedObject _object = MyUnitialisedObject::init(...)
    data = _object.data()
  }


then data could indeed contain garbage after the if block ends.

The problem of the ``classic solution'' is that it reserves twice the space in the stack. Right now I'm solving it with something like:

1
2
3
4
5
6
7
 uint8_t _object_space[sizeof(MyUnitialisedObject)];

 ...
if(condition){
    *reintepret_cast<MyUnitialisedObject*>(&_object_space) = MyUnitialisedObject::init(...);
    data = reintepret_cast<MyUnitialisedObject*>->data();
}


Not great either, but it gets the job done.
What are trying to achieve?

 
uint8_t _object_space[sizeof(MyUnitialisedObject)];


This will reserve sizeof(MyUnitialisedObject) bytes on the stack (assuming that sizeof(uint8_t) is 1) with _object_space as a uint8_t pointer. This memory reference is only valid as long as _object_space is within scope.

Why not just (not tried):

 
static MyUnitialisedObject _object[1];


_object is now a pointer to memory space of size sizeof(MyUnitialisedObject) that doesn't become invalid when _object goes out of scope. But this will be space on the heap rather than the stack.
Last edited on
Hye seeplus, that's correct. The point is that _object_space is visible as long as the variable `data' is.
static MyUnitialisedObject _object[1]; won't work because MyUnitialisedObject does not have a default/empty ctor.Indeed, I don't know what it is its constructor, it's a template arg.
Last edited on
I'm not sure what you are asking either. Are you looking for placement new?
some space in the stack


Why do you want space on the stack - as opposed to the heap?

Last edited on
@doug4 ah right =)

@seeplus due to optimisation reasons, already profiled & accounted :(
Topic archived. No new replies allowed.