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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:59:19+00:00 2026-06-06T23:59:19+00:00

I am reading article from Stanford CS library http://cslibrary.stanford.edu/102/ Bad Pointer Example Code with

  • 0

I am reading article from Stanford CS library http://cslibrary.stanford.edu/102/

Bad Pointer Example
Code with the most common sort of pointer bug will look like the above correct code, but without the middle step where the pointers are assigned pointees. The bad code will compile fine, but at run-time, each dereference with a bad pointer will corrupt memory in some way. The program will crash sooner or later. It is up to the programmer to ensure that each pointer is assigned a pointee before it is used. The following example shows a simple example of the bad code and a drawing of how memory is likely to react…

void BadPointer() {
   int* p;     // allocate the pointer, but not the pointee
   *p = 42;    // this dereference is a serious runtime error
}

// What happens at runtime when the bad pointer is dereferenced…

But I remember that char* should be defined like this

char *const name_ptr = "Test";

In this way, if everyone think about if this char* is a bad define?

  • 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-06T23:59:20+00:00Added an answer on June 6, 2026 at 11:59 pm

    The line

    char *const name_ptr = "Test";
    

    is fine; you’re initializing the pointer with the address of the string literal "Test", which is an array of char stored in such a way that the memory for it is allocated at program startup and held until the program terminates.

    A quick digression on the const qualifier:

    In C, declaration of the form

    const T foo = expr;
    

    or

    T const foo = expr;
    

    means that foo may not be written to; it’s assigned the value of expr when it’s created, and that value may not be changed for the rest of foo‘s lifetime1). With pointer variables, it gets a little more complicated:

    const T *p = expr;
    T const *p = expr;
    

    both declare p as a non-const pointer to const data; IOW, you can change the value of p (p can point to different objects), but not the value of *p (you cannot change the value of what p points to).

    T * const p = expr;
    

    declares p as a const pointer to non-const data; you can change the value of what p points to (*p = ...), but you cannot change p to point to a different object.

    const T * const p = expr;
    T const * const p = expr;
    

    both declare p as a const pointer to const data; you cannot change either the value of p or what p points to.

    In C, string literals such as "Test" are stored as arrays of char, but attempting to modify the contents of a string literal is undefined behavior (depending on the platform, you may get an access violation). For safety’s sake, it’s usually a good idea to declare pointers to string literals as const char * or char const *, rather than char * const as in the example above.

    As far as

    void BadPointer() {
       int* p;     // allocate the pointer, but not the pointee
       *p = 42;    // this dereference is a serious runtime error
    }
    

    is concerned, p is an auto variable, which is not initialized to any particular value; it will contain a random bit string that may or may not correspond to a writable address. Because of this, the behavior of the statement *p = 42; is undefined – you may get an access violation, you may wind up overwriting something important and leave the program in a bad state, or it may appear to “work” with no issues (writing to some random memory area that is accessible and not important).

    In general, it’s impossible to tell whether a given pointer value is valid or invalid from the pointer value alone2). The one exception is the special pointer value NULL, which is a well-defined “nowhere” that’s guaranteed to compare unequal to any valid pointer value. Pointer variables declared at file scope (outside of any function) or with the static qualifier are implicitly initialized to NULL. Non-static, block-scope pointer variables should always be explicitly initialized with either NULL or a valid address. This way you can easily check to see if the pointer has been assigned a valid value:

    int *p = NULL;
    ...
    if (p != NULL) // or simply if (p)
    {
      *p = 42;
    }
    else
    {
      // p was not assigned a valid memory location
    }
    

    1) Note that, in C, foo is not a compile-time constant; it’s a regular run-time variable, you just cannot write to it. You cannot use it in a context that requires a compile-time constant.

    2) If you’re intimately familiar with your platform’s memory model you can make some educated guesses, but even then it’s not guaranteed.

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

Sidebar

Related Questions

I am reading the build process article from MS patterns and pratices team http://msdn.microsoft.com/en-us/library/ee817676.aspx
I was reading about data driven testing using mbunit from this article. http://blog.benhall.me.uk/2007/04/mbunit-datafixture-data-driven-unit.html I
I'm reading an article from this website , but when i run the code
After reading this article http://camendesign.com/code/developpeurs_sans_frontieres I have decided to follow what it says and
Reading this article Taking Advantage of High-Definition Mouse Movement - http://msdn.microsoft.com/en-us/library/windows/desktop/ee418864(v=vs.100).aspx , I surmise
After reading this article, it makes sense to rebase to gather changes from the
Firstly, sorry for my English (I'm from China). By reading this article( how to
Reading this article http://support.microsoft.com/kb/813878 I have a question: Where can I get ipseccmd.exe for
After reading this article I don't have a clear answer: http://palizine.plynt.com/issues/2010Oct/bypass-xss-filters/ Will browsers interpret
After reading this article http://lukast.mediablog.sk/log/?p=155 I decided to use mingw on linux to compile

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.