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 3840762
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:34:02+00:00 2026-05-19T15:34:02+00:00

Bit confused about usage of new to allocate memory dynamically. e.g. If I need

  • 0

Bit confused about usage of new to allocate memory dynamically.

e.g. If I need to allocate memory for 100 ints(assuming int is 4 bytes), should I say :

int *ptr = new int[100];

or

int *ptr = new int[100*4]; //assuming int is 4 bytes.
  1. Basially new operator allocates memory in bytes or that many bytes of type T used while invoking the new operator?

  2. If my class doesn’t have a allocator member function defined, & i need to allocate an array of object of that class type, will the new oeprator find the sizeof(class type) and allocate accordingly or how would it work?

EDIT:

Sorry for clubbing multiple questions, but its related:

will this piece of code work fine if i want to allocate a 2D array of size [100][4] of ints

int *arr = new int [100][4];

thank you.

-AD

  • 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-19T15:34:02+00:00Added an answer on May 19, 2026 at 3:34 pm

    The size given to new is the number of items, not the memory size. However, consider using std::vector instead.

    For example, new int[100] allocates at least 100 * sizeof(int) bytes (which is 400 when sizeof(int) is 4); any more it allocates will be due to implementation and runtime details which you (except for very rarely) cannot depend on.

    If you don’t have an operator new or operator new[] in your class (which you usually shouldn’t), then the global versions will be used, and they will use sizeof(your_type) correctly.


    Did you try the code in the update?

    Multidimensional arrays are actually arrays of arrays; so new returns a pointer to the first item just as it does for single dimensional arrays, which is a pointer to an array:

    typedef int int4[4];  // for clarity
    int4 *arr = new int4[100];
    // above two lines are identical to:
    int (*arr)[4] = new int[100][4];
    

    Again, you’re almost always better off using a container than managing this yourself. That’s vector, et. al., but also containers like boost::array and dedicated “matrix” types for “square” 2-dimensional arrays.

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

Sidebar

Related Questions

I'm a bit confused about the usage of Abstract classes in C#. In C++,
I am bit confused about the difference between the usage of std::remove algorithm. Specifically
i'm a bit confused about modules, namespace and reflection. $obj = new default_Model_foo(); $obj->bar();
I'm a bit confused about the streams... which is which? Simply, which stream should
I am a bit confused about the usage of cudaEvent_t . Currently, I am
I'm a bit confused about proper usage of svn in case of a single
I'm little bit confused about constants naming. kName NameKey NAME_KEY Definitely usage of these
I'm a bit confused about the memory management examples regarding setters floating around various
I am a bit confused about socket programming in C. You create a socket,
I am bit confused about ADO.Net Data Services. Is it just meant for creating

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.