Pointers & dynamic variables. Why does this work?

Hello,

Quick intro: I'm a professional front-end web developer & graphic designer, with some experience with Java. I am trying to learn C++ just for a hobby.

I'm going through a text book (Sam's teach yourself C++), and have been writing little programs to help cement the lessons along the way.

I thought I understood what the book was saying about pointers and dynamic numbers of objects: that the programmer has to set aside memory space on the heap in order to create a dynamic amount of objects at runtime; and the way to do this is to use the "new" keyword to create a pointer to that memory space.

However, I tried writing a program that defied that logic, and it worked just fine, leaving my quite confused.

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

#include <iostream>
using namespace std;

main()
{

    cout << "Enter no. of dynamic variables to create: ";

    int count = 0;
    cin >> count;

// *** I was expecting one of the below two lines to cause a compiler error ***

    // create dynamic array
    int numbers[count]; // "new" keyword -not- used

    // fill numbers array with dynamic amount of ints
    for (int x = 0; x < count; x++)
        numbers[x] = x; // "new" keyword -not- used

    // Print variables' memory locations
    for (int x = 0; x < count; x++)
        cout << endl << &numbers[x];

    cout << endl << endl; // just for neatness
}


Second question, why do I need to create a pointer with the new keyword, why not just create an object like in Java? I guess I can do the same thing as below, I just don't understand why it has to be like that, and I'm afraid it's because I don't really understand the whole deal with pointers. Maybe my head is still in Java-world.

If someone can explain, I'd appreciate it.

1
2
3
4
5
6
7
8
9
10
11

MyObject * p_obj = new MyObject();
MyObject obj = *p_obj;

// OR -->

MyObject obj = *(new MyObject());

// Why the need to use a pointer? Why not just MyObject o = new MyObject(); ?

Last edited on
squashedMangoes wrote:
However, I tried writing a program that defied that logic, and it worked just fine, leaving my quite confused.

Yes, this is not standard C++ code, but some compilers (e.g. GCC) support it as an extension.
( http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html )

squashedMangoes wrote:
Second question, why do I need to create a pointer with the new keyword, why not just create an object like in Java?

That's how things work in C++. The address (pointer) of the newly created object is required in order to free the memory after you're done working with it (C++ doesn't have garbage collection*). I believe Java does it like this because, after all, in Java every user-defined object is allocated on the heap and represented by a pointer internally (correct me if I'm wrong here, I'm not very familiar with Java).

More info on dynamic memory allocation -> http://cplusplus.com/doc/tutorial/dynamic/

* Though, there are constructs that attempt to provide this feature:
http://cplusplus.com/reference/std/memory/auto_ptr/
And, of course, you can create your own if you like...
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
#include <iostream>
using namespace std;

void main()
{

	cout << "Enter no. of dynamic variables to create: ";

	int count = 0;
	cin >> count;

	// *** I was expecting one of the below two lines to cause a compiler error ***

	// create dynamic array
	int* numbers = new int[count]; // "new" keyword -not- used

	// fill numbers array with dynamic amount of ints
	for (int x = 0; x < count; x++)
		numbers[x] = x; // "new" keyword -not- used

	// Print variables' memory locations
	for (int x = 0; x < count; x++)
		cout << endl << numbers[x];

	cout << endl << endl; // just for neatness
	//return 0;
}
m4aster:

Thanks very much for taking the time to help out.

That answers both my questions.

I wonder if there is someway of setting GCC (that is indeed what I am using) to some kind of "strict mode". I don't want to confuse myself while learning.




bolo809:

Thanks mate, but what is confusing me was that my version does work without your modifications. I wasn't asking to fix it, I was asking why it's not broken! ;-)
Last edited on
squashedMangoes wrote:
I wonder if there is someway of setting GCC (that is indeed what I am using) to some kind of "strict mode". I don't want to confuse myself while learning.

I believe this will help:

By default, GCC provides some extensions to the C++ language; See Options Controlling C++ Dialect. Use of the -std option listed above will disable these extensions. You may also select an extended version of the C++ language explicitly with -std=gnu++98 (for C++98 with GNU extensions) or -std=gnu++0x (for C++0x with GNU extensions). The default, if no C++ language dialect options are given, is -std=gnu++98.

Found here -> http://gcc.gnu.org/onlinedocs/gcc/Standards.html
Topic archived. No new replies allowed.