-2

I'm wondering if RAII always allocates on the stack, or if the compiler ever uses the heap for large objects (and then perhaps adds a token to the stack as a sort of reminder of when to destroy the corresponding heap-allocated object)?

UPDATE: Apparently this question has been deemed unclear. Perhaps a code example will make this clearer:

In this code:

void dosomething() {
    MyClass myclass();
}

Assuming the compiler didn't optimize away such a trivial example, does the instance of MyClass that's hereby created always get allocated on the stack, or is the heap ever used?

I think I understand the answer now thanks to the accepted answer -- the answer appears to be that the class instance itself goes on the stack while its contents may or may not depending on how its constructor was defined. Please add a comment/answer if this is incorrect.

Magnus
  • 10,736
  • 5
  • 44
  • 57
  • 4
    RAII and "stack" allocation are quite orthogonal concepts. – juanchopanza Jul 06 '14 at 19:10
  • Stack allocation is stack allocation, that's all about the point. Period. – πάντα ῥεῖ Jul 06 '14 at 19:10
  • Where does RAII allocate then? Can the user specify? – Magnus Jul 06 '14 at 19:11
  • 2
    you should read about _dynamic and automatic allocations_, `RAII` doesn't interfere with neither of this 2 concepts, you are on the wrong track . – user2485710 Jul 06 '14 at 19:11
  • 1
    @Magnus RAII doesn't allocate anywhere. It has to do with what happens at the beginning and at the end of an object's lifetime, regardless of where it was allocated. – juanchopanza Jul 06 '14 at 19:12
  • @juanchopanza Correct me if I'm wrong but based on my new understanding I don't think your comment is quite true -- RAII does allocate for the object itself on the stack, as I suspected, even if the contents of the object might be allocated elsewhere depending on the object definition. Is this incorrect? – Magnus Jul 07 '14 at 01:27
  • That's incorrect. RAII is not in charge of allocation. It doesn't allocate objects anywhere. – juanchopanza Jul 07 '14 at 05:26
  • What happens then when I write MyClass myclass(); ? Doesn't this create an instance of MyClass on the stack? – Magnus Jul 07 '14 at 14:15
  • people might downvoted the question due to it's being asked unclearly, i'd say it's exact the value of SO -- to clarify confusion. – athos Oct 01 '16 at 05:16

1 Answers1

6

The way you talk about RAII makes it sound like you have something of a mis-impression about the most fundamental idea of what RAII is. RAII (also known as SBRM--stack bound resource management, so no RAII isn't really entirely orthogonal to at least the concept of a stack) is basically a style of programming.

Programs written using RAII can and often do allocate memory from the free store. Such allocations, however, are handled by an object of some class. When the object is destroyed, the class' destructor executes, and that frees the dynamically allocated memory.

Just for example, a typical string object will only contain a small amount of data, such as a pointer to the string's contents and an integer or two to keep track of the string size. When you create the string, it'll allocate some space from the free store to hold the actual data. When the string is destroyed, it'll automatically free that data. In some cases, it'll have some logic to avoid that free store allocation for small strings by allocating some small (fixed) amount of space in the string object itself, but doesn't change the basic idea.

So, the real answer is a qualified "yes". Yes, it's fairly common to have a small object that contains a pointer to some data allocated on the heap. Yes, the object will free that memory when the object itself is destroyed. But no, that's not something the compiler does for you. Rather, it's something you do in designing and implementing your classes.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • So the class instantiation itself is on the stack though, while its members might be a mix of similarly RAII-based and traditional new/delete-based members? In other words when I say MyClass myclass(); this object itself was on the stack even if its innards weren't necessarily? – Magnus Jul 07 '14 at 01:06
  • 2
    The answer to your basic question is yes. Most code should just defin objects with automatic storage. Unfortunately, your example is of the "most vexing parse": `MyClass myclass()` declares a function named `myclass` with a return type of `MyClass`. To define an object, you need to omit the parentheses: `MyClass myclass;`. – Jerry Coffin Jul 07 '14 at 01:14