pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ckpradip@gmail.com

    #1

    pointers

    This is my code

    #include <stdio.h>
    #include <stdlib.h>

    int main ( void )
    {
    unsigned char *ptr = ( unsigned char ) 0x00070000;

    printf ( " %c", *ptr ); /* getting segmentation fault here */

    return EXIT_SUCCESS;
    }


    /* I know that I get this because I am trying to access protected
    memory */
    /* But I need to get these contents [ used to find SMBIOS identifier
    '_SM_ ] */

  • Vladimir S. Oka

    #2
    Re: pointers

    ckpradip@gmail. com wrote:
    [color=blue]
    > This is my code
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main ( void )
    > {
    > unsigned char *ptr = ( unsigned char ) 0x00070000;[/color]

    Assuming you know what you're doing here (in terms of 0x00070000 being
    able to be cast to a meaningful pointer on your system), you should
    have said:

    unsigned char *ptr = ( unsigned char * ) 0x00070000;
    ^

    The way you said it, you first cast 0x00070000 to an unsigned character,
    which is then implicitelly converted to a pointer -- most likely not
    what you intended, and obviously pointing to somewhere outside your
    sandbox.

    --
    BR, Vladimir

    Whistler's Law:
    You never know who is right, but you always know who is in charge.

    Comment

    • felix

      #3
      Re: pointers


      I am sorry. You are right.

      My code should be -

      #include <stdio.h>
      #include <stdlib.h>


      int main ( void )
      {
      unsigned char *ptr = ( unsigned char * ) 0x00070000;

      printf ( " %c", *ptr ); /* getting segmentation
      fault here */

      return EXIT_SUCCESS;

      }

      /*
      I am not able to access that memory location
      I use gcc (GCC) 3.2.3 20030502 (Red Hat Linux 3.2.3-20) .
      Any suggestions to overcome that .
      */

      Comment

      • Vladimir S. Oka

        #4
        Re: pointers

        felix wrote:[color=blue]
        > I am sorry. You are right.
        >
        > My code should be -
        >
        > #include <stdio.h>
        > #include <stdlib.h>
        >
        >
        > int main ( void )
        > {
        > unsigned char *ptr = ( unsigned char * ) 0x00070000;
        >
        > printf ( " %c", *ptr ); /* getting segmentation
        > fault here */
        >
        > return EXIT_SUCCESS;
        >
        > }
        >
        > /*
        > I am not able to access that memory location
        > I use gcc (GCC) 3.2.3 20030502 (Red Hat Linux 3.2.3-20) .
        > Any suggestions to overcome that .
        > */[/color]

        <OT>
        And why do you expect to be able to do that? There's no such guarantee
        (there may even be no memory location with that address, and before you
        say you have more memory than that, memory addresses do not necessarily
        begin with zero). Modern operating systems implement memory protection
        mechanisms, and do not allow you to access what you do not own.

        This is all off-topic, as Standard C knows nothing about memory
        addresses. Pointers can be implemented in any convenient way (even
        like: second door on the right). If you want to learn how to access
        physical memory on a given system, ask in the group which discusses
        that particular system (Linux in your case).
        </OT>

        Also, please provide context. Read advice in the following link if you
        want to get meaningful help in c.l.c:



        --
        BR, Vladimir

        He's dead, Jim
        -- McCoy, "The Devil in the Dark", stardate 3196.1

        Comment

        • Mark McIntyre

          #5
          Re: pointers

          On 1 Mar 2006 23:22:00 -0800, in comp.lang.c , "felix"
          <ckpradip@gmail .com> wrote:
          [color=blue]
          > unsigned char *ptr = ( unsigned char * ) 0x00070000;
          >
          > I am not able to access that memory location
          > I use gcc (GCC) 3.2.3 20030502 (Red Hat Linux 3.2.3-20) .
          > Any suggestions to overcome that .[/color]

          Use a different operating system.

          All modern OSes on personal computers provide protection which
          prevents user-mode apps directly accessing the hardware. You should
          either use the system services provided by the OS to access h/w, or
          else write a kernel-mode driver of your own to do it.

          Either is offtopic here.
          Mark McIntyre
          --
          "Debugging is twice as hard as writing the code in the first place.
          Therefore, if you write the code as cleverly as possible, you are,
          by definition, not smart enough to debug it."
          --Brian Kernighan

          ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
          http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
          ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

          Comment

          Working...