c - Whats the difference between the two command lines? (SegFault)


Question: 

I have written a code that maps to a shared memory location,so that the first program opens a shared memory block and stores some data in it.And the second program reads the shared data.

Whats the difference between the two command lines:

1.

 if(argc<2)
 {
     printf("USAGE:%s text-to-share\n",argv[0]);
 }

This code gives me a Segmentation Fault if I run it without the second argument. However it works fine when I enter in some data.

2.

 if(argc<2)
    return printf("USAGE:%s text-to-share\n",argv[0]);

This one serves my purpose.

But I fail to understand the difference between the two. I'm a novice.For me the two are same,because ideally they should have the same output. Please Help!




2 Answers: 

why first statment giving you segmentation fault,in C it name of the program which you are executing,so it should be absolutely fine.I am able to execute this testcase properly

int main(int argc,char ** argv){
    if(argc<2)
     {
         printf("USAGE:%s text-to-share\n",argv[0]);
     }
    return 0;
 }

it output :

USAGE:./prog text-to-share

except you are doing something wrong in the code executed before this.

 

The two are obviously not the same:

printf("USAGE:%s text-to-share\n",argv[0]);        // From example 1
return printf("USAGE:%s text-to-share\n",argv[0]); // From example 2

The second line has something the first line does not: a return statement.

 

More Articles


shell - UNIX: copy entire directory structure and only specific file type

How i can recreate entire directory structure and copy only files with .txt extension if present. I would like to copy the directory or sub-directory even if its empty.

How do I insert PHP variables in a MySQL table?

This question already has an answer here: Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors? 3 answers

node.js - Ubuntu + Nodejs + curl: (52) Empty reply from server

I have a nodejs process listening a port on my server:app.get('/statistic', function(req, res) { log_get_connection(req); getStatistic().then(statistic => { res.json(statistic); });});When I try to access it via curl being on the same machine:curl "http://127.0


Unable to Connect to mysql db through php in GCE instance with ubuntu

I'm a little bit frustrated with a problem I'm having in a system migration from AWS to GCE.I've an ec-2 instance running on amazon web services, perfectly working and configured.The problem is that google is offering a 100.000$ credit in a special plan for Startups, my company's CEO asks for a migr

node.js - webpack-dev-server does not create initial bundle on start

I have built a basic babel webpack starter project that i clone from git. The git repository has no dest/output/build folder nor does it have any initial build files. I am knew to webpack so i assumed that when the webpack-dev-server started up it would create a folder (in my case 'build') and do a

linux - List out text files from given directory in unix

Writing shell script to sort text file data from given directory with for loop in ubuntu as: echo "Enter directory Name" read dr path= ` find /home/user -name $dr ` for x in ` ls *.txt $path ` do sort $x done echo "------Script finished-------"But this script is giving me er


linux - Segmentation fault due to lack of memory in C

This code gives me segmentation fault about 1/2 of the time:int main(int argc, char **argv) { float test[2619560]; int i; for(i = 0; i < 2619560; i++) test[i] = 1.0f;}I actually need to allocate a much larger array, is there some way of allowing the operating system to allow me ge

Unable to install LAMP (Linux, Apache, Mysql, PHP stack) on Ubuntu 16.04

I am trying to install LAMP server on my Ubuntu desktop. The problem I am facing is related to IP address of my laptop.While Setting Global ServerName to Suppress Syntax Warnings, as the documentation says here, when I am adding a ServerName directive, pointing to my server's public IP address in co

linux - filter out content between two files

I have two files conditions.txtabcdefghlogs.txtefghijklmnopqrstI am expecting output to be:ijklmnopqrstActual output:efghijklijklmnopmnopqrstqrstHere's the code I had worked till nowfunc(){while read conditiondoif [[ $line = $condition ]] ; then:elseecho "$line";done < condition.txt}while read li

c - Why do I get a seg fault on Ubuntu but not mac?

I have a program that checks the modification time of a file and executes the file if it has changed. Currently it works if I run it on my mac, but it seg faults if I run it on ubuntu. Please help me.note: this is in c#include <unistd.h>#include <stdlib.h>#include <stdio.h>#inclu