![]() |
|
#1
|
|||
|
|||
c simple questionis there any effect if i compile a program for several time. I just don't understand sometimes i don't make any change to the program and suddenly i got segmentation fault and sometimes the compiler detect error in something that's not wrong. WHats Happened ?? I use gcc under linux is there any bugs? How to handle this problem?
|
|
#2
|
|||
|
|||
|
Segmentation faults are mainly caused when your program (or statements within a program to be specific) try to write data in memory location thats does not belong to them. for e.g non-malloced char pointers. When they are used to store the strings using strcpy(), it produces a sure crash.
This has got nothing to do with complier. Compiler will not detect run time problems in the program.(atleast I haven't seen gcc doing that). It will only check for the syntax of statements in your program. It may give you some sort hints about the possible problems with syntax in your code but definitely can not give you hint of the missing code or wrong logic in your code. For any compiler, following set of statments are perfectly are valid and it will not give any warning or error at compliation time. CPP / C++ / C Code:
however this is a sure crash when program is executed. strcpy() will attempt to write in an un allocated memory area which does not belong to your program.(some programmers like to refer to it as "nowhereland"). As a progammer you should allocate memory to "str" pointer first, before actually storing something into it. In other words, make "str" pointer point to a valid memory location owned by your program. Also, make sure that there is no overflow. i.e. data being stroed in "str" does not exceeds the available storage space with "str" variable after memory allocation. To correct the runtime problem with above code . CPP / C++ / C Code:
The above code will execute perfectly fine. What we have done is allocated required amount of memory to "str". We need to cast the generic pointer returned by malloc to the require storage type on left side of "=" operator, to avoid compiler warning about "incompatible pointer assignments". Also, in above example, you may have noticed that I have allocated any additional charater storage than actual size of the data. This is becuase I want to store the end-of-line character after the string that I store in str. It is important to terminate your strings with end of line ('\0') character, to avoid crashes with statments that involves measuring the legth of the string or reading characters from the string one at a time till it hits end-of-line character. These statments in your program may produces crash at runtime if strings are not properly terminated. Again, your complier will not detect this mistake in your code at compliation time. |
|
#3
|
||||
|
||||
|
Quote:
hehe segfault -- the bane of C programming. This is by far the most common run-time error and in my opinion one of the hardest to track down. nkhambal has given a good explanation for what this is - it is an attempt to access memory that your program shouldn't. C has no bounds checking, so it is going to try to do whatever you tell it. The problem with segfault as with all runtime errors is that they don't always occur. Sometimes your program logic goes around them or only a certain situation will cause a segfault. The key to identifying it is reproducing it. As for the code that compiled once, but doesn't now? I don't know about that... I have used gcc under linux for years and the only time that my code doesn't compile that use to compile is when I have changed the code, the machine, upgraded gcc or linux, etc. etc. I dare say I am 100% positive that this is not a bug under gcc in linux. __________________
The best damn Sports Blog period. |
Recent GIDBlog
Stupid Management Policies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| c simple question problem with switch case | if13121 | C Programming Language | 2 | 25-Oct-2004 02:06 |
| another c simple question | if13121 | C Programming Language | 1 | 20-Oct-2004 14:27 |
| very simple c question | if13121 | C Programming Language | 1 | 18-Oct-2004 01:12 |
| Simple Question regarding installing Apache 2.0 | macjoubert | Apache Web Server Forum | 0 | 13-Nov-2003 12:39 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The