locked
How to avoid pointers, dynamic allocation, and malloc when using the function pointer in the class in C++ RRS feed

  • Question

  • Hello All,

    How to avoid pointers, dynamic allocation, and malloc when using the function pointer in the class in C++

    mailAPI is the function pointer of the structure. And this structure has the callbacks like as shown below:

    mailcallbacks.h

    typedef struct
    {
    short (*getmails) (int * numberOfmails);
    }mailAPI;


    I am using the above defined function pointer (mailAPI) like as shown below:

    CMyClass.h

    static mailAPI* m_pAPIs;

    CMyClass.cpp

    mailAPI* CMyClass::m_pAPIs = nullptr;
    int CMyClass::Initialize()
    {
    m_pAPIs = (mailAPI*)malloc(sizeof(mailAPI));	
    if (m_pAPIs == nullptr)	
    {		
    return -1;	
    }
    }

    I was asked to avoid pointers, dynamic allocation, and malloc. 
    And asked to define statically in the class.

    Can someone please help me how to do this?

    Thanks in Advance,

    Thursday, July 16, 2020 2:43 PM

Answers

  • Just make it

    static mailAPI m_pAPIs;

    (drop the star), and you won't need dynamic memory allocation. I don't see any reason to allocate an instance of mailAPI on the heap.


    Igor Tandetnik

    • Marked as answer by David student Friday, July 17, 2020 4:42 PM
    Thursday, July 16, 2020 5:35 PM

All replies

  • Just make it

    static mailAPI m_pAPIs;

    (drop the star), and you won't need dynamic memory allocation. I don't see any reason to allocate an instance of mailAPI on the heap.


    Igor Tandetnik

    • Marked as answer by David student Friday, July 17, 2020 4:42 PM
    Thursday, July 16, 2020 5:35 PM
  • Thank you. It worked with your input.
    Friday, July 17, 2020 4:42 PM