473,327 Members | 1,952 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,327 software developers and data experts.

Why do I get a segmentation fault when I dereference a typecast?

Please review my pastebin. I commented exactly where it occurs, and how GDB will not access my variable.

I made a program to prove that you can store an address into an unsigned int and use it as a pointer. It works on old 32bit machine, but not on my 64 bit. I can only assume it is because of my compiler??

Expand|Select|Wrap|Line Numbers
  1. //I am trying to figure out why I get a segmentation fault at "*((char *) hacky_nonpointer)"
  2. //
  3. //This works on a 32 bit older gcc compiler on an older machine, but with gcc-4.4.3 on my
  4. //x64 machine, I get a segmentation fault at that point. Is it my compiler??
  5. //Also, GDB shoots an "Error accessing memory" when I do x/xw hacky_nonpointer. It does
  6. //retain it's value though.
  7.  
  8. #include <stdio.h>
  9.  
  10. int main() {
  11.    int i;
  12.  
  13.    char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
  14.    int int_array[5] = {1, 2, 3, 4, 5};
  15.  
  16.    unsigned int hacky_nonpointer;
  17.  
  18.    hacky_nonpointer = (unsigned int) char_array;
  19.  
  20.    for(i=0; i < 5; i++) { // Iterate through the int array with the int_pointer.
  21.       printf("[hacky_nonpointer] points to %p, which contains the char '%c'\n",
  22.             hacky_nonpointer, *((char *) hacky_nonpointer));
  23.       hacky_nonpointer = hacky_nonpointer + sizeof(char);
  24.    }
  25. }
  26.  
http://pastebin.com/aAx7yMD7
Dec 16 '11 #1

✓ answered by Banfa

No it is not your compiler it is your code.

A 64 bit machine (and 64 bit compiler) has a 64bit address space and 64 bit pointers but you are assigning the pointer to a unsigned int which is 32 bits and then casting that back to a pointer, 64 bits again. You are loosing the most significant 32 bits of the pointer.

If you are going to assign a pointer value to an integer (and it isn't very good practice) then you should use an integer of the right size.

C99 contains a header, stdint.h which defines uintptr_t which is defined as being an integer of the right size to contain a pointer on the platform.

2 2356
Banfa
9,065 Expert Mod 8TB
No it is not your compiler it is your code.

A 64 bit machine (and 64 bit compiler) has a 64bit address space and 64 bit pointers but you are assigning the pointer to a unsigned int which is 32 bits and then casting that back to a pointer, 64 bits again. You are loosing the most significant 32 bits of the pointer.

If you are going to assign a pointer value to an integer (and it isn't very good practice) then you should use an integer of the right size.

C99 contains a header, stdint.h which defines uintptr_t which is defined as being an integer of the right size to contain a pointer on the platform.
Dec 16 '11 #2
Awesome.
Assigning a pointer value to an int most certainly isn't good practice. The program was designed to show the fundamentals of pointers and what really is going on under the cover, even though I am just refreshing my own memory.

I appreciate the insight and it definitely works. Kudos to you, Banfa, for now I know where my studies are going to take me next. Thanks a lot!
Dec 17 '11 #3

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

Similar topics

14
by: deancoo | last post by:
Hello all, I have a class member, which calls another member from the same class. The problem I'm experiencing is that the nested function executes fine, but causes a segmentation fault as it...
5
by: sandwich_eater | last post by:
I get a segmentation fault in my program when resizing a vector. This seems strange because it is the second resize that causes the error. std::vector<double> a; std::vector<double> b; ...
10
by: Cocy | last post by:
Hi, This might be a sort of FAQ, but I don't see why, so I would someone help me to understand what's wrong? I've just created following code which wold trim white space(s) in a (given) string....
2
by: francescomoi | last post by:
Hi. I've got a C program which calls a function: ---------------- printf("TEST1\n"); int value = my_function(param); printf("TEST2\n"); int my_function(int my_param) {
12
by: nae zot bba la | last post by:
Hi, very short code here. Basically I want to open a file called instruction in a++ mode and then write data of %s type to it, then read from the same file and print to screen. The code compiles...
5
by: silverburgh.meryl | last post by:
Hi, I have a segmentation fault in line 66 of GroupResult.h and I can't figure out why that causes any problem, I appreciate if anyone can help. line 66 of Result.h: 66 size_t size()...
6
by: Wes | last post by:
I'm running FreeBSD 6.1 RELEASE #2. The program is writting in C++. The idea of the program is to open one file as input, read bytes from it, do some bitwise operations on the bytes, and then...
1
by: TiggiePurr | last post by:
Helpppp..... I have tons of code and can't seem to figure out why I'm getting a segmentation fault on the last line... int twoc; int i; twoc = count1s *2; // definitely init vector...
3
by: krista | last post by:
Hi, I am beginner of C++. When I try to read a file and put it into X and Y variables. I got that segmentation fault. the data file is data.txt: 4.4 6.8 3.2 -5.5 3.3 0.9
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...
0
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.