Whats is difference between ::malloc and malloc , ::free and free and ::realloca and realloc in qMalloc ,qFree,qRealloc



  • In file corelib/global/qglobal.cpp of QtCore

    What is difference between

    Version one
    void *qMalloc(size_t size) { return ::malloc(size); }
    1950 void qFree(void *ptr) { ::free(ptr); }
    1951 void *qRealloc(void *ptr, size_t size) { return ::realloc(ptr, size); }

    Version 2

    Void* qMalloc(size_t sz) {return malloc(sz);}
    void qFree(void* ptr) {free(ptr);}
    void* qRealloc(void* ptr, size_t sz) {return realloc(ptr, sz);}

    Are they same


  • Lifetime Qt Champion

    Hi,

    The double column operator is called the scope-resolution operator. When used like that it will resolve the malloc symbol starting at the global namespace.



  • Did not get that will that if you could explain more by example


  • Qt Champions 2016

    :: = scope-resolution operator. The name.

    void someclass::func() {
    cow(); // this looks in someclass for cow() and then global
    }

    void someclass::func() {
    ::cow(); // this looks in global first, then in class to find cow()
    }

    so its a way of saying, Im talking about the global one or please look for a global version first.



  • but as I understand we do have nor definition our own malloc defined this the following will be same

    Version one
    void *qMalloc(size_t size) { return ::malloc(size); }
    1950 void qFree(void *ptr) { ::free(ptr); }
    1951 void *qRealloc(void *ptr, size_t size) { return ::realloc(ptr, size); }

    Version 2

    Void* qMalloc(size_t sz) {return malloc(sz);}
    void qFree(void* ptr) {free(ptr);}
    void* qRealloc(void* ptr, size_t sz) {return realloc(ptr, sz);}


  • Moderators

    @Qt-Enthusiast This is done to make sure that the global one is used. Even if you don't have any other at the moment, later someone could add one. To make sure that the global one is always used you write ::malloc
    You should ask C++ related questions here: https://forum.qt.io/category/34/c-gurus
    This is a C++ question not related to Qt.


  • Lifetime Qt Champion

    @jsulm Moved ! :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.