top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Why am i getting segfault in below code?

+2 votes
422 views

I am writing the code without main function.

#include<stdio.h>
_start()
{
int a=10,b=20,res;
res=a+b;
printf("Res = %d\n",res);
}

Compiling : cc -nostartfiles filename.c

Output:

Res = 30
Segfault
posted Apr 6, 2015 by Chirag Gangdev

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Without _exit(0); the program will compile, but there will be Segmentation fault while running it. So you need to add _exit(0) at the end of _start function :)

1 Answer

+2 votes
 
Best answer

Without _exit(0); the program will compile, but there will be Segmentation fault while running it.

Very Good article related to how main() get called

_start() use a exit(main());
Return value of main is passed to exit() which terminates our program.

Here we do not have main(), we have to explicitly call the _exit(0);

answer Apr 6, 2015 by Arshad Khan
Similar Questions
+2 votes

I have function in python,(Assume that i have imported all necessary module),

 def DL_Iperf(args):
        ssh=paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(server_ip,username="root",password=Password)
some_code

This function is actually a thread and it will be created as many no of UE i have, (Ex: if i have 1 UE than 1 Thread will be created),

So, if i have 1 UE/ 2 UE than its working but if i have 3 UE then it is failing, with error "Paramiko : Error reading SSH protocol banner",

Below is the stderr of the script,

    No handlers could be found for logger "paramiko.transport"

    Unhandled exception in thread started by <function DL_Iperf at 0x02B8ACF0>
    Traceback (most recent call last):

    File "C:\Users\qxdm-5\Desktop\Chirag\LTE_11_Perfect_Working\TCP_Latest_2\Windo
    ws_UE\slave.py", line 379, in DL_Iperf

    ssh.connect(ServerIp,username="root",password=Pwd)

    File "build\bdist.win32\egg\paramiko\client.py", line 295, in connect

    File "build\bdist.win32\egg\paramiko\transport.py", line 451, in start_client

paramiko.SSHException: Error reading SSH protocol banner

From some reference i found that this is because of some network related issue, but my question is if it network related then why everytime in 3rd call of the function i am getting this error? And how do i resolve it?

0 votes
#include<stdio.h>
int main()
{
   int n;
   for(n = 7; n!=0; n--)
     printf("n = %d", n--);
   getchar();
   return 0;
}
+3 votes

1.

main()
{
printf("%x",-1<<4);
}

2.

main()
{
int i=10;
i=!i>14;
Printf ("i=%d",i);
}
0 votes

Following is my code and getting segmentation fault. Please help

#include<stdio.h>
#include<string.h>
main()
{
    char *p,str[100]="Hi, How Are You, Fine, Thank You";
    p=strtok(str,",");
    printf("%s\n",p);
    while(p != NULL)
    {
        p=strtok(NULL,",");
        printf("%s\n",p);
    }
}

O/p is:
Hi
How Are You
Fine
Thank You
and then Segmentation fault.

...