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

  • Home
  • SEARCH
  • 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 7723727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:30:54+00:00 2026-06-01T04:30:54+00:00

#include <iostream> using namespace std; int main(void){ int size = -2; int* p =

  • 0
#include <iostream>

using namespace std;

int main(void){
    int size = -2;
    int* p = new int[size];
    cout<<p<<endl;
    return 0;
}

Above code compiles without any problem on visual studio 2010.

But

#include <iostream>

using namespace std;

int main(void){
    const int size = -2;
    int* p = new int[size];
    cout<<p<<endl;
    return 0;
}

But this code(added const keyword) gives error on compilation(size of array cannot be negative).

Why these different results ?

  • 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-01T04:30:55+00:00Added an answer on June 1, 2026 at 4:30 am

    By making it a constant expression, you’ve given the compiler the ability to diagnose the problem at compile time. Since you’ve made it const, the compiler can easily figure out that the value will necessarily be negative when you pass it to new.

    With a non-constant expression, you have the same problem, but it takes more intelligence on the part of the compiler to detect it. Specifically, the compiler has to detect that the value doesn’t change between the time you initialize it and the time you pass it to new. With optimization turned on, chances are pretty good at least some compilers still could/would detect the problem, because for optimization purposes they detect data flow so they’d “realize” that in this case the value remains constant, even though you haven’t specified it as const.

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

Sidebar

Related Questions

#include <iostream> using namespace std; int main() { cout << !!!Hello World!!! << endl;
#include <iostream> #include <vector> using namespace std; int main(void) { int i, s, g;
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(void) {
The following code #include <iostream> using namespace std; int main() { const char* const
I have this code main.cpp #include <iostream> #include functs.h using namespace std; int main()
#include <iostream> using namespace std; int main() { double u = 0; double w
#include <iostream> using namespace std; int main (int argc, char* const argv[]) { int
Example: #include <iostream> using namespace std; int main() { wchar_t en[] = LHello; wchar_t
Short problem: #include <iostream> using namespace std; int main() { double **T; long int
#include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000,

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.