Jump to content

help with segmentation fault

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
18 replies to this topic

#1
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
the following program when compiled gives a segmentation fault

#include<stdio.h>

main(){

       char *s="i love you",*p=NULL;

       sprintf(p,"%s\n",s);

       printf("%s",p);

}

why am i getting a segmentation fault?
what is a segmentation fault?

#2
mmo-dev

mmo-dev

    Learning Programmer

  • Members
  • PipPipPip
  • 87 posts

csepraveenkumar said:

the following program when compiled gives a segmentation fault
#include<stdio.h>
main(){
       char *s="i love you",*p=NULL;
       sprintf(p,"%s\n",s);
       printf("%s",p);
}
why am i getting a segmentation fault?
what is a segmentation fault?

ummm im not perfect at the lang but arent u forgetting return 0; ?
also if ur just making it say a string why not just use printf to display it? y use the other functions?
just a thought.

#3
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
i wanna see how sprintf() works.
and return 0 doesn't make any difference.
still getting a segmentation fault.

#4
mmo-dev

mmo-dev

    Learning Programmer

  • Members
  • PipPipPip
  • 87 posts

csepraveenkumar said:

i wanna see how sprintf() works.
and return 0 doesn't make any difference.
still getting a segmentation fault.

hmmm

char *s = "i love you",*p = NULL;

try that since the = was together might have been reading it wrong anyways see if that works for ya

#5
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
again no difference.

#6
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts

csepraveenkumar said:

the following program when compiled gives a segmentation fault

#include<stdio.h>

main(){

       char *s="i love you",*p=NULL;

       sprintf(p,"%s\n",s);

       printf("%s",p);

}

why am i getting a segmentation fault?
what is a segmentation fault?

char *p isn't pointing to anything that can hold data.
If you want that sprintf call to work you'll need to either point p at some form of memory that is writeable(dynamically allocating memory as an example) or turn it into a character array.

#7
mmo-dev

mmo-dev

    Learning Programmer

  • Members
  • PipPipPip
  • 87 posts

Sysop_fb said:

char *p isn't pointing to anything that can hold data.
If you want that sprintf call to work you'll need to either point p at some form of memory that is writeable(dynamically allocating memory as an example) or turn it into a character array.

main()
{
char *s="i love you",*p=NULL;
sprintf(p,"%s\n",s);
printf("%s",p);
}

im unsure but does the bracket have 2 be on a new line? im just looking over all the obvious possiblitys at the moment. also wat compiler are u using ive seen some ****ty compilers make app's give seg fault as well might try a diff compiler maybe to see if its the compiler and not ur code.

#8
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
thanks people
the program finally works
the problem was i had not allocated space that p pointed to

#9
mmo-dev

mmo-dev

    Learning Programmer

  • Members
  • PipPipPip
  • 87 posts

csepraveenkumar said:

thanks people
the program finally works
the problem was i had not allocated space that p pointed to

hmmm that would have been my next area to look anyways can i see the completed code?

#10
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts

mmo-dev said:

main()
{
char *s="i love you",*p=NULL;
sprintf(p,"%s\n",s);
printf("%s",p);
}

im unsure but does the bracket have 2 be on a new line? im just looking over all the obvious possiblitys at the moment. also wat compiler are u using ive seen some ****ty compilers make app's give seg fault as well might try a diff compiler maybe to see if its the compiler and not ur code.

no you can put the entire thing on one line and it would still compile, the compiler understands that each instruction ends at the ;, indenting is done so the code is easier to read.
The segfault is from passing a null pointer to sprintf which then gets derefrenced and sprintf attempts to load data into it.

#11
mmo-dev

mmo-dev

    Learning Programmer

  • Members
  • PipPipPip
  • 87 posts

Sysop_fb said:

no you can put the entire thing on one line and it would still compile, the compiler understands that each instruction ends at the ;, indenting is done so the code is easier to read.
The segfault is from passing a null pointer to sprintf which then gets derefrenced and sprintf attempts to load data into it.

ya i figured as much i try 2 look at the easy to debug stuff first b4 i look into the
{
...
}

if that dont work i work my way thu the code till i locate the problem unless the compiler tells me wats wrong xD

#12
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
the completed code is

#include<stdio.h>

#include<stdlib.h>

main(){

char *s = "i love you",*p=malloc(sizeof(s));

//int max=5;

sprintf(p,"%s\n",s);

printf("%s",p);

return 0;

}                                                                                                                                             

@sysop the code was giving segfault even when i had not initialized it to NULL
do char pointers get initialized to NULL automatically?