473,322 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Segmentation fault - using char pointer to read int data

Hi All
I am trying the below code in Linux platform using GNU compiler - g++ . It got compiled and generated executable. However I got "Segmentation fault" error during execution .

Can you please help me why the char pointer failed to read 1 byte of memory .
The error is caused by the line
"printf("%c",*p);"

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.         int x= 23458;
  6.         char *p=(char*)x;
  7.         printf("%c",*p);
  8. }
May 7 '12 #1
2 1936
weaknessforcats
9,208 Expert Mod 8TB
You can't just access any old memory location. The locatin must be in the address space of your process. Location 23458 is certainly part of your OS.
May 7 '12 #2
donbock
2,426 Expert 2GB
"Segmentation Fault" is a run-time error. The computer hardware detected that your program attempted an unauthorized or illegal memory access. Examples of unauthorized memory accesses are writing to read-only memory or any access to memory outside the assigned memory range of your process. Example of an illegal memory access is any access to a nonexistent memory range (ie, the memory chip isn't installed). Unauthorized accesses are typically detected by the Memory Management Unit (MMU) hardware. Not all processor architectures have an MMU - most notably, many embedded systems lack this feature. Illegal accesses can be detectable by the MMU too, but they more typically detected by the address decoding hardware. Address decoding is present in all computer architectures.

Your program attempts to read the character at address 23458, which is apparently an unauthorized or illegal access. Did you instead intend to read a single byte of the value 23458? If so, then change line 6 to
Expand|Select|Wrap|Line Numbers
  1.         char *p=(char*)&x;
May 8 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Ramprasad A Padmanabhan | last post by:
Hello all, On my linux box ( redhat 7.2 ), I have been using char as a boolean data type. In order to save on the number of bytes as compared to using int or short int. typedef char boolean;...
0
by: Jonathan Villa | last post by:
I'm trying to add some functionality to an already existing application...this applications works fine, and uses the PHP function pg_pconnect to make a persistent connection to the database. This...
1
by: cesco | last post by:
Hi, I'm using the boost library program_options for parsing the command line given to my program as described in the class Parameter: // Main.cpp #include "Parameter.h" using namespace std;...
11
by: jtagpgmr | last post by:
I am currently using the gcc compiler on a cygwin platform, I am a beginner when it comes to programming in C and want to know why anytime I run the .exe with the following code I get a...
0
by: jgarber | last post by:
Hello, I just upgraded MySQLdb to the 1.2.0 version provided by Redhat Enterprise Linux ES4. At that point I began to get segfaults when importing twisted after MySQLdb, but not before. --...
7
by: blacksoil | last post by:
Hi, I have a question regarding reading data from a file and put it to a member of a class. I use fscanf. The class looks like this class myclass { double a;
3
by: fantasticamir | last post by:
Guys, DO you know why I got segmentation fault on this piece of code? char p={68,69,98,112,105}; char *p1; for (int i=0; i<5; i++) p1=p; p1='\0'; cout << p1<<endl;
1
by: cloudie | last post by:
Hie, I've been trying to read an excel file using OLEDB. So far so good, I must say. However, problem arises when there are slashes or spaces in my headers. How should I construct my queries so...
5
by: lancer6238 | last post by:
Dear all, I'm trying to implement the Smith-Waterman algorithm for DNA sequence alignment. I'm aligning a 2040 bp query sequence against a 5040 bp sequence. I'll be trying to implement a parallel...
4
by: spiralfire | last post by:
I wrote a translator, that reads a DIMACS graph format and writes to a simpler format... basically DIMACS format is: c comment p type nodes edges //type is alwats edge on my problems,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.