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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:44:14+00:00 2026-06-17T11:44:14+00:00

int * a; a = new int[10]; cout << sizeof(a)/sizeof(int); if i would use

  • 0
int * a;
a = new int[10];
cout << sizeof(a)/sizeof(int);

if i would use a normal array the answer would be 10,
alas, the lucky number printed was 1, because sizeof(int) is 4 and iszeof(*int) is 4 too. How do i owercome this? In my case keeping size in memory is a complicated option. How do i get size using code?

My best guess would be to iterate through an array and search for it’s end, and the end is 0, right? Any suggestions?

–edit

well, what i fear about vectors is that it will reallocate while pushing back, well you got the point, i can jus allocate the memory. Hoever i cant change the stucture, the whole code is releevant. Thanks for the answers, i see there’s no way around, so ill just look for a way to store the size in memory.

what i asked whas not what kind of structure to use.

  • 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-06-17T11:44:16+00:00Added an answer on June 17, 2026 at 11:44 am

    Simple.

    Use std::vector<int> Or std::array<int, N> (where N is a compile-time constant).

    If you know the size of your array at compile time, and it doens’t need to grow at runtime, then use std::array. Else use std::vector.

    These are called sequence-container classes which define a member function called size() which returns the number of elements in the container. You can use that whenever you need to know the size. 🙂

    Read the documentation:

    • std::array with example
    • std::vector with example

    When you use std::vector, you should consider using reserve() if you’ve some vague idea of the number of elements the container is going to hold. That will give you performance benefit.

    If you worry about performance of std::vector vs raw-arrays, then read the accepted answer here:

    • Is std::vector so much slower than plain arrays?

    It explains why the code in the question is slow, which has nothing to do with std::vector itself, rather its incorrect usage.


    If you cannot use either of them, and are forced to use int*, then I would suggest these two alternatives. Choose whatever suits your need.

    struct array
    {
          int *elements;  //elements
          size_t size;    //number of elements
    };
    

    That is self-explanatory.

    The second one is this: allocate memory for one more element and store the size in the first element as:

    int N = howManyElements();
    int *array = int new[N+1];  //allocate memory for size storage also!
    array[0] = N; //store N in the first element!
    
    //your code : iterate i=1 to i<=N 
    
    //must delete it once done
    delete []array;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A simple test app: cout << new int[0] << endl; outputs: 0x876c0b8 So it
int main() { char* NamePointer = new char; std::cout << Enter the file you
Code snippet (normal pointer) int *pi = new int; int i = 90; pi
I have an array: int[][] lawn = new int[980][1280]; wich stores the values of
Is it safe to use Microsoft specific _msize() function with new []? Example: int*
int* p_bob = new int; *p_bob = 78; The above code makes sense to
int[] arrc = new int[] {1, 2, 3}; System.out.println(new ArrayList(Arrays.asList(arrc))); prints address, but i
Having code: int** a = new int*[2]; a[0] = new int(1); a[1] = new
For example: m_array = new int[6][6]; m_array[0] = new int[]{2, 0, 0, 0, 0,
Given a list of elements like so: int[] ia = new int[] { -4,

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.