Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Memory allocation

Memory allocation

Scheduled Pinned Locked Moved Solved C++ Gurus
2 Posts 2 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tikani
    wrote on last edited by
    #1

    Hi! I have a question. When we write "SomeClass obj;" in any function's body it means that memory for our class will be allocated on stack. If some fields have a type of a pointer then memory for these fields will be allocated on heap via new or via initialisation list in/of the constructor and cleared in the destructor at the end of lifetime of that class, but the 4(8)*N (N-number of pointer type fields) bytes (size of a pointer at x86 or x86-64) will nevertheless go to the stack. And here is the core of the question. What happens when we write "SomeClass *p = new SomeClass();" but every field in class is a primitive, an array with predefined size or declared using "SomeClass obj;" syntax? How does storaging go on in this case?

    jsulmJ 1 Reply Last reply
    0
    • T Tikani

      Hi! I have a question. When we write "SomeClass obj;" in any function's body it means that memory for our class will be allocated on stack. If some fields have a type of a pointer then memory for these fields will be allocated on heap via new or via initialisation list in/of the constructor and cleared in the destructor at the end of lifetime of that class, but the 4(8)*N (N-number of pointer type fields) bytes (size of a pointer at x86 or x86-64) will nevertheless go to the stack. And here is the core of the question. What happens when we write "SomeClass *p = new SomeClass();" but every field in class is a primitive, an array with predefined size or declared using "SomeClass obj;" syntax? How does storaging go on in this case?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Tikani said in Memory allocation:

      SomeClass *p = new SomeClass();

      This is heap allocation. That means the complete object will be stored on the heap - it doesn't matter what the content of the object is - it will be allocated on the heap in this case. Only the pointer (p) will be on the stack.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved