开发者

c++ what is "pointer = new type" as opposed to "pointer = new type []"?

In many tutorials, the first code samples about dynamic memory start along the lines of:

int * pointer;
pointer = new int;        // version 1
//OR
pointer = new int [20];    // version 2

They always proceed to explain how the second version works, but totally avoid talking about the first version.

What I want to know is, what does pointer = new int create? What can I do with it? What does it mean? Every tutorial without fail will avoid talking about the first version entirely. All I've found out (through messing about) is this:

#include <iostream>

using namespace std;

int main()
{
    int * pointer;
    pointer = new int;
   pointer[2] = 1932;   // pointer [2] exists? and i can  assign to it?!
   cout << pointer[2] << endl;      // ... and access it successfully?!
};

The fact that I can subscript pointer tells me so far that pointer = new int implicitly creates an array. But if so, then what si开发者_如何学编程ze is it?

If someone could help clear this all up for me, I'd be grateful...


My teacher explained it like this.
Think of cinema. The actual seats are memory allocations and the ticket you get are the pointers.

int * pointer = new int;

This would be a cinema with one seat, and pointer would be the ticket to that seat

pointer = new int [20] 

This would be a cinema with 20 seats and pointer would be the ticket to the first seat. pointer[1] would be the ticket to the second seat and pointer[19] would be the ticket to the last seat.

When you do int* pointer = new int; and then access pointer[2] you're letting someone sit in the aisle, meaning undefined behaviour


This is a typical error in C and C++ for beginners. The first sentence, creates a space for holding just an int. The second one creates a space for holding 20 of those ints. In both cases, however, it assigns the address of the beginning of the dynamically-reserved area to the pointer variable.

To add to the confusion, you can access pointers with indices (as you put pointer[2]) even when the memory they're pointing is not valid. In the case of:

int* pointer = new int;

you can access pointer[2], but you'd have an undefined behavior. Note that you have to check that these accesses don't actually occur, and the compiler can do usually little in preventing this type of errors.


This creates only one integer.

pointer = new int;        // version 1

This creates 20 integers.

pointer = new int [20]    // version 2

The below is invalid, since pointer[2] translates as *(pointer + 2) ; which is not been created/allocated.

int main()
{
    int * pointer;
    pointer = new int;
   pointer[2] = 1932;   // pointer [2] exists? and i can  assign to it?!
   cout << pointer[2] << endl;      // ... and access it succesfuly?!
};

Cheers!


new int[20] allocates memory for an integer array of size 20, and returns a pointer to it.

new int simply allocates memory for one integer, and returns a pointer to it. Implicitly, that is the same as new int[1].

You can dereference (i.e. use *p) on both pointers, but you should only use p[i] on the pointer returned by the new int[20].

p[0] will still work on both, but you might mess up and put a wrong index by accident.

Update: Another difference is that you must use delete[] for the array, and delete for the integer.


pointer = new int allocates enough memory on the heap to store one int.

pointer = new int [20] allocates memory to store 20 ints.

Both calls return a pointer to the newly allocated memory.

Note: Do not rely on the allocated memory being initialized, it may contain random values.


pointer = new int; allocates an integer and stores it's address in pointer. pointer[2] is a synonym for pointer + 2. To understand it, read about pointer arithmetic. This line is actually undefined behavior, because you are accessing memory that you did not previously allocate, and it works because you got lucky.


int* p = new int allocates memory for one integer. It does not implictly create an array. The way you are accessing the pointer using p[2] will cause the undefined behavior as you are writing to an invalid memory location. You can create an array only if you use new[] syntax. In such a case you need to release the memory using delete[]. If you have allocated memory using new then it means you are creating a single object and you need to release the memory using delete.


*"The fact that i can subscript pointer tells me so far that I pointer = new int implicitly creates an array. but if so, then what size is it?" *

This was the part of the question which I liked the most and which you emphasize upon.

As we all know dynamic memory allocation makes use of the space on the Stack which is specific to the given program. When we take a closer look onto the definition of new operator :-

void* operator new[] (std::size_t size) throw (std::bad_alloc);

This actually represents an array of objects of that particular size and if this is successful, then it automatically Constructs each of the Objects in the array. Thus we are free to use the objects within the bound of the size because it has already been initialized/constructed.

int * pointer = new int;

On the other hand for the above example there's every possibility of an undefined behaviour when any of

*(pointer + k) or *(k + pointer)

are used. Though the particular memory location can be accessed with the use of pointers, there's no guarantee because the particular Object for the same was not created nor constructed.This can be thought of as a space which was not allocated on the Stack for the particular program.

Hope this helps.


It does not create array. It creates a single integer and returns the pointer to that integer. When you write pointer[2] you refer to a memory which you have not allocated. You need to be carefull and not to do this. That memory can be edited from the external program which you, I belive, don't want.


int * pointer; pointer = new int;  // version 1
//OR 
pointer = new int [20]             // version 2 

what I want to know is, what does pointer = new int create? what can I do with it? what does it mean? Every tutorial without fail will avoid talking about the first version entirely

The reason the tutorial doesn't rell you what to do with it is that it really is totally useless! It allocates a single int and gives you a pointer to that.

The problem is that if you want an int, why don't you just declare one?

int i;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜