GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Oct-2004, 04:28
if13121 if13121 is offline
New Member
 
Join Date: Oct 2004
Posts: 27
if13121 is on a distinguished road

c simple question


is 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  
Old 28-Oct-2004, 05:24
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
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:

chat *str;

strcpy(str,"Hello World");

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:

char *str;

str=(char *) malloc(12*(sizeof(char));

strcpy(str,"Hello World");

str[11]='\0';


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  
Old 28-Oct-2004, 08:15
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by if13121
is 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?

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.
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 06:43.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.