gcc program core dump 
Author Message
 gcc program core dump

There is a message of core dump may be related to the scope of
variables , please any explainations . it's from a book of C .It is
written as it is .

reults :
//--------

# gcc part01_iti_r01_ch01_TV_Rental_bills_ver03_conditional_statements.c
# ./a*

Enter customer number :1

Enter number of weeks rent due :2
Enter type of rental -c for colors TV
                     -o for otherc

 rent_per_week 3.600000

 TV RENTAL BILL

 Customer number is 7
 Number of weeks rent due is 1
 Rent per week is 3.60

 Rental due is 3.60

Segmentation Fault(coredump)
#

The source code :
//----------------

#include <stdio.h>
//TV Rental Bills -Version03 conditional statements
//TV Rental Bills -Bills only for those renting colors
main ()
{
int no_of_weeks,customer_no;
float rent_per_week,bill,weeks_rental(),calc_bill();
printf("\nEnter customer number :");
scanf("%4d",&customer_no);
printf("\nEnter number of weeks rent due :");
scanf("%2d",&no_of_weeks);
rent_per_week=weeks_rental();
printf("\n rent_per_week %f " ,rent_per_week);
if (rent_per_week!=0)
{
bill=calc_bill(no_of_weeks,rent_per_week);
print_bill(customer_no,no_of_weeks,rent_per_week,bill);

Quote:
}
return 0;
}

//Determine rent per week
float weeks_rental()
{
char tv_type;
printf("Enter type of rental -c for colors TV");
printf("\n                     -o for other");
scanf("%s",&tv_type);
if (tv_type=='c')
return(3.60);
else {
printf("\n Program does not handle this type of rental\n");
return(0);
Quote:
}
}

float calc_bill(weeks,rent)
int weeks;
float rent;
{
return(weeks*rent);
Quote:
}

print_bill(cust_no,no_weeks,rent_week,bi)
int no_weeks,cust_no;
float rent_week,bi;
{
printf("\n\n TV RENTAL BILL");
printf("\n\n Customer number is %d",cust_no);
printf("\n Number of weeks rent due is %d",no_weeks);
printf("\n Rent per week is %.2f ",rent_week);
printf("\n\n Rental due is %.2f \n\n",bi);
Quote:
}

--



Thu, 29 Dec 2005 02:21:14 GMT  
 gcc program core dump
On 12 Jul 2003 18:21:14 GMT

Quote:

> There is a message of core dump may be related to the scope of
> variables , please any explainations . it's from a book of C .It is
> written as it is .

If the code you post is direct from the book, then stop using this book
since it is severely out of date.

Quote:
> reults :
> //--------

> # gcc part01_iti_r01_ch01_TV_Rental_bills_ver03_conditional_statements.c

gcc -ansi -pedantic -Wall -W -O whatever.c

Quote:
> # ./a*

Not very sensible, but OT here.

Quote:
> Enter customer number :1

> Enter number of weeks rent due :2
> Enter type of rental -c for colors TV
>                      -o for otherc

>  rent_per_week 3.600000

>  TV RENTAL BILL

>  Customer number is 7
>  Number of weeks rent due is 1
>  Rent per week is 3.60

>  Rental due is 3.60

> Segmentation Fault(coredump)
> #

> The source code :
> //----------------

> #include <stdio.h>
> //TV Rental Bills -Version03 conditional statements
> //TV Rental Bills -Bills only for those renting colors

// comments are only available in C99 and can cause problems on news groups.
Using /* ... */ style comments is probably more sensible.

Quote:
> main ()

Main returns an int, so specify this.

int main(void)

Quote:
> {
> int no_of_weeks,customer_no;
> float rent_per_week,bill,weeks_rental(),calc_bill();

It's better to either provide full prototype for functions or to define
them before they are used. Then the compiler can tell you about passing
incorrect parameter lists to the functions.

Quote:
> printf("\nEnter customer number :");

fflush(stdout);

Otherwise the user might not get the prompt until after having input the
data. Similarly for subsequent prompts.

Quote:
> scanf("%4d",&customer_no);

Check the return value of scanf. If the user inputs garbage nothing will be
assigned to customer_no and you then invoke Undefined Behavious when you
use it,

Quote:
> printf("\nEnter number of weeks rent due :");
> scanf("%2d",&no_of_weeks);
> rent_per_week=weeks_rental();
> printf("\n rent_per_week %f " ,rent_per_week);
> if (rent_per_week!=0)
> {
> bill=calc_bill(no_of_weeks,rent_per_week);
> print_bill(customer_no,no_of_weeks,rent_per_week,bill);

As no prototypes are in scope the float arguments above will be promoted to
doubles. However, the functions expect floats so this is Undefined Behaviour.

Quote:
> }
> return 0;
> }
> //Determine rent per week
> float weeks_rental()
> {
> char tv_type;
> printf("Enter type of rental -c for colors TV");
> printf("\n                     -o for other");
> scanf("%s",&tv_type);

Buffer overflow giving undefined behaviour. %s reads a null terminated
string you pass a character variable.

Quote:
> if (tv_type=='c')
> return(3.60);
> else {
> printf("\n Program does not handle this type of rental\n");
> return(0);
> }
> }
> float calc_bill(weeks,rent)
> int weeks;
> float rent;

Old style funciot declaration. You should use
float calc_bill(int weeks,float rent)
instead.

Quote:
> {
> return(weeks*rent);
> }
> print_bill(cust_no,no_weeks,rent_week,bi)
> int no_weeks,cust_no;
> float rent_week,bi;

Again, old style declaration. Also, you don.y specify the return type so it
will default to int when you actually want void since you don't return
anything. Replace with

void print_bill(int cust_no, int no_weeks, float rent_week, float bi)

Quote:
> {
> printf("\n\n TV RENTAL BILL");
> printf("\n\n Customer number is %d",cust_no);
> printf("\n Number of weeks rent due is %d",no_weeks);
> printf("\n Rent per week is %.2f ",rent_week);
> printf("\n\n Rental due is %.2f \n\n",bi);
> }

If you turn up the warning level on your compiler it will warn about most
of the above.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
--



Fri, 30 Dec 2005 09:29:18 GMT  
 gcc program core dump

|There is a message of core dump may be related to the scope of
|variables , please any explainations . it's from a book of C .It is
|written as it is .

It seems to be either an old or a not very good book on C, given the
number of warnings about basic function declarations and prototypes
produced from such a simple program:

% cc ehab.c
"ehab.c", line 5: warning: old-style declaration or incorrect type for:
main
"ehab.c", line 17: warning: implicit function declaration: print_bill
"ehab.c", line 42: warning: old-style declaration or incorrect type for:
print_bill

It doesn't actually core dump on Solaris 9/sparc with SunONE cc 8 or gcc
2.95, nor does the SunONE dbx memory checker flag any errors, when run
with the simple input you showed.  It can easily be made to core dump
if you type a very long string to the 'Enter type of rental' prompt.

That appears to be due to an obvious error:

|char tv_type;
|scanf("%s",&tv_type);

%s is for a string.  %s should never be used with scanf like this since
you have no control over how much data will be copied to the buffer you
specified.  I could type 100 characters, and it would start copying them
into the specified buffer - but since you only allocated room for one,
it would write all over the next 100 characters worth of memory (the
other 99 characters of input and the trailing NUL byte).  In fact, with
the buffer you specified, hitting return without typing anything is the
only way not to scribble outside the bounds of memory, since that is the
only way to write simply the NUL byte.  You probably want %c with scanf
or simply getchar since all you want is a single character.

Also, indenting makes it much easier for people reading your program
to follow the structure. The code you posted is much harder to read
than it needs to be.

--
________________________________________________________________________


  Working for, but definitely not speaking for, Sun Microsystems, Inc.
--



Fri, 30 Dec 2005 09:29:34 GMT  
 gcc program core dump

Quote:
>There is a message of core dump may be related to the scope of
>variables , please any explainations . it's from a book of C .It is
>written as it is .

>reults :
>//--------

># gcc part01_iti_r01_ch01_TV_Rental_bills_ver03_conditional_statements.c
># ./a*

>Enter customer number :1

>Enter number of weeks rent due :2
>Enter type of rental -c for colors TV
>                     -o for otherc

> rent_per_week 3.600000

> TV RENTAL BILL

> Customer number is 7
> Number of weeks rent due is 1
> Rent per week is 3.60

> Rental due is 3.60

>Segmentation Fault(coredump)
>#

>The source code :
>//----------------

>#include <stdio.h>
>//TV Rental Bills -Version03 conditional statements
>//TV Rental Bills -Bills only for those renting colors
>main ()

Implicit return types have been eliminated from the standard.  You
must specify that main returns an int.  Try
     int main(void)

Quote:
>{
>int no_of_weeks,customer_no;

Make life easy on yourself.  Try to make your code readable (to
yourself and others).  One way is to use a new line for each
declaration and definition.

Quote:
>float rent_per_week,bill,weeks_rental(),calc_bill();
>printf("\nEnter customer number :");
>scanf("%4d",&customer_no);
>printf("\nEnter number of weeks rent due :");
>scanf("%2d",&no_of_weeks);
>rent_per_week=weeks_rental();

Another way is to use a little white space"
     rent_per_week = weeks_rental();

Quote:
>printf("\n rent_per_week %f " ,rent_per_week);
>if (rent_per_week!=0)
>{

And another way is to indent a visually significant amount (many
recommend four or five characters) to show the structure of program
blocks.

Quote:
>bill=calc_bill(no_of_weeks,rent_per_week);

Let the compiler help you out as much as possible.  Your prototype for
this function (up in your first float statement) did not describe the
parameters.  This will prevent the compiler from checking (and
possibly converting) the arguments when you call the function.  Your
prototype should read
     float calc_bill(int, float);

Quote:
>print_bill(customer_no,no_of_weeks,rent_per_week,bill);

There is no prototype in scope for this function.

Quote:
>}
>return 0;
>}
>//Determine rent per week
>float weeks_rental()
>{
>char tv_type;
>printf("Enter type of rental -c for colors TV");
>printf("\n                     -o for other");

printf writes to sdtout which can be buffered.  If you want to insure
that your prompt is visible to the user before the system waits for
the input to the following scanf, you must either end your output with
a '\n' or add the following statement between the printf and the
scanf:
     flush(stdout);

Quote:
>scanf("%s",&tv_type);

Here is the cause of your problem.  You are asking the user to enter a
character and press the enter key.  Your %s format tells scanf that
the result should be stored as a string in tv_type.  The string will
be two characters long, one for the character typed in ('c' or 'o')
and one for the mandatory '\0' that terminates the string.  However,
tv_type is a char which, by definition, has room for only on
character.  This is a fairly typical example of buffer overflow.  It
invokes undefined behavior.  You were lucky - a segmentation fault is
one the nicest types of undefined behavior you can experience.

Check your book again.  Is it by Schildt?  Did it use a %s or a %c?  A
char or an array of char?  If it really tried to read a %s into a
char, you need a better book.

As noted below, your book is using an archaic syntax that has been
deprecated in the current standard.  Since the language has evolved
considerably since the time of this syntax, you may want to get
something a little more current.

Quote:
>if (tv_type=='c')
>return(3.60);
>else {
>printf("\n Program does not handle this type of rental\n");
>return(0);
>}
>}
>float calc_bill(weeks,rent)
>int weeks;
>float rent;

This is the archaic syntax I referred to.  In the current language,
this would be one statement:
     float calc_bill(int weeks, float rent)

Quote:
>{
>return(weeks*rent);
>}
>print_bill(cust_no,no_weeks,rent_week,bi)
>int no_weeks,cust_no;
>float rent_week,bi;
>{
>printf("\n\n TV RENTAL BILL");
>printf("\n\n Customer number is %d",cust_no);
>printf("\n Number of weeks rent due is %d",no_weeks);
>printf("\n Rent per week is %.2f ",rent_week);
>printf("\n\n Rental due is %.2f \n\n",bi);
>}

<<Remove the del for email>>
--



Fri, 30 Dec 2005 09:29:40 GMT  
 gcc program core dump

writes

Quote:
>//Determine rent per week
>float weeks_rental()
>{
>char tv_type;
>printf("Enter type of rental -c for colors TV");
>printf("\n                     -o for other");
>scanf("%s",&tv_type);

If this is actually what was in the book and not:
scanf("%c", &tv_type);

then the book is dangerously bad written by an author who should not be
allowed to corrupt the newcomer. The declaration of tv_type provides
storage for 1 char, for all but the empty string %s will require a
minimum of 2. IOWs using this code is guaranteed to corrupt memory.

--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Sat, 31 Dec 2005 00:45:08 GMT  
 gcc program core dump

Apart from the fact that you're writing code that is far
from being standard conforming (K&R),

Quote:

> The source code :
> //----------------
> char tv_type;
> printf("Enter type of rental -c for colors TV");
> printf("\n                     -o for other");
> scanf("%s",&tv_type);

        ^^^^
Here's your error. tv_type is a char, but scanf is writing at least
2 chars to the variable: the character you enter plus the terminating zero.

make that either
 char tv_type[2];
 scanf("%1s", &tv_type);

or
 scanf("%c", &tv_type);

And your segfault will go away.

And do yourself a favor and junk that book and start writing ISO C code.

Philipp

--

SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nuremberg, Germany
--



Fri, 06 Jan 2006 07:54:10 GMT  
 gcc program core dump


Quote:
> On 12 Jul 2003 18:21:14 GMT


<other good corrections snipped>
Quote:
> > {
> > int no_of_weeks,customer_no;
> > float rent_per_week,bill,weeks_rental(),calc_bill();

> It's better to either provide full prototype for functions or to define
> them before they are used. Then the compiler can tell you about passing
> incorrect parameter lists to the functions.

Plus it's more conventional, expected, and almost always better to
declare (preferably prototype) functions at file scope, rather than
within a block/function as here, though this is legal.  If you move
the definition up, the issue doesn't arise, as function definitions
can only occur at file scope.

Quote:
> > print_bill(customer_no,no_of_weeks,rent_per_week,bill);

> As no prototypes are in scope the float arguments above will be promoted to
> doubles. However, the functions expect floats so this is Undefined Behaviour.

No, an *old-style* (nonprototype) definition with a float parameter,
which is what the OP had, does work correctly with a nonprototyped and
hence default-promoted call.  In fact if you want to write a prototype
declaration for such a definition, you must use the *promoted* type.
And similarly for (various signednesses of) short and char.

A prototyped definition, and a prototyped declaration if any, are
better, but both old-style is valid if done correctly.

- David.Thompson1 at worldnet.att.net
--



Fri, 06 Jan 2006 07:56:06 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Core dump: EMX/gcc and os2

2. Can't Happen OR /* NOT REACHED */ OR Real Programs Dump Core

3. Thread program core dump

4. core dump on program exit

5. The shortest core-dumping program

6. Getting a core dump in my program.

7. core dump for a short program

8. core dumped when a program is run from inside a shell

9. segmentation fault(Core dumped)

10. segmentation fault(Core dumped)

11. memset core dump

12. provent abort from producing core dump?

 

 
Powered by phpBB® Forum Software