Methods arguments alteration rules in Java and C/C++

Hello and thank you for reading my post.

First of all, I am more a Java programmer in my programming practice...
I realized recently (to my very great astonishment and displeasure) that, considering a Java method argument, if this argument is changed INSIDE the method, the changes persist after the method has exited.
This doesn't apply to arguments which type is primitive ("int", "double", ...) or immutable ("String", "Timestamp", ...).

So I wanted to check how it works in C/C++.
Below is the code I wrote for that purpose.
This is a simple code with two classes "Point" and "PairPoints" (see the corresponding .h and .cpp files) and a file "main.cpp" which contains the "main()" method.

I would like to ask you two questions about this code.
Note that even if I'm casting doubt on that code, it compiles and executes properly.

1) Is the method "PairPoints::trickyOrNot2()" correct or not?
Is it correct to pass an object "as is" as an argument to a method in C++?

The advantage I see with this method is that, if the object "point" passed as an argument, is altered inside the method, these changes are not persistent after the method has exited.

Yet, I am not sure this is current practice in C++ to pass objects "as is" as arguments to methods (not as pointers, like with "Point * p_point").
In comparison and as far as I know, this is all what is possible in Java...

Instead, with the methods "PairPoints::trickyOrNot()" and "PairPoints::trickyOrNot3()", the arguments "Point * p_point" and "Point & point" changes persists after the method has exited.

Note that, as far as I know, in Java, there is no way to indicate that, if a method argument state changes inside a method, these changes shouldn't persist after the method has exited...

2) What do you think about the way the objects are allocated and deallocated?
I know there are some other methods but is this one correct (see "malloc", "free", "Point p1(1, 2);", "PairPoints pairPoints(&p1, &p2);")?

Can you clarify these points?
Best regards,


Point.h
1
2
3
4
5
6
7
8
9
10
11
class Point
{
    public:
        Point();
        Point(int n_x, int n_y);
        virtual ~Point();

    public:
        int mn_x;
        int mn_y;
};

Point.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Point.h"

Point::Point()
{
    //ctor
}

Point::Point(int n_x, int n_y)
{
    mn_x = n_x;
    mn_y = n_y;
}

Point::~Point()
{
    //dtor
}

PairPoints.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "Point.h"
#include <stdlib.h>

class PairPoints
{
    public:
        PairPoints();
        PairPoints(Point * mp_point1, Point * mp_point2);
        virtual ~PairPoints();

    public:
        Point * mp_point1;
        Point * mp_point2;

    public:
        void trickyOrNot(Point * p_point, int & n_anInt);
        void trickyOrNot2(Point point, int n_anInt);
        void trickyOrNot3(Point & point);
};

PairPoints.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "PairPoints.h"

PairPoints::PairPoints()
{
    //ctor
}

PairPoints::PairPoints(Point * p_point1, Point * p_point2)
{
    //Point point1;
    //mp_point1=&point1;

    mp_point1 = (Point *) malloc(sizeof(Point));
    mp_point1->mn_x = p_point1->mn_x;
    mp_point1->mn_y = p_point1->mn_y;

    mp_point2 = (Point *) malloc(sizeof(Point));
    mp_point2->mn_x = p_point2->mn_x;
    mp_point2->mn_y = p_point2->mn_y;
}

PairPoints::~PairPoints()
{
    free(mp_point1);
    free(mp_point2);
}


void PairPoints::trickyOrNot(Point * p_point, int & n_anInt)
{
    if(p_point != NULL)
    {
        p_point->mn_x = 10;
        p_point->mn_y = 20;

        n_anInt = 6;
    }
}

void PairPoints::trickyOrNot2(Point point, int n_anInt)
{
    point.mn_x = 50;
    point.mn_y = 60;

    n_anInt = 7;
}

void PairPoints::trickyOrNot3(Point & point)
{
    point.mn_x = 50;
    point.mn_y = 60;
}

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "PairPoints.h"


using namespace std;

int main( int argc, const char* argv[] )
{
Point p1(1, 2);
Point p2(3, 4);
PairPoints pairPoints(&p1, &p2);
int n_anInt;

    cout << "Pair point 1 x = " << pairPoints.mp_point1->mn_x << endl;
    cout << "Pair point 1 y = " << pairPoints.mp_point1->mn_y << endl;

    cout << "Pair point 2 x = " << pairPoints.mp_point2->mn_x << endl;
    cout << "Pair point 2 y = " << pairPoints.mp_point2->mn_y << endl;

    n_anInt = 3;
    pairPoints.trickyOrNot(&p1, n_anInt);

    cout << "Point 1 x = " << p1.mn_x << endl;
    cout << "Point 1 y = " << p1.mn_y << endl;
    cout << "n_anInt   = " << n_anInt << endl;

    n_anInt = 4;
    pairPoints.trickyOrNot2(p1, n_anInt);

    cout << "Point 1 x = " << p1.mn_x << endl;
    cout << "Point 1 y = " << p1.mn_y << endl;
    cout << "n_anInt   = " << n_anInt << endl;

    pairPoints.trickyOrNot3(p1);

    cout << "Point 1 x = " << p1.mn_x << endl;
    cout << "Point 1 y = " << p1.mn_y << endl;

	cin.get();
}
Where to start with this? So many problems.

point.cpp line 5: ctor does not initialize default values.

pairpoints.h lines 12-13: Why are these pointers?

pairpoints.cpp lines 13,17: NEVER allocate a C++ object using malloc. The object constructor won't get called. Always allocate a C++ object via new

pairpoints.cpp lines 14,15 and 18,19: These should be set via the objects constructor.

pairpoints.cpp lines 24,24: NEVER release a C++ object by calling free. It's destructor won't get called. Always use delete. Also, if PairPoints default constructor is called, the free statements will fail because mp_point1 and mp_point2 never got allocated.

pairpoints.cpp lines 42-45: THese lines do nothing. point was pased by value, therefore these statements modify a local copy but the changes are never seen by the caller.

And you have a memory leak because the points you allocated never get released.


Topic archived. No new replies allowed.