Sponsored Content
Top Forums Programming C. To segmentation fault or not to segmentation fault, that is the question. Post 303032348 by wisecracker on Friday 15th of March 2019 12:14:06 PM
Old 03-15-2019
C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1...

I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one.

Below are two very condensed snippets of which I have added the results inside the each code section.

IMPORTANT! This will NOT even compile on gcc 2.95.3 for the AMIGA, but WORKS correctly on OSX 10.14.1, gcc 4.2.1.
Code:
/* No_error gcc 4.2.1 demo. */

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

int NUM_1;
int NUM_2;
char CHARACTER;

int main(int argc, char *argv[])
{
    /* Conditional BEFORE _variables_! */
    if (argc <= 3)
    {
        printf("ERROR!\n\n");
        printf("Not enough arguments!\n");
        exit(1);
    }

    int NUM_1 = strtod(argv[1], NULL);
    int NUM_2 = strtod(argv[3], NULL);
    CHARACTER = *argv[2];

    printf("\n%i, %i, %c\n\n", NUM_1, NUM_2, CHARACTER);

    return(0);
}


/* ****************************************
     Results OSX 10.14.1, gcc 4.2.1...

Last login: Fri Mar 15 14:45:03 on ttys000
AMIGA:amiga~> cd Desktop/Code/C
AMIGA:amiga~/Desktop/Code/C> gcc noerror.c
AMIGA:amiga~/Desktop/Code/C> ./a.out
ERROR!

Not enough arguments!
AMIGA:amiga~/Desktop/Code/C> ./a.out 1 v
ERROR!

Not enough arguments!
AMIGA:amiga~/Desktop/Code/C> ./a.out 1 v 3

1, 3, v

AMIGA:amiga~/Desktop/Code/C> _

**************************************** */

ALSO IMPORTANT! This compiles and WORKS perfectly on the AMIGA but gives a segmentation fault on OSX 10.14.1, gcc 4.2.1.
Code:
/* Error gcc 4.2.1 demo. */

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

int NUM_1;
int NUM_2;
char CHARACTER;

int main(int argc, char *argv[])
{
    int NUM_1 = strtod(argv[1], NULL);
    int NUM_2 = strtod(argv[3], NULL);
    CHARACTER = *argv[2];

    /* Conditional AFTER _variables_! */
    if (argc <= 3)
    {
        printf("ERROR!\n\n");
        printf("Not enough arguments!\n");
        exit(1);
    }

    printf("\n%i, %i, %c\n\n", NUM_1, NUM_2, CHARACTER);

    return(0);
}

/* ****************************************

Last login: Fri Mar 15 15:48:37 on ttys000
AMIGA:amiga~> cd Desktop/Code/C
AMIGA:amiga~/Desktop/Code/C> gcc error.c
AMIGA:amiga~/Desktop/Code/C> ./a.out
Segmentation fault: 11
AMIGA:amiga~/Desktop/Code/C> ./a.out 1 r
Segmentation fault: 11
AMIGA:amiga~/Desktop/Code/C> ./a.out 1 r 6

1, 6, r

AMIGA:amiga~/Desktop/Code/C> _

**************************************** */

As you can see the second code gives a Segmentation fault: 11 on OSX 10.14.1, gcc 4.2.1 but compiles and works correctly on the AMIGA.

Can anyone explain why there is a SEGMENTATION FAULT when the if conditional statement is inside the 'main()' function to start with and the assignments set?
 

10 More Discussions You Might Find Interesting

1. Programming

segmentation fault

sometimes for this code i get a segmentation fault for codes llike this : int main{ int * a= 0; int b; a = (int*)malloc(sizeof(int)); ///some code using these variable but no freeing of a if(a){ free(a); a = 0; } return... (3 Replies)
Discussion started by: wojtyla
3 Replies

2. AIX

Segmentation fault

Hi , During execution a backup binary i get following error "Program error 11 (Segmentation fault), saving core file in '/usr/datatools" Riyaz (2 Replies)
Discussion started by: rshaikh
2 Replies

3. Linux

Segmentation fault

Hi, on a linux Red HAT(with Oracle DB 9.2.0.7) I have following error : RMAN> delete obsolete; RMAN retention policy will be applied to the command RMAN retention policy is set to redundancy 2 using channel ORA_DISK_1 Segmentation fault What does it mean ? And the solution ? Many thanks. (0 Replies)
Discussion started by: big123456
0 Replies

4. Programming

segmentation fault

If I do this. Assume struct life { char *nolife; } struct life **life; // malloc initialization & everything if(life->nolife == 0) Would I get error at life->nolife if it is equal to 0. wrong accession? (3 Replies)
Discussion started by: joey
3 Replies

5. Programming

Segmentation fault.

I'm getting a segmentation fault. I'm new to Linux programming. Thanks so much for all of your input.:eek: #include </usr/include/mysql++/mysql++.h> #include <stdio.h> #include <iostream> #include <sstream> #include <string.h> using namespace std; int outputToImport(const char*... (1 Reply)
Discussion started by: sepoto
1 Replies

6. Programming

Segmentation fault in C

i have this code int already_there(char *client_names, char *username) { int i; for(i = 0; i<NUM; i++) { printf("HERE\n"); if (strcmp(client_names, username)==0) return(1); } return(0); } and i get a segmentation fault, whats wrong here? (7 Replies)
Discussion started by: omega666
7 Replies

7. UNIX for Advanced & Expert Users

segmentation fault with ps

What does this mean and why is this happening? $ ps -ef | grep ocular Segmentation fault (core dumped) $ ps -ef | grep ocular Segmentation fault (core dumped) $ ps aux | grep ocular Segmentation fault (core dumped) $ ps Segmentation fault (core dumped) $ pkill okular $ ps... (1 Reply)
Discussion started by: cokedude
1 Replies

8. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

9. Homework & Coursework Questions

Segmentation Fault

this is a network programming code to run a rock paper scissors in a client and server. I completed it and it was working without any error. After I added the findWinner function to the server code it starts giving me segmentation fault. -the segmentation fault is fixed Current problem -Also... (3 Replies)
Discussion started by: femchi
3 Replies

10. Solaris

Segmentation fault

Hi Guys, I just installed and booted a zone called testzone. When I logged in remotely and tried changing to root user I get this error: "Segmentation fault" Can someone please help me resolve this? Thanks alot (2 Replies)
Discussion started by: cjashu
2 Replies
Desktop::Notify(3pm)					User Contributed Perl Documentation				      Desktop::Notify(3pm)

NAME
Desktop::Notify - Communicate with the Desktop Notifications framework VERSION
Version 0.03 SYNOPSIS
use Desktop::Notify; # Open a connection to the notification daemon my $notify = Desktop::Notify->new(); # Create a notification to display my $notification = $notify->create(summary => 'Desktop::Notify', body => 'Hello, world!', timeout => 5000); # Display the notification $notification->show(); # Close the notification later $notification->close(); DESCRIPTION
This module provides a Perl interface to the Desktop Notifications framework. The framework allows applications to display pop-up notifications on an X desktop. This is implemented with two components: a daemon that displays the notifications, and a client library used by applications to send notifications to the daemon. These components communicate through the DBus message bus protocol. More information is available from <http://trac.galago-project.org/wiki/DesktopNotifications> This module serves the same purpose as "libnotify", in an object-oriented Perl interface. It is not, however, an interface to "libnotify" itself, but a separate implementation of the specification using Net::DBus. METHODS
new %opts Connect to the notification daemon. %opts can include the following options: app_name The application name to use for notifications. Default is "basename($0)" bus The Net::DBus mesage bus to use. Default is to call Net::DBus->session, which is usually where notification-daemon can be reached. service The DBus service name of the daemon. Default is org.freedesktop.Notifications. objpath The path to the notifications DBus object. Default is /org/freedesktop/Notifications. objiface The DBus interface to access the notifications object as. Default is org.freedesktop.Notifications. create %params Creates a new notification object that can be displayed later. This will return a Desktop::Notify::Notification object; see that module for information about using it. close_callback $coderef Sets a user-specified function to be called whenever a notification is closed. It will be called with one argument, which is the Notification object that was just closed. AUTHOR
Stephen Cavilia, "<sac at atomicradi.us>" SEE ALSO
Net::DBus <http://www.galago-project.org/specs/notification/index.php> <http://www.galago-project.org/downloads.php> BUGS
Please report any bugs or feature requests to "bug-desktop-notify at rt.cpan.org", or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Desktop-Notify>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. SUPPORT
You can find documentation for this module with the perldoc command. perldoc Desktop::Notify You can also look for information at: o AnnoCPAN: Annotated CPAN documentation <http://annocpan.org/dist/Desktop-Notify> o CPAN Ratings <http://cpanratings.perl.org/d/Desktop-Notify> o RT: CPAN's request tracker <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Desktop-Notify> o Search CPAN <http://search.cpan.org/dist/Desktop-Notify> ACKNOWLEDGEMENTS
COPYRIGHT &; LICENSE Copyright 2007 Stephen Cavilia, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.10.1 2009-12-24 Desktop::Notify(3pm)
All times are GMT -4. The time now is 11:16 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy