writing and printing out constructor pointer

Expanding my learning on c++, definitely now that i am in the deps of c++, going to need a lot of help from all of you pros :)

so i have the code below, the purpose is just to print:
1. the constructor which is pointer a that takes 2 parameters --- DONE
2. print out the pointer memory address -- DONE
3. print out the default constructor.... hmmm i messed something up :(

also, if you can check my code, see if there is anything i can do to make it more cleaner and your thoughts on it? again beginner :(

thanks in advance :)

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
53
54
55
56
57
58
59
60
61
62

#include <iostream>


struct Vector2
{
private:
	float x, y;
public:
	Vector2()
		: x(0), y(0)
	{
		std::cout << x << "," << y << std::endl;
	}
	Vector2(int X, int Y)
		: x(X),y(Y)
	{ 
		std::cout << X << "," << Y << std::endl;
	}

	void Printout()
	{
		std::cout << "Printing out " << x << "," << y << std::endl;
	}

	void Printout(int x,int y)
	{
		std::cout << "Printing out " << x << "," << y << std::endl;
	}

	~Vector2() { std::cout << "Got Destroyed" << std::endl; }
};


void printing(int x, int y)
{
	std::cout << x << "," << y << "TEST" << std::endl;
}

int main()
{
	Vector2* a;

	Vector2* b = a;
	//b->x = 2;

	a->Printout(5, 5);	
	//std::cout << b->Printout() << std::endl;
	

	std::cout << &a << std::endl;
	std::cout << &b << std::endl;

	//a->~Vector2();

	//b->~Vector2();

	std::cin.get();


}
Where are you allocating memory for those pointers?

Note on line 44 you're attempting to use a variable that is not initialized.

hey jlb,

thanks for your input, i was using stack memory, so i would assume it is allocating memory automatically.

also, for line 44, i thought i was hence Vector2 has a default initializer that takes in no parameters but uses x and y default which is assigned 0 for both:

1
2
Vector2()
		: x(0), y(0)


let me know and what are your suggestions? :)
I'm not asking where the Vector2 was initialized, I asked where are the pointers initialized (allocated memory). This has nothing to do with any class constructor.

also, for line 44, i thought i was hence Vector2 has a default initializer

Again it is not the Vector2 that we are talking about it is the pointer to that Vector2 that is uninitialized.

i thought i was doing that on line 42:
 
Vector2* a;

and on line 44
 
Vector2* b = a;


do i need to just put:
 
	Vector2* b = {0};


also, if you see line 10-14, how can i just use that to display/print out x = 0 and y = 0?
1
2
3
4
5
Vector2()
		: x(0), y(0)
	{
		std::cout << x << "," << y << std::endl;
	}


thank you for taking the time jlb, but if you can give me a hint or something?
i thought i was doing that on line 42:

No all that is doing is creating a pointer to a Vector2, but it doesn't allocate any memory to that pointer (hence it's uninitialized).

and on line 44

Since 'a' is not initialized 'b' will also be uninitialized (pointing to some random memory location).

Normally in C++ the idea is to avoid pointers, in this program there is no real need for pointers, you could just use "normal" non-pointer instances.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
        // Create a Vector2, default initialized.
	Vector2 a;

        // Copy Vector2 a to Vector2 b.
	Vector2 b = a;
	
	a.Printout();
	
	
        // Print the address of the two instances.
	std::cout << &a << std::endl;
	std::cout << &b << std::endl;
}


If pointers are required then you need to use new to allocate memory for 'a', and don't forget to delete what you new.
thanks jlb for bringing this to my attention, though i can definitely see how easier it is, but i want to learn all there is about c++, including practicing it, so i wanted to make pointers and maybe after making a basic pointer move onto the smart pointers (shared and unique), using the same code.

but if you can tell me how to allocate memory for my pointer, or if you can give me a good link to reference on how to allocate memory for an int, char, string or even a class?

and yes, definitely need to delete the pointer after done:

1
2
delete  a;
delete b;


is that correct above?

i thought you would have to do that if you used the word "new" such as:

 
Vector2* a = new Vector2();


then that would make it a heap and not a stack, so you would have to manually delete it, and if its a smart pointer like a unique or shared, then you wouldnt need to delete correct?


again thanks again for your input :)
is that correct above?

Probably not. How many times did you call new?


i thought you would have to do that if you used the word "new" such as:

Well did you try looking up some documentation for "C++ new" using your trusty search engine? What did the documentation say?

You clearly already know how to dynamically allocate memory on the heap.

If you want to create an int, or a Vector2, on the stack, you just declare it normally. That's what happens when you declare a local variable - it is created on the stack.

But creating a pointer to an object doesn't magically create an object for that pointer to point to. Nowhere in your code have you created a Vector2 object, on the stack or on the heap. Nowhere in your code have you set your pointers to actually point to any objects.

Do you understand what a pointer actually is?
Last edited on
1
2
3
// b points to a which is allocated on the stack.
Vector2 a;
Vector2* b = &a;

Using delete b; here would be a mistake because the object that is pointed to was not created with new.
Topic archived. No new replies allowed.