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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:26:11+00:00 2026-05-17T01:26:11+00:00

The following crashes with a seg-V: // my code int* ipt; int bool set

  • 0

The following crashes with a seg-V:

// my code
int* ipt;
int bool set = false;
void Set(int* i) {
  ASSERT(i);
  ipt = i;
  set = true;
}

int Get() {
  return set ? *ipt : 0;
}

// code that I don't control.
struct S { int I, int J; }
int main() {
  S* ip = NULL;
  // code that, as a bug, forgets to set ip...
  Set(&ip->J);
  // gobs of code
  return Get();
}

This is because while i is not NULL it still isn’t valid. The same problem can happen if the calling code takes the address of an array index operation from a NULL pointer.

One solution to this is to trim the low order bits:

void Set(int* i) {
  ASSERT((reinterpret_cast<size_t>(i))>>10);
  ipt = i;
  set = true;
}

But how many bits should/can I get rid of?


Edit, I’m not worried about undefined behavior as I’ll be aborting (but more cleanly than a seg-v) on that case anyway.

FWIW: this is a semi-hypothetical situation. The bug that caused me to think of this was fixed before I posted, but I’ve run into it before and am thinking of how to work with it in the future.

Things that can be assumed for the sake of argument:

  • If Set is called with something that will seg-v, that’s a bug
  • Set may be called by code that isn’t my job to fix. (E.g. I file a bug)
  • Set may be called by code I’m trying to fix. (E.g. I’m adding sanity checks as part of my debuggin work.)
  • Get my be called in a way that provide no information about where Set was called. (I.e. allowing Get to seg-v isn’t an effective way to debug anything.)
  • The code needn’t be portable or catch 100% of bad pointers. It need only work on my current system often enough to let me find where things are going wrong.
  • 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-17T01:26:11+00:00Added an answer on May 17, 2026 at 1:26 am

    There is no portable way to test for any invalid pointer except NULL. Evaluating &ip[3] gives undefined behaviour, before you do anything with it; the only solution is to test for NULL before doing any arithmetic on the pointer.

    If you don’t need portability, and don’t need to guarantee that you catch all errors, then on most mainstream platforms you could check whether the address is within the first page of memory; it’s common to define NULL to be address zero, and to reserve the first page to trap most null pointer dereferences. On a POSIX platform, this would look something like

    static size_t page_size = sysconf(_SC_PAGESIZE);
    assert(reinterpret_cast<intptr_t>(i) >= page_size);
    

    But this isn’t a complete solution. The only real solution is to fix whatever is abusing null pointers in the first place.

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

Sidebar

Related Questions

I wanted to know why the following code crashes. int main( ) { int
The following code crashes on the second Pop() call. I'm a novice with C
I'm using libxml2 and C++. The following function crashes here at return (char*)cur->content; .
This question comes from my experience with the following question: https://stackoverflow.com/questions/492748/new-responses-icon-on-so-crashes-ie7-closed In that question,
I have some trouble figuring out why the following crashes (MSVC9): //// the following
I get the following error Cross-thread operation not valid: Control accessed from a thread
The following view crashes the iPhone If I comment out the using (webView =
Recently I got IE7 crashed on Vista on jar loading (presumably) with the following
Following on from my recent question on Large, Complex Objects as a Web Service
Following my question regarding a .NET YAML Library ... as there doesn't seem to

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.