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. why most of the programmers use malloc instead of calloc to allocate dynamic memory ?

why most of the programmers use malloc instead of calloc to allocate dynamic memory ?

Scheduled Pinned Locked Moved Solved C++ Gurus
4 Posts 3 Posters 541 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.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on 4 Mar 2022, 06:14 last edited by
    #1

    I have found that there is possibility of junk data come in malloc based dynamic memory allocation. But in calloc based memory allocation does not have junk data.

    But though most of programmers why use malloc instead calloc ?

    J 1 Reply Last reply 4 Mar 2022, 06:16
    0
    • Q Qt embedded developer
      4 Mar 2022, 06:14

      I have found that there is possibility of junk data come in malloc based dynamic memory allocation. But in calloc based memory allocation does not have junk data.

      But though most of programmers why use malloc instead calloc ?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 4 Mar 2022, 06:16 last edited by
      #2

      @Qt-embedded-developer calloc overwrites allocated memory with 0 which makes it slower.
      Usually the "junk" data is not a problem, or why do you care what is in the allocated memory?

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

      Q 1 Reply Last reply 4 Mar 2022, 06:23
      3
      • J jsulm
        4 Mar 2022, 06:16

        @Qt-embedded-developer calloc overwrites allocated memory with 0 which makes it slower.
        Usually the "junk" data is not a problem, or why do you care what is in the allocated memory?

        Q Offline
        Q Offline
        Qt embedded developer
        wrote on 4 Mar 2022, 06:23 last edited by
        #3

        @jsulm because i think junk data may create problem.

        J 1 Reply Last reply 4 Mar 2022, 08:27
        0
        • Q Qt embedded developer
          4 Mar 2022, 06:23

          @jsulm because i think junk data may create problem.

          J Offline
          J Offline
          JKSH
          Moderators
          wrote on 4 Mar 2022, 08:27 last edited by JKSH 3 Jun 2022, 06:53
          #4

          @Qt-embedded-developer said in why most of the programmers use malloc instead of calloc to allocate dynamic memory ?:

          i think junk data may create problem.

          Then I suggest:

          • Port to C++ classes and containers
          • Initialize all your member variables correctly
          • Use new instead of malloc/calloc

          This way, junk data won't be a problem, and you get better protection against other subtle bugs.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          5

          1/4

          4 Mar 2022, 06:14

          • Login

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