pointers

C

ckpradip

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_ ] */
 
V

Vladimir S. Oka

This is my code

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

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

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.
 
F

felix

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 .
*/
 
V

Vladimir S. Oka

felix said:
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 .
*/

<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:

http://cfaj.freeshell.org/google/
 
M

Mark McIntyre

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 .

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,947
Members
47,498
Latest member
yelene6679

Latest Threads

Top