Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6742629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:51:59+00:00 2026-05-26T11:51:59+00:00

I am compiling using g++. This is valid but I want to convert to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:51:59+00:00Added an answer on May 26, 2026 at 11:51 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Ubuntu 8.10 (Intrepid Ibex) and compiling C++ files with GCC, but when
I am Compiling this using g++ and a makefile. The g++ version is 4.1.2
i'm using this code on AIX/HP-UX/LINUX, but when i try to run it on
Greetings, SO. I have some code which I've made attempts at compiling using gcc,
I'm getting a lot of errors compiling code using the boost libraries, mainly when
After compiling a simple C++ project using Visual Studio 2008 on vista, everything runs
I was using mysql++ library and compiling with GCC 3.3.4. That GCC version had
I am compiling a YACC file using Cygwin shell on a PC. I am
I was compiling a C++ program in Cygwin using g++ and I had a
I'm compiling some C# and VB code at run time using the CodeDomProvider, CompilerInfo,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.