2

For instance:

int* pArray;
pArray = new array[];

instead of:

int* pArray;
pArray = new array[someNumber];

Since pointers are able to dynamically change the size of an array at run time, and the name of the pointer points to the first element of an array, shouldn't the default size be [1]? Does anyone know what's happening behind the scene?

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
Hollis
  • 39
  • 6
  • 1
    "Since pointers are able to dynamically change the size of an array at run time ...". Who told you this? If you heard it from a professor, either she was wrong, or you misheard her. If you read it in a book, let us know the name of the book so that we can recommend against its use. – Robᵩ Oct 06 '11 at 15:03
  • @Rob I must have misunderstood my professor. – Hollis Oct 06 '11 at 15:06
  • 1
    @TadeuszKopec: This is, I think, uncalled for. Many of the things we take for granted are not obvious to beginners, and this is *fortunate*. Innovation is driven by people trying to break free of old models, so I would not go about chastising people for asking questions (even when the answer seems obvious) as long as the questions (like this one) are well written. – Matthieu M. Oct 06 '11 at 15:59

8 Answers8

10

Since pointers are able to dynamically change the size of an array at run time

This is not true. They can't change the size unless you allocate a new array with the new size.

If you want to have an array-like object that dynamically changes the size you should use the std::vector.

#include<vector>
#include<iostream>

...
std::vector<int> array;

array.push_back(1);
array.push_back(2);
array.push_back(3);
array.push_back(4);

std::cout << array.size() << std::endl; // should be 4
Constantinius
  • 34,183
  • 8
  • 77
  • 85
1

When you create an array with new, you are allocating a specific amount of memory for that array. You need to tell it how many items are to be stored so it can allocate enough memory.

When you "resize" the array, you are creating a new array (one with even more memory) and copying the items over before deleting the old array (or else you have a memory leak).

JackWink
  • 666
  • 3
  • 10
0

Quite simply, C++ arrays have no facility to change their size automatically. Therefore, when allocating an array you must specify it size.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Great answer! I know that they can't change their size automatically but I also know that when you create an array, memory is allocated to hold the contents of that array. I wasn't sure if there was a 'default' size based on the type. Now I know, thanks. – Hollis Oct 06 '11 at 15:00
0

Pointers cannot change an array. They can be made to point to different arrays at runtime, though.

However, I suggest you stay away from anything involving new until you have learned more about the language. For arrays changing their size dynamically use std::vector.

sbi
  • 219,715
  • 46
  • 258
  • 445
  • If I stay away from new, how am I supposed to learn the language? I am aware of what an object and an instance are (and therefore, I understand the purpose of new) so when would you suggest that I start using new? Also, I suggest you stay away from commenting on stack until you can come up with constructive comments that don't rephrase the last two postings. – Hollis Oct 06 '11 at 14:57
  • @Hollis May I suggest that you show some more respect to people trying to help you by taking on their time to answer your questions? Especially when these people are valuable members of the community who have more than proven their skills on this site. – Luc Touraille Oct 06 '11 at 15:05
  • @Hollis: You use automatic (stack-based) objects. You can also use those that wrap dynamic (heap-based) objects, as does `std::vector` or `std::string`. Not messing up with `std::vector` and its iterators is complicated enough for a beginner. When I taught C++, `std::vector` was introduced in about the third lecture, `new` maybe in the tenth. – sbi Oct 06 '11 at 15:09
  • @Hollis: As for your suggestion: Please don't be rude. When I wrote my answer, nobody had yet made the points I made. An earlier answer has since been altered to include it (and I upvoted it for that), but it hadn't back then. – sbi Oct 06 '11 at 15:09
  • @sbi We haven't even covered vectors in my class. We've gone from pointers to linked lists so far and he's never mentioned vectors. I misunderstood what my professor was saying about being able to resize arrays at runtime. I went back over my notes and essentially, he was saying that a user could specify the size of and create a new array at runtime, not alter the size of a given array at runtime. – Hollis Oct 06 '11 at 15:35
  • @sbi Also, I apologize for being rude. I saw that your comment was third on the list and posted several minutes apart from the others so I assumed that they had already posted before you posted yours. – Hollis Oct 06 '11 at 15:37
  • @Hollis: Then you are getting a "traditional" introduction to C++. Too bad. You might want to have a look at [a good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – sbi Oct 06 '11 at 16:05
0

Pointers point to dynamically allocated memory. The memory is on the heap rather than the stack. It is dynamic because you can call new and delete on it, adding to it and removing from it at run time (in simple terms). The pointer has nothing to do with that - a pointer can point to anything and in this case, it just happens to point to the beginning of your dynamic memory. The resizing and management of that memory is completely your responsibility (or the responsibility of the container you may use, e.g. std::vector manages dynamic memory and acts as a dynamic array).

John Humphreys
  • 37,047
  • 37
  • 155
  • 255
0

They cannot change the size dynamically. You can get the pointer to point to a new allocation of memory from the heap.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

Behind the scenes there is memory allocated, a little chunk of silicium somewhere in your machine is now dedicated to the array you just newed.

When you want to "resize" your array, it is only possible to do so in place if the chunk of silicium has some free space around it. Most of the times, it is instead necessary to reserve another, bigger, chunk and copy the data that were in the first... and obviously relinquish the first (otherwise you have a memory leak).

This is done automatically by STL containers (like std::vector or std::deque), but manually when you yourself call new. Therefore, the best solution to avoid leaks is to use the Standard Library instead of trying to emulate it yourself.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
-1

int *pArray = new int; can be considered an array of size 1 and it kinda does what you want "by default".

But what if I need an array of 10 elements? Pointers do not have any magical abilites, they just point to memory, therefore:

pArray[5] = 10; will just yield a run-time error (if you are lucky).

Therefore there is a possibility to allocate an array of needed size by calling new type[size].

Grozz
  • 8,317
  • 4
  • 38
  • 53
  • -1 "`int *pArray = new int;` can be considered an array of size 1 and it kinda does what you want "by default"." - This is nonsense. – UncleBens Oct 06 '11 at 14:49
  • maybe it's my poor english, what I meant was he originally wrote that he wants `pArray = new array[];` creating array of 1 element to be the default behavior, that's what `new int;` does. And it is almost exactly an array of size 1. – Grozz Oct 06 '11 at 17:03
  • The asker seems to be confused as it is. Now you are inviting him to mix `new` / `new[]` and `delete` / `delete[]`? If you are going to be allocating arrays dynamically in the first place, you have to be very careful and know what you are doing. – UncleBens Oct 06 '11 at 17:18