Segmentation fault during execution?

Change this line char *arg10,*c to char arg1000,c This line scanf("%s",arg1) to scanf("%s",arg) And this line while((*c=getchar())! =EOF) to while((c=getchar())! =EOF) Explanation: char *c is not a character.It's a pointer to a character.

It starts out just pointing to a random bit of memory, which will often be filled with random data - whatever was most recently written there char c is a character The same thing applies to char *arg10 It's an array of ten pointers. They point into random memory, filled with random data Note: my change is not best practice. If someone were to type in a filename 1000 characters or more long, you'd write over the end of the arg buffer.

Depending on what you're doing, this can be a security bug.

Change this line char *arg10,*c; to char arg1000,c; This line scanf("%s",arg1); to scanf("%s",arg); And this line while((*c=getchar())! =EOF) to while((c=getchar())! =EOF) Explanation: char *c; is not a character.It's a pointer to a character.

It starts out just pointing to a random bit of memory, which will often be filled with random data - whatever was most recently written there. Char c; is a character. The same thing applies to char *arg10.

It's an array of ten pointers. They point into random memory, filled with random data. Note: my change is not best practice.

If someone were to type in a filename 1000 characters or more long, you'd write over the end of the arg buffer. Depending on what you're doing, this can be a security bug.

I have used char * so that 1 arg of the fwrite will have a pointer and I wnt to seek the file. I have made the necessary changes like *argto arg1000,char *c to char c but I am gettin till a segmentation fault . My intention is to reverse the contents of a file and paste it on another file .

Above program is a bit of it. – ar3 Apr 17 at 5:20.

In char *arg10; you define an array of 10 pointers to char but you do not initialize its elements. Arg0, arg1, ..., arg9 will all have undefined values. Then, you try to enter a string into one of those undefined values.

Lucky you, you got a segmentation fault. Had you been unlucky, your program could format your hard disk instead.

I don't see any wrong here. Why downvote? Up voting to balance :) – Mahesh Apr 16 at 14:32 1 I didn't downvote, and I wouldn't downvote for this, but "your program could format your hard disk instead" is over-egging things a bit... – Jon Bright Apr 16 at 14:33 Clever link notwithstanding, the compiler can generate all the code it likes, there's no modern OS that is going to let it format the HD.

I stick with over-egging. – Jon Bright Apr 16 at 14:38 @Jon: I'll try and remember to use my other UB example in the future ... make the computer ooze lemon juice from the USB port – pmg Apr 16 at 14:42 1 @pmg I actually prefer that one. Conveys the point more succinctly :-) – Jon Bright Apr 16 at 14:46.

Char *arg10 ; arg is array of char pointers. You need to assign them memory locations using malloc before taking input - scanf("%s",arg1); // arg1 is not assigned to point to any memory location // and is what causing the segmentation fault. So do - arg1 = malloc( stringLengthExpectedToEnter + 1 ) ; // +1 for termination character Should do like that with the rest of array elements too (or) simply change char*arg10 to char arg10 and make sure to enter only enter 9 characters.

I think you are confusing between a pointer and a normal variable. Int *ptr; ptr is variable that can hold the address of an integer variable. Memory is allocated to for ptr variable to hold an integer address.

That's it. Ptr is in an uninitalized state and is pointing no where (or) might be pointing to garbage. Dereferencing an uninitialized pointer's behavior is undefined and you are lucky enough if it gives a segmentation-fault.

Now, you need to assign it a valid memory location using malloc. Ptr = malloc( sizeof(int) ) ; // Allocates number of bytes required to hold an // integer and returns it's address. So, ptr is now pointing to memory location acquired from free store that can hold an integer.

Such acquired locations from free stored must be freed using free, else you have classical problem of memory leak. It is good practice to initialize pointer to NULL while declaration. Int *ptr = NULL ; Hope it helps!

Scanf("%d", ptr) ; // Notice that & is not required before ptr. Because ptr // content is address itself. A normal variable story is entirely different.

When declared - int var ; Memory is allocated to var to hold an integer. So, you can directly assign it an integer.

Good explanation but I have a doubt here. Is the memory of the pointer static or it stores permanently after the exiting the program( if I have not allocated memory to it dynamically) and where is the memory stored like in command line args ex- int main(argc ,*argv)...... – ar3 Apr 17 at 5:25.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions