Thread: operator+ loses pointer in member object

  1. #1
    Registered User
    Join Date
    Mar 2009

    Question operator+ loses pointer in member object

    I have a C++ class with this data member:

    		char ** data;
    The memory for this variable is properly allocated and the variable is usable throughout the entire program, but after using the overloaded plus operator (implemented as a friend function) my program gets an Invalid Allocation Size error.

    Here is an example of a line that errors. s1, sa3, and sa4 are all properly created stringArray objects:

                    s1 = sa3 + sa4;
    Here is the operator+ function:

    		 stringArray& operator+(const stringArray& arr1, const stringArray& arr2)
    			 stringArray temp(arr1.count + arr2.count);
    			 for(size_t i = 0; i < arr1.count; i++)
    				 if([i] != NULL)
    			 for(size_t i = 0; i < arr2.count; i++)
    			 	if([i] != NULL)
    			 return temp;
    temp gets correctly created and has all of the strings created, but once the function returns, the data variable is an undefined pointer.

    I think the issue is that temp is created on the stack. I do not know what convention C++ uses to return object, but I assume that they put the memory location of the object in the return register. If this is the case, I understand that when the stack was decremented, that the memory location is lost.

    I am hoping someone can explain to me how other people create the operator+ function when they have a pointer in their class, it seems like the only thing to do would be to return a point (created in heap) and take the memory leak.

    Thank you.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    Return by value, not reference. Make sure you copy constructor works correctly.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Also, implement + in terms of +=. I've never come across a regular (as in, didn't do weird things) + operator that didn't look or shouldn't have looked like this:
    Whatever operator +(const Whatever& lhs, const Whatever& rhs) {
      Whatever t(lhs);
      t += rhs;
      return t;
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to a Member Function
    By TheDan in forum C++ Programming
    Replies: 25
    Last Post: 04-03-2006, 08:18 PM
  2. Dereference pointer to void pointer to member
    By phil in forum C Programming
    Replies: 5
    Last Post: 04-20-2005, 11:54 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. syntax to pass a member function pointer to another class?
    By reanimated in forum C++ Programming
    Replies: 4
    Last Post: 11-27-2003, 05:24 PM
  5. Testing object pointer for null
    By VanJay011379 in forum C++ Programming
    Replies: 4
    Last Post: 07-24-2002, 10:51 AM