2

I am compiling using g++. This is valid but I want to convert to the C++ operators new() and delete() because this is considered best practice.

  int *scratch = (int *)malloc(size * sizeof(int));
  free(scratch);

What is the equivalent using new and delete? This is my guess.

 int **scratch = new int*[size];
 delete[] scratch;

This is for an array of pointers.

The double star is a reference. It is a pointer to a pointer.

  • 4
    A reference and a pointer to a pointer are _very_ different things. – Mooing Duck Oct 27 '11 at 21:37
  • Your link is broken. A pointer is close to a reference. So a pointer to a pointer is close (conceptually) to a reference to a pointer. But either way they're used differently, with different limitations, and different syntax. – Mooing Duck Oct 27 '11 at 21:47
  • Your first sentence contradicts your code: It is indeed true that `::operator new()` is the C++ analogue of `malloc()`, and the former is in fact almost [universally implemented by the latter](http://stackoverflow.com/questions/7443782/does-dynamic-memory-allocation-differ-in-c-and-c-in-popular-implementations). However, what you're using in the code is the `new` **expression**`, which is a different animal entirely (as it has something to do with constructors). – Kerrek SB Oct 27 '11 at 21:49
  • Maybe conceptually (although that concept would be a really mysterious one), but it is definitely not equivalent to your `malloc` example. C'mon you already know how pointers work, they're not any different in C++. And I don't know what reference you are talking about, but I never heard somebody call a pointer to pointer a reference, at least not in C++, where the word reference has an entirely different meaning, anyway. – Christian Rau Oct 27 '11 at 21:50
  • Search for reference here... http://cslibrary.stanford.edu/103/LinkedListBasics.pdf - this is not broken..a reference is a pointer to a pointer....but syntactically they are different –  Oct 27 '11 at 21:54
  • new and delete are C++ operators while malloc and calloc are functions frequently used to implement new and delete –  Oct 27 '11 at 21:57
  • The 4 people that upvoted Mooing Duck are flat wrong...they are not very different things –  Oct 27 '11 at 21:58
  • My guess is that pre-C++ a pointer to a pointer was called a reference...when C++ implmented this with the &...this concept was lost too most, as it was no longer expressed explicitly. What do you all think a reference is in terms of data and addresses? –  Oct 27 '11 at 22:01
  • @ChrisAaker No you're plain wrong! You cannot just generalize from the linked paper terminology. In the words of this paper, you can call a pointer to a pointer a reference **to a pointer**, depending on the context (in this case function parameters), and not just a reference in general. That's what they all complain about, with right. But even then in your example it doesn't make any sense. – Christian Rau Oct 27 '11 at 22:01
  • @Christian..O.K what is a refence than? Other than the syntax &...what is it? –  Oct 27 '11 at 22:03
  • @ChrisAaker Conceptually, a reference is an alias to another variable. Practically, a C++ reference is a pointer without some of the pointer functionality (artihmetic, repointing). But even in a very general language-agnostic terminology, your above thing is in no way a reference, but a pointer of pointers. It is the context (like function parameters), that gives a type (like a pointer to pointer) the meaning of a reference (in this case a reference to a pointer). But you cannot just go along and call a pointer to a pointer a reference. – Christian Rau Oct 27 '11 at 22:07
  • Inteanlly, though a reference is implemented as a pointer to pointer..when you use &...that is what is happening at the address level –  Oct 27 '11 at 22:11
  • (Just in case you were curious) C++ has its built in "& argument" feature to implement reference parameters for the programmer. –  Oct 27 '11 at 22:12
  • @Christian...read the whole article –  Oct 27 '11 at 22:13
  • The short story is, append an '&' to the type of a parameter, and the compiler will automatically make the parameter operate by reference for you. –  Oct 27 '11 at 22:13
  • Which in the context of the article a reference is ** –  Oct 27 '11 at 22:13
  • @ChrisAaker No! A reference is not implemented as a pointer to pointer, but just as a pointer. A reference to a pointer would be a pointer to a pointer, technically, the same as a reference to an int would be a pointer to an int, technically. – Christian Rau Oct 27 '11 at 22:14
  • How else would you implement a reference? not in a language context..but in a memory and data context –  Oct 27 '11 at 22:14
  • @ChrisAaker I've explained that. In a memory and data (hardware-like, if you want) context, I would implement a reference as a pointer. A reference to a pointer as a pointer to a pointer and a reference to an int as a pointer to an int, but a reference without a type just has no meaning, and it's definitely not a pointer to a pointer, only if it yould refer to a pointer, but then the reference part alone is just a pointer (thouhg, to a pointer), but not pointer to a pointer. – Christian Rau Oct 27 '11 at 22:19
  • Wehter it is a pointer to a pointer...or just a pointer..you proved my point that they are related...and not "completely different things" as stated above....in fact you prove my point more than I do as you claim they are the same thing in hardware where as I claim a referecne is a pointer to a pointer. –  Oct 27 '11 at 22:21
  • @ChrisAaker I never said they are the same thing (even in hardware). A reference is not a pointer to a pointer, but just a pointer (do you at least notice the lack of a **pointer** in the second term, now after the tenth time?). And specifically in your `new` example the whole term doesn't fit in any way, and neither was it related to the `malloc` example. – Christian Rau Oct 27 '11 at 22:24

5 Answers5

5

You probably want to do:

std::vector<int> scratch(size);

But for the record, what you tried should be:

int* scratch = new int[size];
delete[] scratch;

Note that if size is known at compile-time, you also simply do:

int scratch[size];

Edit

For an array of pointers to int, the desired syntax is:

int** scratch = new int*[size];
delete[] scratch;

Note that the the pointers inside the array are not refering to allocated memory and that you must allocate every pointed int before being able to use them.

That is, you may do:

int a = 3;
int b = 5;

scratch[0] = &a;
scratch[1] = &b;
scratch[2] = &a;
scratch[3] = new int(5); // Well, I don't see why anyone would like to do that but if you do, don't forget to also call delete on the pointer when you are done with it.

Anyway, unless you have some particular constraints, the most C++ way of doing things is probably still a std::vector<int*> as dealing with pointers of pointers becomes a bit unmaintainable after some levels of indirection.

ereOn
  • 53,676
  • 39
  • 161
  • 238
  • @ChrisAaker Your `malloc` example doesn't use an array of pointers either. If that is what you want your `new` example was indeed correct and your `malloc` example was broken. – Christian Rau Oct 27 '11 at 21:40
  • O.K. I see any chance you would add info. on how to do pointer to pointers...using malloc...as another EDIT on you answer...Thanks! –  Oct 27 '11 at 21:45
  • the MOST C++ way is `std::vector >` :) – totowtwo Oct 27 '11 at 21:49
  • @totowtwo: `std::auto_ptr` is deprecated in C++11. That would rather be `std::vector>` if anything. Anyway, we don't know about the OP requirements (perhaps the array or the vector **does not own** the pointed integers so a `std::vector` is good enough in this situation. – ereOn Oct 27 '11 at 21:50
2

int *scratch = new int[size]; delete[] scratch;

or

std::vector<int> scratch(size);

or for an array of pointers

std::vector<std::auto_ptr<int> > scratch; 
for(int i = 0; i < size; i++) {     
    scratch.push_back(std::auto_ptr(new int)) 
}
totowtwo
  • 2,101
  • 1
  • 14
  • 21
  • You don't want an array of pointers in your question. You probably don't want an array of pointers in general. – totowtwo Oct 27 '11 at 21:46
1
int **scratch = new int*[size];

How did you come up with this? What about the simple

int *scratch = new int[size];

The delete was used correctly, though.

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
0

You are close. It's:

int *scratch = new int[size];
delete [] scratch;
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
  • How do you distinguish between an array of pointers and an array of ints? –  Oct 27 '11 at 21:38
0

The equivalent is:

int *scratch = new int[size];
delete[] scratch;

In short, for arrays (T*)malloc(size * sizeof(T)) translates as new T[size] and free(ptr) into delete[] ptr.

K-ballo
  • 80,396
  • 20
  • 159
  • 169