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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:41:23+00:00 2026-05-22T21:41:23+00:00

I have a zap() function written to deallocate a 1-d array as follows. void

  • 0

I have a zap() function written to deallocate a 1-d array as follows.

void zap(double *(&data))
{
    if (data != NULL)
    {
       delete [] data;
       data = NULL;
    }
    return;
}

I was under the impression that if data != NULL would not try to deallocate memory that had never been allocated, but I think I am mistaken. I am having the following implementation problem.

void fun()
{
    int condition = 0;
    double *xvec;
    double *yvec;
    allocate_memory_using_new(yvec); //a function that allocates memory
    if (condition == 1) allocate_memory_using_new(xvec);
    //some code
    //deallocate memory:
    zap (yvec);
    zap (xvec); //doesn't work
    return;
}

The output is the following:

 Unhandled exception at 0x6b9e57aa (msvcr100d.dll) in IRASC.exe: 0xC0000005: Access
 violation reading location 0xccccccc0.

So I realize it is not a desirable thing to try to call zap when it is obvious that the pointer was never actually used. I am just wondering if there is a way to check the address of the pointer at some point in the zap() function to avoid the exception. Thanks in advance for your help and insight!

  • 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-22T21:41:25+00:00Added an answer on May 22, 2026 at 9:41 pm

    Pointers do not get magically initialized to 0, only when they are global or static. You need to do so:

    double *xvec = NULL;
    double *yvec = NULL;
    

    If you do not, they contain random junk that was left on the stack where they are created. And this junk is most of the time not NULL.

    Also, you do not need to check against NULL, as delete is a no-op in that case:

    double* xvec = NULL;
    delete xvec; // perfectly valid
    

    Further, if you’re working with Visual Studio 2010, I recommend to use nullptr instead of NULL.

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

Sidebar

Related Questions

I have function: char *zap(char *ar) { char pie[100] = INSERT INTO test (nazwa,
So I have some XML like this: <bar> <foo>Something</foo> <baz> <foo>Hello</foo> <zap>Another</zap> <baz> <bar>
**Have it working now. I forgot to populate the Array List. How embarrassing. I'm
have an an array String classname[]={'a','b','c','d'}; ArrayAdapter<CharSequence> adapterClasses = new ArrayAdapter<CharSequence>( getApplicationContext(), R.layout.spinner_item_class, R.id.spinnerclasstxt,
Have you written custom balancers for sharding to balance chunks not using the balancer
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a

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.