LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-15-2002, 10:54 AM   #1
wwnn1
Member
 
Registered: Apr 2002
Location: PRC
Posts: 32

Rep: Reputation: 15
Unhappy segmentation fault !


I have a programme below:

/************* code ****************/
#include<stdio.h>

main()
{
char * ptr;

ptr = (char *)0xb8000000L;
printf("%c\n",ptr[0]);
getch();
}
/**********************************/

this programme can be complied without any error/warning.

but when I run it ( ./a.out ), it display this error message: segmentation fault.

Why?
 
Old 06-15-2002, 01:09 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
ptr=(char *)0xb8000000L;
It inserts a value to ptr. Then you're using ptr to write the contents of memory in ptr. But you don't know if this address can be used by your program. It can't. You can only use memory the system gives to your program. You should modify your program to allocate memory using malloc, then copy the string to the array.
BTW What would you like to do in this program?
 
Old 06-15-2002, 01:24 PM   #3
wwnn1
Member
 
Registered: Apr 2002
Location: PRC
Posts: 32

Original Poster
Rep: Reputation: 15
Thanks!

Is there some methods to let a process visit the memory that does not allocated to it?

Last edited by wwnn1; 06-15-2002 at 01:26 PM.
 
Old 06-15-2002, 02:06 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
AFAIK only when you're root.
 
Old 06-17-2002, 11:40 AM   #5
herambshembekar
Member
 
Registered: Feb 2002
Location: India
Distribution: redhat 7.1
Posts: 70

Rep: Reputation: 15
I first thaught as 0xb8000000L address is of video memory i will not be able to acess within process (user mode).

thinking I will be able to acess it within kernel module, i have written kernel module,

but i was wrong i am geting segmentation fault within kernel module too.
 
Old 06-18-2002, 04:03 AM   #6
ferrous
LQ Newbie
 
Registered: Dec 2001
Location: Atlanta
Distribution: Slackware
Posts: 16

Rep: Reputation: 0
So you wanna be a gangsta and all that shit? Smoke any motherfucker, don't even quit?

There's a concept in operating system design called memory protection. I suggest you read up on the concept before you go mucking about, dropping pointers in random places looking to snoop data. Apart from that, check the man page on mmap.
 
Old 06-18-2002, 04:37 AM   #7
tundra
Member
 
Registered: Jun 2002
Location: Koom Valley
Distribution: rh8
Posts: 528

Rep: Reputation: 31
Quote:
So you wanna be a gangsta and all that shit? Smoke any motherfscker, don't even quit?
huh? why the comment?
 
Old 06-18-2002, 09:48 AM   #8
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
No pointer in C or C++ code will point to an actual physical address whatever mode you're running your program in. Remember that the Linux kernel uses paging to create a virtual address space for each process. (Of course this happens in pretty much any multitasking operating system, although paging isn't necessarily the method used).

Alex
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what does Segmentation Fault mean ? baronlynx Linux - Newbie 10 10-25-2009 04:32 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Help !!! Segmentation fault mola Linux - Software 3 06-23-2005 11:13 AM
Segmentation fault tejas15_10 Programming 9 06-20-2005 09:12 AM
Segmentation fault santhosh_o Programming 3 10-26-2004 05:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration