c - Printf ubuntu Segmentation fault (core dumped)


Question: 

I have this code:

int a;
printf("&a = %u\n",(unsigned)&a);
printf("a\n");
printf("b\n");
printf("c\n");
printf("d\n");

I tried to print the pointer of a variable.
But it fail on the row printf("a\n"); and says Segmentation fault (core dumped)
Output:

&a = 134525024
Segmentation fault (core dumped)

When I remove the row printf("&a = %u\n",(unsigned)&a); from the code, its success. Output:

a
b
c
d

What worng in my code?




2 Answers: 

This could be an architecture problem. I ran this on OS X and it ran fine:

#import <stdio.h>
int main()
{
    int a;
    printf("&a = %u\n",(unsigned int)&a);
    printf("a\n");
    printf("b\n");
    printf("c\n");
    printf("d\n");
}

However, I did get a warning during compilation:

warning: cast from pointer to integer of different size

I am on a 64-bit machine, so my 64-bit pointers can't fit into a 32-bit int. Try running this instead and see what you get:

#import <stdio.h>
int main()
{
    int a;
    printf("unsigned %ld\n", sizeof(unsigned)); // sizes reported in bytes
    printf("int %ld\n", sizeof(int));           // same as unsigned
    printf("long %ld\n", sizeof(long));         // should fit a pointer
    printf("ptr %ld\n", sizeof(void*));         // size of a pointer
    printf("&a = %lu\n", (unsigned long)&a);    // should print out your pointer
    printf("&a = %p\n", &a);                    // the Right Way of doing things
}

Output:

unsigned 4
int 4
long 8
ptr 8
&a = 140734544742408
&a = 0x7fff508c0808

EDIT:

Just in case you didn't know, unsigned actually means unsigned int. Likewise, long actually means long int.

Also, whether a value is signed or unsigned does not change its physical size, only how it is interpreted. You can see that from the first 2 prints.

 

I solvd my problem. I just reinstall my ubuntu and the vmware, now its work. I am not realy sure what the exact problem, but it solved my problem... :-)

 

More Articles


Why does the following code raise a SegFault. c(Linux)

This a code that would reverse the data of a document and save it in the same document itself.However I am getting a Segmentation Fault.Please Help,I don't know why it gives a SegFault.#include <stdio.h>#include <stdlib.h>#include <termios.h>#include <fcntl.h>#include <str

mysql - Apache2 / phpmyadmin - PHP isn't working

I was working on a project of mine . then i had to restart the computer.Once I did suddenly PHP stopped working on Apache. then i starting looking into the configuration files and error logs and fixed a spelling error in the config file, but STILL the problem persisted, then i decided to re-install

Shell script - convert Excel (xlsx) to CSV - remove blank space / tab space

I receive excel file (xslx) with multiple sheets for my project. The number of records on these sheets ranges from 15k to 70k per sheet. I need to perform following tasks on this data and then convert it to CSV. Or covert to CSV and then process the data either way its fine.Input Example:call_no un


node.js - Querying multiple sensors regularly using NodeJS

I need to fetch the values of about 200 sensors every 15 seconds or so. To fetch the values I simply need to make an HTTP call with basic authentication and parse the response. The catch is that these sensors might be on slow connection so I need to wait at least 5 seconds for one sensor (but usuall

php - How to redirect everything after slash to domain before?

This is what I'm trying to do:I want example.com/car/{whatever} redirected to example.com/{whatever}. For example, example.com/car/honda should be redirected to example.com/hondaBUT if there is nothing after /car/, then just leave it as that. How can i do that in .htaccess ?

ubuntu - Docker: bash terminal starts without prompt

I have a simple container that looks like this:FROM devbox/rails3.2.1RUN apt-get install -y -q libmysql-ruby libmysqlclient-devRUN apt-get install -y -q libqtwebkit-devEXPOSE 3000CMD /bin/bashwhere devbox/rails3.2.1 is a container I made that starts with 'FROM ubuntu' and installs Ruby on Rails. Thi


Segmentation fault: Stack allocation in a C program in Ubuntu when bufffer>4M

Here's a small program to a college's task:#include <unistd.h>#ifndef BUFFERSIZE#define BUFFERSIZE 1#endifmain(){ char buffer[BUFFERSIZE]; int i; int j = BUFFERSIZE; i = read(0, buffer, BUFFERSIZE); while (i>0) { write(1, buffer, i); i = read(0, buffer, BUFFE

php - Having trouble with Ubuntu LAMP stack

I have installed Ubuntu 16.04 LTS, Apache2, Mysql 14, Php7. I have installed Drupal with a few difficulties. Specifically, I had to install php extensions and tweak the configs to enable those extensions, and finally the installation was successful. However, whenever I try to click on any link, the

Segmentation Fault Core dump with C on ubuntu

I have a problem with some line of codes I've written in plain C.It worked fine on windows, but on ubuntu there's an error saying "Segmentation Fault (core dumped)". I've searched for answers but there's too many problem that can caused that error.char line[80];char sett[50][80];int index=0;static c

linux - Docker run command error

I have just installed docker and pulled their official ubuntu image following their documentation installation tutorial.However, when I get to the point of executing a command in a specific container, I end up with the following error:Error: Cannot start container f6e1084236794548bbd5cd059f46f916367