Will this do what I think it does?

This is a discussion on Will this do what I think it does? within the C++ Programming forums, part of the General Programming Boards category; I have a function that takes a pointer as a parameter Code: int SetElement(unsigned int p_iIndex, Type* p_tType) { if ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Will this do what I think it does?

    I have a function that takes a pointer as a parameter
    Code:
    int SetElement(unsigned int p_iIndex, Type* p_tType)
            {
                if ( p_iIndex < m_iMax )
                {
                    if ( m_vArray.at(p_iIndex) == NULL )
                         m_vArray.at(p_iIndex) = p_tType;
                    return 0;
                }
                return -1;
            }
    if were to call the function like this...
    Code:
    SetElement(0, new Type);
    would this dynamically allocate the memory in the m_vArray variable?
    I am hoping it does, but I am unsure.
    Last edited by Raigne; 03-06-2008 at 11:04 PM. Reason: typo

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Would you kindly post the declaration of m_vArray please?

    Note that the SetElement call is already bad because p_tType is the only pointer to a newly allocated object, and if the if-statement in your code evaluates to false then you lose the last remaining pointer to the new item and the memory is leaked.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Yes, it will store a pointer to the dynamically allocated memory in the location 0 in the m_vArray.

    If m_iMax is less than or equal to 0, the memory will be leaked. If there is already a non-null value at index 0, the memory will be leaked.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    The entire code is in my other thread... the Resource manager one
    but m_vArray is a vector
    Code:
    std::vector<Type*> m_vArray;
    EDIT:
    If it will work then I am going to have it free the old memory if the program calls SetElement on it.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    So this will prevent this leak?
    Code:
    int SetElement(unsigned int p_iIndex, Type* p_tType)
    		{
    			if ( p_iIndex < m_iMax )
    			{
    				if ( m_vArray.at(p_iIndex) == NULL )
    					m_vArray.at(p_iIndex) = p_tType;
    				else
    				{
    					delete m_vArray.at(p_iIndex);
                                            m_vArray.at(p_iIndex) = NULL;
    					m_vArray.at(p_iIndex) = p_tType;
    				}
    				return 0;
    			}
    			return -1;
    		}

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    If m_iMax is less than or equal to p_iIndex the memory will be leaked.

    You could write the inside of the if more concisely like this:
    Code:
    				delete m_vArray.at(p_iIndex);
    				m_vArray.at(p_iIndex) = p_tType;
    				return 0;
    Also, why not return bool? That would seem to be clearer.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    What is m_iMax?
    If you're storing the number of elements in the vector in it, which it looks like you might be, then you don't need to do that. vectors have a size() function that tells you how many items are in it. Furthurmore if you check against size() instead then you may as well use [] instead of .at() because then you've already checked for buffer overrun.

    In regards to returning bools, I'm going to assume you had zero meaning success and -1 meaning failure. The below simply returns true if it succeeds.

    So, what you have right now will simplify to this (also fixing the memory leak):
    Code:
    bool SetElement(unsigned int p_iIndex, Type* p_tType)
    {
        if ( p_iIndex < m_iMax )
        {
            delete m_vArray.at(p_iIndex);
            m_vArray.at(p_iIndex) = p_tType;
            return true;
        }
        delete p_tType;
        return false;
    }
    Furthurmore it looks like all this does is attempt to implement a ptr_container, in which case you should simply be using the ones from boost instead.
    Last edited by iMalc; 03-07-2008 at 02:08 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    it is a pointer container. I just like making things like this for educational purposes. I also like to write all my own code in my projects. More satisfying I guess.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21