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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:13:03+00:00 2026-06-09T02:13:03+00:00

Consider piece of code int main(int argc,char* argv[]){ int *arrPtr = new int[4]; for

  • 0

Consider piece of code

int main(int argc,char* argv[]){
        int *arrPtr = new int[4];
        for (int i=0;i<4;i++)
                arrPtr[i]=i*2;


        for (int i=0;i<10;i++){
                arrPtr++;
                cout<<"arrPtr ["<<i<<"]\t"<<*arrPtr<<endl;
        }
        cout<<endl;
        return 0; }

When I compile this program

g++ main.cpp -o main && ./main

It gives me

arrPtr [0]  2
arrPtr [1]  4
arrPtr [2]  6
arrPtr [3]  0
arrPtr [4]  135145
arrPtr [5]  0
arrPtr [6]  0
arrPtr [7]  0
arrPtr [8]  0
arrPtr [9]  0

I want to be able to detect (throw or check) when arrPtr is not pointing to “valid” element.
In the code above last valid element is arrPtr[3].

No “Me Too” answers please!

  • 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-09T02:13:06+00:00Added an answer on June 9, 2026 at 2:13 am

    Use a vector.

    int main()
    {
        std::vector<int> a(4);
    
        for (size_t i = 0; i < 4; ++i)
            a[i] = i * 2;
        for (size_t i = 0; i < 10; ++i)
            std::cout << i << ": " << a.at(i) << std::endl;
    }
    

    Result:

    0: 0
    1: 2
    2: 4
    3: 6
    terminate called after throwing an instance of 'std::out_of_range'
      what():  vector::_M_range_check
    Aborted
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this piece of code: struct Trade { float Price; char* time; int shares;
Consider the following piece of code: #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void)
Please consider the following piece of code: int main() { typedef boost::ptr_vector<int> ptr_vector; ptr_vector
Consider a piece of code like this: struct B { int c; B() {
Consider the following piece of code. int var; cout << (long)&var; My doubt is
Consider this piece of code: Uint counter = 0; int* p1; int* p2; deque<int>
Consider the following piece of code: char* str1 = new char [30]; char* str2
Consider this piece of code: int *a, *b; a = foo(); if (a) b
Consider the following piece of code: int i, k, m; k = 12; m
Consider the following piece of code - class MyThread extends Thread { private int

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.