Hi, The following code reads 20 characters from one file and writes them (appends them) to the other file. The code works in Turbo C++ on windows but it shows segmentation fault on Linux. I am using Ubuntu 10.10 and gcc compiler.
Please tell me where I was wrong.
This doesn't create memory for you. All a pointer is is an integer describing where memory is. And just like an uninitialized integer, an uninitialized pointer can be anything at all. So you could be trying to use any memory at all, which, in DOS, could mean accidentally overwriting bits of your operating system and hanging the computer or worse... In Linux though, your process resides in its own protected memory space, and if you try to use memory you didn't ask for, it can tell the difference and kills it.
IOW, this code was always wrong. It didn't crash in DOS mode only because DOS mode is physically incapable of detecting that problem. If you compiled this as a 32-bit Windows program it'd crash too.
If you want something that actually has memory, you can declare it as a buffer on the stack:
or you can give the pointer something to point to like
malloc() needs malloc.h in Turbo C++, or stdlib.h for compilers that aren't 30 years out of date.
The rest of your code looks fine. You should check if fp and fp2 are NULL though -- if they didn't succeed in opening for some reason, your program goes ahead and tries to use them anyway, which will crash too -- or at least really ought to crash.
It might be better to program in Linux than DOS. Linux is a less forgiving environment, you'll catch some mistakes immediately which in DOS might not do anything immediately but could cause very weird side-effects later. Linux is even capable of detecting which line it crashed in if you compile with -ggdb and run the program with gdb...
Last edited by Corona688; 03-25-2011 at 04:13 PM..
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
Hi.
Sometimes lint-like tools can catch these errors. For example, on Debian GNU/Linux, there is a utility splint. The output from running it on your code is:
Best wishes ... cheers, drl
This doesn't create memory for you. All a pointer is is an integer describing where memory is. ...
Arrgh! No! Not that!
Seriously, in order to be perfectly clear: while a pointer may be an integer-type value, a pointer is NOT an int, nor a long, nor an unsigned int or unsigned long. A pointer is a pointer. Just because it might be the same number of bytes as a some specfic integer-type variable for whatever architecture you're coding in, that doesn't mean that relationship will hold for other architectures.
Along the same lines, size_t is size_t, NOT unsigned int.
If you screw that up and get it ingrained into your programming practices, when you move to another architecture (Can you say "64 bits"? Thank you, I knew you could!), you'll have some serious problems.
Seriously, in order to be perfectly clear: while a pointer may be an integer-type value, a pointer is NOT an int, nor a long, nor an unsigned int or unsigned long. A pointer is a pointer. Just because it might be the same number of bytes as a some specfic integer-type variable for whatever architecture you're coding in, that doesn't mean that relationship will hold for other architectures.
And nowhere is this more true than Borland Turbo C++, which has several kinds of pointers, of differing sizes, due to 16-bit segment weirdness.
Last edited by Corona688; 03-26-2011 at 04:32 PM..
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!... (11 Replies)
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)
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)
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)
Hi,
I am having this segmentation fault not in the following program, bt. in my lab program . My lab program is horrible long so cannot post it here bt. I am using the following logic in my program which is giving the segmentation fault. Bt. if I run this sample program as it is it dosen't give... (3 Replies)
Hi,
Why I don't receive a segmentation fault in the following sample.
int main(void)
{
char buff;
sprintf(buff,"Hello world");
printf("%s\n",buff);
}
If I define a buffer of 10 elements and I'm trying to put inside it twelve elements, Should I receive a sigsev... (22 Replies)
Hi ,
During execution a backup binary i get following error
"Program error 11 (Segmentation fault), saving core file in '/usr/datatools"
Riyaz (2 Replies)
ive written my code in C for implementation of a simple lexical analyser using singly linked list hence am making use of dynamic allocation,but when run in linux it gives a segmentation fault is it cause of the malloc function that ive made use of????any suggestions as to what i could do???
thank... (8 Replies)
I have written a program which takes a directory as command line arguments and displays all the dir and files in it.
I don't know why I have a problem with the /etc directory.It displays all the directories and files untill it reaches a sub directory called peers which is in /etc/ppp/peers.the... (4 Replies)
hello all,
I tried a program on an array to intialise array elements from the standard input device.it is an integer array of 5 elements.but after entering the 4th element it throws a message called "Segmentation Fault" and returns to the command prompt without asking for the 5th element.
... (3 Replies)