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. malloc / new issue

malloc / new issue

Scheduled Pinned Locked Moved Unsolved C++ Gurus
4 Posts 4 Posters 758 Views 1 Watching
  • 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.
  • Match0umM Offline
    Match0umM Offline
    Match0um
    wrote on last edited by
    #1

    Hi !
    Sorry for that, but as I am not a expert in programming, I am stucked here. :(

    I wanted to take avantage of new/delete instead of malloc/free.

    In an existing program, I had:

    bufferSize = (int*)malloc(sizeof(int));
    

    and I changed for:

    int *bufferSize = new int();
    

    Compilation is OK but programm crashes when I try to access bufferSize. Can someone kindly tell me if something is wrong in my new line ? :(

    Thanks for your help / explanation

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      First - where is the relation to Qt here?
      Second - why do you want to create a single integer on the heap at all?
      Third - you code does not show anything, especially not the place where it's crashing. Provide a minimal, compilable example so we can see what you're doing (wrong).

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • M Offline
        M Offline
        mchinand
        wrote on last edited by mchinand
        #3

        Also, in your first (old) line of code, you don't declare the type of bufferSize ; is it a class member variable? In your second (new) line of code, you do declare the type, making it a local shadow variable (which should generate a compiler warning and/or c-tidy warning). If you try to reference bufferSize in another method, it would try to access the uninitialized member variable.

        1 Reply Last reply
        0
        • nageshN Offline
          nageshN Offline
          nagesh
          wrote on last edited by
          #4

          @Match0um where is this declared?
          int bufferSize;
          if declared as member variable during initialization use like this..
          bufferSize = new int(); //don't use int

          1 Reply Last reply
          0

          • Login

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