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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:39:42+00:00 2026-05-24T03:39:42+00:00

The code: int *ptr = new int[10]; int *q = ptr; delete q; works

  • 0

The code:

int *ptr = new int[10];
int *q = ptr;
delete q;

works fine without any issues (no run-time error).

However, the following code:

int *ptr = new int[10];
int *q = ptr;
q++;
delete q;

results in run-time error.

I am using Microsoft Visual Studio-8 and Win-7 as platform.

I am not able to figure out why there is a run-time error in the second case?

  • 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-24T03:39:43+00:00Added an answer on May 24, 2026 at 3:39 am

    Your code is causing an Undefined Behavior. An Undefined Behavior means anything can happen, the behavior cannot be defined. The program works just by pure luck its behavior cannot be explained.

    Basically,

    If you are allocating dynamic memory with new you MUST use delete to deallocate it.

    If you are allocating dynamic memory with new[] you MUST use delete[] to deallocate it.

    It is undefined behavior to pass any address to delete which was not returned by new.
    Here is the quote from the Standard.

    As per C++03 Standard § 3.7.4.2-3:

    If a deallocation function terminates by throwing an exception, the behavior is undefined. The value of the first argument supplied to a deallocation function may be a null pointer value; if so, and if the deallocation function is one supplied in the standard library, the call has no effect. Otherwise, the value supplied
    to operator delete(void*) in the standard library shall be one of the values returned by a previous invocation of either operator new(std::size_t) or operator new(std::size_t, const std::nothrow_-t&) in the standard library, and the value supplied to operator delete[](void*) in the standard library shall be one of the values returned by a previous invocation of either operator new[](std::size_t) or
    operator new[](std::size_t, const std::nothrow_t&) in the standard library.

    In C++ it is better to use RAII(SBRM) by using Smart pointers instead of raw pointers, which automatically take care of the memory deallocations.

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

Sidebar

Related Questions

when i run this code : CONADefinitions.CONAPI_FOLDER_INFO2 FolderInfo; int iResult = 0; IntPtr Buffer
The following snippet of C# code: int i = 1; string result = String.Format({0},{1},{2},
I'm trying to mimic the following Java code: int[][] multi; // DIMENSIONS ARE UNKNOWN
I have the following fields: Inventory control (16 byte record) Product ID code (int
Take the following two lines of code: for (int i = 0; i <
When compiling the following code: void DoSomething(int Numbers[]) { int SomeArray[] = Numbers; }
Let's take the code int a, b, c; ... if ((a + b) >
If I have the code: int f(int a) { return a; } double f(double
This is the code: unsigned int number; FILE* urandom = fopen(/dev/urandom, r); if (urandom)
Is there a side effect in doing this: C code: struct foo { 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.