By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,814 Members | 1,678 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,814 IT Pros & Developers. It's quick & easy.

General Protection Exception

P: n/a
I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
/* Integer declarations */
const int exitcode = -1;
float arraylength = 0;
float angfreq = 0;
float* time = (float*)NULL;
float* output = (float*)NULL;
float ysinesum = 0;
float ycossum = 0;
float sumy = 0;
float acoef = 0;
float bcoef = 0;
float ccoef = 0;
int count = 0;

/* Input section */
printf("How many pairs of data are available?");
scanf("%f", &arraylength);
if(arraylength<1) {
printf("Invalid array length\n");
exit(exitcode);
}
printf("Enter the angular frequency: ");
scanf("%f", &angfreq);

for(count = 0;count<=(arraylength-1);count++) {
printf("Enter the time for entry %d: ",count);
scanf("%f", &time[count]);
printf("Enter the output cooresponding to t = %d: ", count);
scanf("%f", &output[count]);
}

/* Calculation Section */
for(count = 0;count<=(arraylength-1);count++) {
sumy = sumy + output[count];
ysinesum = ysinesum + output[count]*sin(angfreq*time[count]);
ycossum = ycossum + output[count]*cos(angfreq*time[count]);
}

acoef = sumy / (arraylength - 1);
bcoef = (2/(arraylength - 1)) * ycossum;
ccoef = (2/(arraylength - 1)) * ysinesum;

/* Output Section */
printf("The equation of the sinusoid is: \n");
printf("%3.3f + %3.3f cos(%3.3f t) + %3.3f sin(%3.3f t)\n");
return 0;
}

Thanks,
Dave

Jan 9 '07 #1
Share this Question
Share on Google+
37 Replies


P: n/a
dm******@cox.net <dm******@cox.netwrote:
I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.
I realize that being a mathematician you won't care about some of the
advice I'll give you, but you should if you want to use C properly in
the future:
>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
use: int main(void)
{
/* Integer declarations */
const int exitcode = -1;
float arraylength = 0;
float angfreq = 0;
float* time = (float*)NULL;
float* output = (float*)NULL;
no need for the casts.
float ysinesum = 0;
float ycossum = 0;
float sumy = 0;
float acoef = 0;
float bcoef = 0;
float ccoef = 0;
int count = 0;

/* Input section */
printf("How many pairs of data are available?");
scanf("%f", &arraylength);
if(arraylength<1) {
printf("Invalid array length\n");
exit(exitcode);
}
printf("Enter the angular frequency: ");
scanf("%f", &angfreq);
It is a good idea to check the value returned by scanf to make sure it
read properly.
>
for(count = 0;count<=(arraylength-1);count++) {
printf("Enter the time for entry %d: ",count);
scanf("%f", &time[count]);
You did not allocate memory for time which is of type pointer to float
and is initialized to NULL. You are trying to write in a place that
doesn't belong to you. This is, probably, the reason for your error,
although I don't remember what the error means in Turbo C. Protection
Exception makes me believe that's something related to your attempt to
write where you shouldn't.
To allocate space for arraylength elements you should use this:
time=malloc(arraylength*sizeof(double))
or
time=malloc(arraylength*sizeof*arraylength)
then you should check and make sure the memory was allocated:
if(time==NULL) {
printf("Oooops");
/* ... add code to handle the allocation problem */
/* probably to cleanup and exit */
}

If everything goes well, do not forget to free the memory allocated by
malloc.

The same goes lower with output.
printf("Enter the output cooresponding to t = %d: ", count);
scanf("%f", &output[count]);
Same mistake as above. You didn't allocate memory for output.
}

/* Calculation Section */
for(count = 0;count<=(arraylength-1);count++) {
sumy = sumy + output[count];
ysinesum = ysinesum + output[count]*sin(angfreq*time[count]);
ycossum = ycossum + output[count]*cos(angfreq*time[count]);
}
You're using output although it doesn't have memory allocated for it.
Your program probably doesn't get here, anyway.
>
acoef = sumy / (arraylength - 1);
bcoef = (2/(arraylength - 1)) * ycossum;
ccoef = (2/(arraylength - 1)) * ysinesum;
You should test for correct input for arraylength, otherwise you may
have surprises when you divide by 0.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Jan 9 '07 #2

P: n/a
On 8 Jan 2007 18:55:30 -0800, "dm******@cox.net" <dm******@cox.net>
wrote:
>I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
/* Integer declarations */
const int exitcode = -1;
It would be more portable to remove this and use EXIT_FAILURE as the
argument to exit().
> float arraylength = 0;
It is a bit unusual to use a float to hold the number of elements of
an array.
> float angfreq = 0;
float* time = (float*)NULL;
First, remove the casts. They serve no purpose.

Each of these pointers is initialized to point to "nowhere". In
particular, they do not point to memory you own or have legal access
to. You cannot extract data from where they point nor can you store
data there. Since the pointers are initialized, you can test them for
equality with other pointers or pointer constants, but you may not
perform arithmetic on them.
> float* output = (float*)NULL;
float ysinesum = 0;
float ycossum = 0;
float sumy = 0;
float acoef = 0;
float bcoef = 0;
float ccoef = 0;
int count = 0;

/* Input section */
printf("How many pairs of data are available?");
scanf("%f", &arraylength);
if(arraylength<1) {
printf("Invalid array length\n");
exit(exitcode);
}
printf("Enter the angular frequency: ");
scanf("%f", &angfreq);

for(count = 0;count<=(arraylength-1);count++) {
printf("Enter the time for entry %d: ",count);
scanf("%f", &time[count]);
Here you violate all those restrictions. The expression &time[count]
is identical to the expression time+count. This is not a valid
address. And then you expect scanf to store something at that
address.

Before you can use time to reference an array of values, you must
initialize it so that it points to memory you can use. The
traditional method is
time = malloc(arraylength * sizeof *time);

This will, if it succeeds, allocate a contiguous block of memory
capable of holding arraylength objects, each of which is the size of
the object type time points to, in this case float.

You should always check to see if malloc succeeded by testing the
return value against NULL.
> printf("Enter the output cooresponding to t = %d: ", count);
scanf("%f", &output[count]);
Ditto.
> }

/* Calculation Section */
for(count = 0;count<=(arraylength-1);count++) {
sumy = sumy + output[count];
ysinesum = ysinesum + output[count]*sin(angfreq*time[count]);
ycossum = ycossum + output[count]*cos(angfreq*time[count]);
}

acoef = sumy / (arraylength - 1);
bcoef = (2/(arraylength - 1)) * ycossum;
ccoef = (2/(arraylength - 1)) * ysinesum;

/* Output Section */
printf("The equation of the sinusoid is: \n");
printf("%3.3f + %3.3f cos(%3.3f t) + %3.3f sin(%3.3f t)\n");
Each of the %3.3f terms in the format string means there must be a
corresponding argument following the string which evaluates to a
double. You don't have any arguments after the string. It is OK to
use a float value because such values are promoted to double when
passed to printf.
> return 0;
}

Thanks,
Dave

Remove del for email
Jan 9 '07 #3

P: n/a
On Tue, 09 Jan 2007 03:31:55 +0000, Nelu wrote:
dm******@cox.net <dm******@cox.netwrote:
>I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.

I realize that being a mathematician you won't care about some of the
advice I'll give you,
Didn't your mummy teach you to leave your prejudices at home? Or
perhaps you are so small minded that just can't?
Jan 9 '07 #4

P: n/a

Barry Schwarz wrote:
On 8 Jan 2007 18:55:30 -0800, "dm******@cox.net" <dm******@cox.net>
wrote:
I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
/* Integer declarations */
const int exitcode = -1;

It would be more portable to remove this and use EXIT_FAILURE as the
argument to exit().
float arraylength = 0;

It is a bit unusual to use a float to hold the number of elements of
an array.
float angfreq = 0;
float* time = (float*)NULL;

First, remove the casts. They serve no purpose.

Each of these pointers is initialized to point to "nowhere". In
particular, they do not point to memory you own or have legal access
to. You cannot extract data from where they point nor can you store
data there. Since the pointers are initialized, you can test them for
equality with other pointers or pointer constants, but you may not
perform arithmetic on them.
float* output = (float*)NULL;
float ysinesum = 0;
float ycossum = 0;
float sumy = 0;
float acoef = 0;
float bcoef = 0;
float ccoef = 0;
int count = 0;

/* Input section */
printf("How many pairs of data are available?");
scanf("%f", &arraylength);
if(arraylength<1) {
printf("Invalid array length\n");
exit(exitcode);
}
printf("Enter the angular frequency: ");
scanf("%f", &angfreq);

for(count = 0;count<=(arraylength-1);count++) {
printf("Enter the time for entry %d: ",count);
scanf("%f", &time[count]);

Here you violate all those restrictions. The expression &time[count]
is identical to the expression time+count. This is not a valid
address. And then you expect scanf to store something at that
address.

Before you can use time to reference an array of values, you must
initialize it so that it points to memory you can use. The
traditional method is
time = malloc(arraylength * sizeof *time);

This will, if it succeeds, allocate a contiguous block of memory
capable of holding arraylength objects, each of which is the size of
the object type time points to, in this case float.

You should always check to see if malloc succeeded by testing the
return value against NULL.
printf("Enter the output cooresponding to t = %d: ", count);
scanf("%f", &output[count]);

Ditto.
}

/* Calculation Section */
for(count = 0;count<=(arraylength-1);count++) {
sumy = sumy + output[count];
ysinesum = ysinesum + output[count]*sin(angfreq*time[count]);
ycossum = ycossum + output[count]*cos(angfreq*time[count]);
}

acoef = sumy / (arraylength - 1);
bcoef = (2/(arraylength - 1)) * ycossum;
ccoef = (2/(arraylength - 1)) * ysinesum;

/* Output Section */
printf("The equation of the sinusoid is: \n");
printf("%3.3f + %3.3f cos(%3.3f t) + %3.3f sin(%3.3f t)\n");

Each of the %3.3f terms in the format string means there must be a
corresponding argument following the string which evaluates to a
double. You don't have any arguments after the string. It is OK to
use a float value because such values are promoted to double when
passed to printf.
return 0;
}

Thanks,
Dave


Remove del for email
Thanks for all of your replies; this is the first major program I've
written since I've gotten my degree in math. I will look at these again
tomorrow when I look at it again. I think I really need to get a better
background in C; I only took one programming class in college.

Dave

Jan 9 '07 #5

P: n/a
J. Sommers <js******@sessrimnir.netwrote:
On Tue, 09 Jan 2007 03:31:55 +0000, Nelu wrote:
>dm******@cox.net <dm******@cox.netwrote:
>>I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.

I realize that being a mathematician you won't care about some of the
advice I'll give you,

Didn't your mummy teach you to leave your prejudices at home? Or
perhaps you are so small minded that just can't?
It's not prejudice. I have friends that are mathematicians and none of
them cares about checking the return value of scanf, malloc, and any
other checks, in general. They usually rely on the fact that those
functions will not fail for their needs.
That's all.
I aplogize if I hurt anyone's feelings.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Jan 9 '07 #6

P: n/a
On 8 Jan 2007 18:55:30 -0800, "dm******@cox.net" <dm******@cox.net>
wrote in comp.lang.c:
I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.
There are so many things wrong with this program, meaning that you
have so many misunderstandings about C, that I will probably miss
some.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
/* Integer declarations */
const int exitcode = -1;
In C++ they often use const qualified objects to replace constant
expressions or literals, but this is not idiomatic in C, nor do C
compilers always optimize it as well.

Furthermore, returning -1 from main(), or passing it to exit(), is
non-standard and non-portable and the result is
implementation-defined. Much better, especially since you have
already included <stdlib.h>, is to use the standard and portable macro
EXIT_FAILURE to indicate unsuccessful program termination.
float arraylength = 0;
Given the way you use "arraylength" below, it should be an integer
type, not a floating point type.
float angfreq = 0;
float* time = (float*)NULL;
"time" is a legal, but ill-advised, name for an object at block scope.
The standard C library contains a function named time(), prototyped in
the header <time.h>, and that name is reserved at file scope. It is
not reserved inside a function, as you use it here, but might well
confuse a reader.
float* output = (float*)NULL;
The (float*) cast in the two initializations is completely unnecessary
and serves only to clutter your program and slightly obscure it.
Eliminate them. The placement of the asterisk suggests that you are
actually programming in C++, but the cast is not necessary there
either.
float ysinesum = 0;
float ycossum = 0;
float sumy = 0;
float acoef = 0;
float bcoef = 0;
float ccoef = 0;
int count = 0;

/* Input section */
printf("How many pairs of data are available?");
scanf("%f", &arraylength);
scanf() is a very poor choice to use for interactive user input, for a
variety of reasons. Not the least of which is the fact that the
behavior is undefined if the scanned text represents a value outside
the range of the destination type.
if(arraylength<1) {
printf("Invalid array length\n");
exit(exitcode);
Here is where you should have:
exit(EXIT_FAILURE);
printf("Enter the angular frequency: ");
scanf("%f", &angfreq);

for(count = 0;count<=(arraylength-1);count++) {
printf("Enter the time for entry %d: ",count);
scanf("%f", &time[count]);
Here's where your reap problem starts. You are asking scanf() to
write a value into time[copunt], but "time" is a null pointer, you
specifically initialized it that way. "time" does not point to any
floats, in fact it does not point to any valid memory that your
program can access.

When you try to write to memory through a null pointer, you produce
undefined behavior and under many modern operating systems, generate
some sort of fault that causes the OS to terminate your program.
printf("Enter the output cooresponding to t = %d: ", count);
scanf("%f", &output[count]);
}

/* Calculation Section */
for(count = 0;count<=(arraylength-1);count++) {
sumy = sumy + output[count];
ysinesum = ysinesum + output[count]*sin(angfreq*time[count]);
ycossum = ycossum + output[count]*cos(angfreq*time[count]);
}

acoef = sumy / (arraylength - 1);
bcoef = (2/(arraylength - 1)) * ycossum;
ccoef = (2/(arraylength - 1)) * ysinesum;

/* Output Section */
printf("The equation of the sinusoid is: \n");
printf("%3.3f + %3.3f cos(%3.3f t) + %3.3f sin(%3.3f t)\n");
return 0;
}
Somewhere after the definition of the pointers "time" and "output" and
getting a value for "count", you need to allocate memory to these
pointers to hold the values you want to store there.

Something like this:

time = malloc(count * sizeof *time);
output = malloc(count * sizeof *output);
if ((NULL == time) || (NULL == output))
{
printf("Memory allocation failure\n");
free(time);
free(output);
exit (EXIT_FAILURE);
}

And at the end of your program, before the perfectly correct "return
0;", you should add:

free(time);
free(output);

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jan 9 '07 #7

P: n/a

Nelu wrote:
J. Sommers <js******@sessrimnir.netwrote:
On Tue, 09 Jan 2007 03:31:55 +0000, Nelu wrote:
dm******@cox.net <dm******@cox.netwrote:
I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.

I realize that being a mathematician you won't care about some of the
advice I'll give you,
Didn't your mummy teach you to leave your prejudices at home? Or
perhaps you are so small minded that just can't?

It's not prejudice. I have friends that are mathematicians and none of
them cares about checking the return value of scanf, malloc, and any
other checks, in general. They usually rely on the fact that those
functions will not fail for their needs.
That's all.
I aplogize if I hurt anyone's feelings.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
None taken; I really think that a programming class should've been
required for my math degree. I took it as part of my other degree
(meteorology).

Dave

Jan 9 '07 #8

P: n/a
"dm******@cox.net" wrote:
>
I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code
is below.
.... snip code ...

Your code is apparently C, not C++.

After passing your code through indent to eliminate the excessive
indentation etc. and make it readable, I compiled it. An obvious
error is the value of errorcode, which can only be 0, EXIT_SUCCESS,
or EXIT_FAILURE. Similarly casting NULL to float is evil. If you
want 0.0 say so. Casts are always suspect, and probably errors.
I see no sign of allocation of the arrays, which I suspect should
exist because of the presence of the variable arraylength (and why
should that be a float?). Just a cursory look.

Results follow:

junk.c: In function `main':
junk.c:53: warning: too few arguments for format

The line involved is the last printf.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
/* Integer declarations */
const int exitcode = -1;
float arraylength = 0;
float angfreq = 0;
float *time = (float *)NULL;
float *output = (float *)NULL;
float ysinesum = 0;
float ycossum = 0;
float sumy = 0;
float acoef = 0;
float bcoef = 0;
float ccoef = 0;
int count = 0;

/* Input section */
printf("How many pairs of data are available?");
scanf("%f", &arraylength);
if (arraylength < 1) {
printf("Invalid array length\n");
exit(exitcode);
}
printf("Enter the angular frequency: ");
scanf("%f", &angfreq);

for (count = 0; count <= (arraylength - 1); count++) {
printf("Enter the time for entry %d: ", count);
scanf("%f", &time[count]);
printf("Enter the output cooresponding to t = %d: ", count);
scanf("%f", &output[count]);
}

/* Calculation Section */
for (count = 0; count <= (arraylength - 1); count++) {
sumy = sumy + output[count];
ysinesum = ysinesum +
output[count] * sin(angfreq * time[count]);
ycossum = ycossum +
output[count] * cos(angfreq * time[count]);
}

acoef = sumy / (arraylength - 1);
bcoef = (2 / (arraylength - 1)) * ycossum;
ccoef = (2 / (arraylength - 1)) * ysinesum;

/* Output Section */
printf("The equation of the sinusoid is: \n");
printf("%3.3f + %3.3f cos(%3.3f t) + %3.3f sin(%3.3f t)\n");
return 0;
}

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Jan 9 '07 #9

P: n/a

Jack Klein wrote:
On 8 Jan 2007 18:55:30 -0800, "dm******@cox.net" <dm******@cox.net>
wrote in comp.lang.c:
I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.

There are so many things wrong with this program, meaning that you
have so many misunderstandings about C, that I will probably miss
some.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
/* Integer declarations */
const int exitcode = -1;

In C++ they often use const qualified objects to replace constant
expressions or literals, but this is not idiomatic in C, nor do C
compilers always optimize it as well.

Furthermore, returning -1 from main(), or passing it to exit(), is
non-standard and non-portable and the result is
implementation-defined. Much better, especially since you have
already included <stdlib.h>, is to use the standard and portable macro
EXIT_FAILURE to indicate unsuccessful program termination.
float arraylength = 0;

Given the way you use "arraylength" below, it should be an integer
type, not a floating point type.
float angfreq = 0;
float* time = (float*)NULL;

"time" is a legal, but ill-advised, name for an object at block scope.
The standard C library contains a function named time(), prototyped in
the header <time.h>, and that name is reserved at file scope. It is
not reserved inside a function, as you use it here, but might well
confuse a reader.
float* output = (float*)NULL;

The (float*) cast in the two initializations is completely unnecessary
and serves only to clutter your program and slightly obscure it.
Eliminate them. The placement of the asterisk suggests that you are
actually programming in C++, but the cast is not necessary there
either.
float ysinesum = 0;
float ycossum = 0;
float sumy = 0;
float acoef = 0;
float bcoef = 0;
float ccoef = 0;
int count = 0;

/* Input section */
printf("How many pairs of data are available?");
scanf("%f", &arraylength);

scanf() is a very poor choice to use for interactive user input, for a
variety of reasons. Not the least of which is the fact that the
behavior is undefined if the scanned text represents a value outside
the range of the destination type.
if(arraylength<1) {
printf("Invalid array length\n");
exit(exitcode);

Here is where you should have:
exit(EXIT_FAILURE);
printf("Enter the angular frequency: ");
scanf("%f", &angfreq);

for(count = 0;count<=(arraylength-1);count++) {
printf("Enter the time for entry %d: ",count);
scanf("%f", &time[count]);

Here's where your reap problem starts. You are asking scanf() to
write a value into time[copunt], but "time" is a null pointer, you
specifically initialized it that way. "time" does not point to any
floats, in fact it does not point to any valid memory that your
program can access.

When you try to write to memory through a null pointer, you produce
undefined behavior and under many modern operating systems, generate
some sort of fault that causes the OS to terminate your program.
printf("Enter the output cooresponding to t = %d: ", count);
scanf("%f", &output[count]);
}

/* Calculation Section */
for(count = 0;count<=(arraylength-1);count++) {
sumy = sumy + output[count];
ysinesum = ysinesum + output[count]*sin(angfreq*time[count]);
ycossum = ycossum + output[count]*cos(angfreq*time[count]);
}

acoef = sumy / (arraylength - 1);
bcoef = (2/(arraylength - 1)) * ycossum;
ccoef = (2/(arraylength - 1)) * ysinesum;

/* Output Section */
printf("The equation of the sinusoid is: \n");
printf("%3.3f + %3.3f cos(%3.3f t) + %3.3f sin(%3.3f t)\n");
return 0;
}

Somewhere after the definition of the pointers "time" and "output" and
getting a value for "count", you need to allocate memory to these
pointers to hold the values you want to store there.

Something like this:

time = malloc(count * sizeof *time);
output = malloc(count * sizeof *output);
if ((NULL == time) || (NULL == output))
{
printf("Memory allocation failure\n");
free(time);
free(output);
exit (EXIT_FAILURE);
}

And at the end of your program, before the perfectly correct "return
0;", you should add:

free(time);
free(output);

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Thanks, Jack. Bear with me as I am relearning a lot of this. I got away
with not having to program for about a year and a half after I got my
degree so I'm probably due.

Dave

Jan 9 '07 #10

P: n/a
J. Sommers wrote:
>I realize that being a mathematician you won't care about some of the
advice I'll give you,

Didn't your mummy teach you to leave your prejudices at home? Or
perhaps you are so small minded that just can't?
Such prejudices usually don't develop until after you are forced to work
with mathematicians, then they aren't so much prejudices as judgements.
Jan 9 '07 #11

P: n/a
Jack Klein wrote:
> float arraylength = 0;

Given the way you use "arraylength" below, it should be an integer
type, not a floating point type.
A number of people have said this, but by itself it's misleading. The
OP later uses arraylength in expressions that are intended to be float
valued:
> bcoef = (2/(arraylength - 1)) * ycossum;
ccoef = (2/(arraylength - 1)) * ysinesum;
If he declares arraylength as an int, the subexpression

2 / ( arraylength - 1 )

will be evaluated using integer arithmetic, and will have the value 0
for arraylength 3.

Assuming an arraylength declared as an int, the subexpression must be
coerced to a floating-point type. Either

2 / (( float ) arraylength - 1 )

or

2.0 / ( arraylength - 1 )

Or he could rearrange the full expression:

bcoef = ycossum * 2 / ( arraylength - 1 );

In this case, as long as ycossum is a floating-point type, the entire
expression, written in this order, is evaluated in floating-point.

Making arraylength an int is the right thing to do, but you also have to
account for the numerical consequences.

- Ernie http://home.comcast.net/~erniew
Jan 9 '07 #12

P: n/a
dm******@cox.net wrote:
I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it.
A GPF (general protection fault) is what happens on x86-based machines
when your program does something it's not allowed to do, and by far the
most common cause is trying to access memory that doesn't belong to you.

In your program, you declare a couple of pointers,
float* time = (float*)NULL;
float* output = (float*)NULL;
and later try to store some numbers in the memory they point to,
scanf("%f", &time[count]);
scanf("%f", &output[count]);
but since you didn't actually allocate any memory for them, they don't
point to anywhere.

A couple of observations in addition to the ones others have already made:

scanf() can be a minefield. An alternative that's often better is to use
something like fgets() in a custom function. For example,

int enter_int( void )
{
char buf[ 80 ];

fgets( buf, sizeof buf, stdin );
return atoi( buf );
}

You can make this as fancy as you want, and all the extra work remains in
this one function, no matter how many times you need to get an input from
the user. It could print the prompt and enforce range restrictions:

int enter_int( char *prompt, int lo, int hi )
{
char buf[ 80 ];
int i;

while ( 1 ) {
printf( "%s\n", prompt );
fgets( buf, sizeof buf, stdin );
i = atoi( buf );
if ( i >= lo && i <= hi )
return i;
printf( "The number must be between %d and %d.\n", lo, hi );
}
}

Or it could optionally get the input from any stream, e.g. a data file,
rather than solely from the user.

int enter_int( FILE *fp )
{
char buf[ 80 ];

fgets( buf, sizeof buf, fp );
return atoi( buf );
}
for(count = 0;count<=(arraylength-1);count++) {
The more common and concise idiom for this is

for ( count = 0; count < arraylength; count++ ) {
sumy = sumy + output[count];
C allows you to write this more concisely as

sumy += output[ count ];

Whichever way you write it, beware of the numerical behavior of this
kind of accumulation. In the worst case, roundoff error can grow at
each iteration. Consider the following program.

#include <stdio.h>

int main( void )
{
float f = 1000.0;
int i;

for ( i = 0; i < 1000; i++ )
f += 0.1;
printf( "%.6f\n", f );
return 0;
}

You might expect this to print "1100.000000", but in reality it prints
"1099.975586" (typically).
printf("The equation of the sinusoid is: \n");
printf("%3.3f + %3.3f cos(%3.3f t) + %3.3f sin(%3.3f t)\n");
If the intent is simply to print the number with 3 decimal places, you
don't need the width component of the format specifier. In other words,
rather than "%3.3f", you only need "%.3f".

- Ernie http://home.comcast.net/~erniew
Jan 9 '07 #13

P: n/a
On Tue, 09 Jan 2007 04:00:10 +0000, Nelu wrote:
J. Sommers <js******@sessrimnir.netwrote:
>On Tue, 09 Jan 2007 03:31:55 +0000, Nelu wrote:
>>dm******@cox.net <dm******@cox.netwrote:
I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.

I realize that being a mathematician you won't care about some of the
advice I'll give you,

Didn't your mummy teach you to leave your prejudices at home? Or
perhaps you are so small minded that just can't?

It's not prejudice.
It is prejudice.
I have friends that are mathematicians and none of them cares about
checking the return value of scanf, malloc, and any other checks, in
general.
How many friends do you have that are mathematicians? Ten? Twenty? How
does that compare with the thousands of mathematicians world over? Even
those ten or twenty systematically, and in every single occasion were to
ignore your advice that would not imply that all mathematicians would (but
it would probably imply that there is something systematically weird with
your advice).

I also have a number a software engineer friends who entertain astounding
superstitions, but that is no evidence that all software engineers do so.
You just can't generalize from such small samples and anecdotal evidence.
They usually rely on the fact that those functions will not
fail for their needs. That's all.
Which might be a sensible thing to do for them. C purity and perfection
might be your goal, but other people will just want their little C code to
run well enough to solve their problem, even if, say, they use

int main()

rather than

int main(int argc, char **argv)

I aplogize if I hurt anyone's feelings.
Those who have the guts to apologize in public tend to be a cut above the
rest. Kudos to you. Just remember to keep your prejudices in check. We all
have prejudices - otherwise we could not make sense of a world where
information is almost always incomplete.
Jan 9 '07 #14

P: n/a
Ivar Rosquist said:

<snip>
You just can't generalize from such small samples and anecdotal evidence.
Oh yes he can - as he just proved. :-)

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 9 '07 #15

P: n/a
Ivar Rosquist <IR*******@irq.orgwrote:
On Tue, 09 Jan 2007 04:00:10 +0000, Nelu wrote:
>J. Sommers <js******@sessrimnir.netwrote:
>>On Tue, 09 Jan 2007 03:31:55 +0000, Nelu wrote:

dm******@cox.net <dm******@cox.netwrote:
I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.

I realize that being a mathematician you won't care about some of the
advice I'll give you,

Didn't your mummy teach you to leave your prejudices at home? Or
perhaps you are so small minded that just can't?

It's not prejudice.

It is prejudice.
Ok. Although, combining my (limited) experience with the lack of checks in
the OP's code I don't think it should have come out that way. But that's
just my opinion, of course.
>I have friends that are mathematicians and none of them cares about
checking the return value of scanf, malloc, and any other checks, in
general.

How many friends do you have that are mathematicians? Ten? Twenty? How
does that compare with the thousands of mathematicians world over? Even
those ten or twenty systematically, and in every single occasion were to
ignore your advice that would not imply that all mathematicians would (but
it would probably imply that there is something systematically weird with
your advice).

I also have a number a software engineer friends who entertain astounding
superstitions, but that is no evidence that all software engineers do so.
You just can't generalize from such small samples and anecdotal evidence.
>They usually rely on the fact that those functions will not
fail for their needs. That's all.

Which might be a sensible thing to do for them. C purity and perfection
might be your goal, but other people will just want their little C code to
run well enough to solve their problem, even if, say, they use

int main()

rather than

int main(int argc, char **argv)

This is why I said what I said. I should have probably said 'you may not
care' instead of 'you won't care' and I specified 'some of the advice'
because it 'might be a sensible thing to do for them' to do things the
way they see fit. I did not indent to say anything that may be
considered prejudiced or hurtful in any way. I am really sorry if it
came out that way.
>I aplogize if I hurt anyone's feelings.

Those who have the guts to apologize in public tend to be a cut above the
rest. Kudos to you. Just remember to keep your prejudices in check. We all
have prejudices - otherwise we could not make sense of a world where
information is almost always incomplete.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Jan 9 '07 #16

P: n/a
On Tue, 09 Jan 2007 16:38:25 GMT, Ivar Rosquist <IR*******@irq.org>
wrote:
>It's not prejudice.

It is prejudice.
In general, programmers are more interested in programming than in
mathematics. In general, mathematicians are more interested in
mathematics than programming.

If that's prejudice, so be it.

--
Al Balmer
Sun City, AZ
Jan 9 '07 #17

P: n/a
Al Balmer said:
On Tue, 09 Jan 2007 16:38:25 GMT, Ivar Rosquist <IR*******@irq.org>
wrote:
>>It's not prejudice.

It is prejudice.

In general, programmers are more interested in programming than in
mathematics. In general, mathematicians are more interested in
mathematics than programming.
They're different states of the same element. Mathematics is frozen
programming. (Or, if you prefer, programming is defrosted mathematics.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 9 '07 #18

P: n/a
Nelu wrote:
J. Sommers <js******@sessrimnir.netwrote:
>On Tue, 09 Jan 2007 03:31:55 +0000, Nelu wrote:
>>dm******@cox.net <dm******@cox.netwrote:
I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.
I realize that being a mathematician you won't care about some of the
advice I'll give you,
Didn't your mummy teach you to leave your prejudices at home? Or
perhaps you are so small minded that just can't?

It's not prejudice. I have friends that are mathematicians and none of
them cares about checking the return value of scanf, malloc, and any
other checks, in general. They usually rely on the fact that those
functions will not fail for their needs.
That's all.
I aplogize if I hurt anyone's feelings.
*shrug* I know a graph theorist and he always listen to advice
regarding clean coding.

People from all walks of life ignore returns from malloc(), at al. Even
coders, which is evident from the constant stream of examples to this
very newsgroup.
Jan 9 '07 #19

P: n/a
Richard Heathfield wrote:
Al Balmer said:
>On Tue, 09 Jan 2007 16:38:25 GMT, Ivar Rosquist <IR*******@irq.org>
wrote:
>>>It's not prejudice.
It is prejudice.
In general, programmers are more interested in programming than in
mathematics. In general, mathematicians are more interested in
mathematics than programming.

They're different states of the same element. Mathematics is frozen
programming. (Or, if you prefer, programming is defrosted mathematics.)
So, if you leave your math alone for too long, does it become freezer burnt?
Jan 9 '07 #20

P: n/a
On Tue, 09 Jan 2007 17:10:25 +0000, Richard Heathfield wrote:
Ivar Rosquist said:

<snip>
>You just can't generalize from such small samples and anecdotal evidence.

Oh yes he can - as he just proved. :-)
Pray tell us: What exactly has been proved?
Jan 9 '07 #21

P: n/a
On Tue, 09 Jan 2007 18:28:24 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
>Al Balmer said:
>On Tue, 09 Jan 2007 16:38:25 GMT, Ivar Rosquist <IR*******@irq.org>
wrote:
>>>It's not prejudice.

It is prejudice.

In general, programmers are more interested in programming than in
mathematics. In general, mathematicians are more interested in
mathematics than programming.

They're different states of the same element. Mathematics is frozen
programming. (Or, if you prefer, programming is defrosted mathematics.)
Of course, and both are simply manifestations of the sex drive.

--
Al Balmer
Sun City, AZ
Jan 9 '07 #22

P: n/a
Ivar Rosquist said:
On Tue, 09 Jan 2007 17:10:25 +0000, Richard Heathfield wrote:
>Ivar Rosquist said:

<snip>
>>You just can't generalize from such small samples and anecdotal
evidence.

Oh yes he can - as he just proved. :-)

Pray tell us: What exactly has been proved?
He has proved that he can generalise from small samples and anecdotal
evidence. All humans can do this.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 9 '07 #23

P: n/a
On Tue, 09 Jan 2007 19:30:15 +0000, Richard Heathfield wrote:
Ivar Rosquist said:
>On Tue, 09 Jan 2007 17:10:25 +0000, Richard Heathfield wrote:
>>Ivar Rosquist said:

<snip>

You just can't generalize from such small samples and anecdotal
evidence.

Oh yes he can - as he just proved. :-)

Pray tell us: What exactly has been proved?

He has proved that he can generalise from small samples and anecdotal
evidence. All humans can do this.
Oh, I see :-)
Jan 9 '07 #24

P: n/a
On Tue, 09 Jan 2007 16:38:25 GMT, in comp.lang.c , Ivar Rosquist
<IR*******@irq.orgwrote:
>On Tue, 09 Jan 2007 04:00:10 +0000, Nelu wrote:
>J. Sommers <js******@sessrimnir.netwrote:
>>>
Didn't your mummy teach you to leave your prejudices at home? Or
perhaps you are so small minded that just can't?

It's not prejudice.

It is prejudice.
Y'know, you guys ought to grow up. I don't see any actual
mathematicians complaining about Nelu implying they're not
programmers.
> How many friends do you have that are mathematicians?
Who the hell cares? It was a casual comment, not a bloody indictment
of a class. If you want to start moaning, why not complain about
something sensible, like the overpriced rubbish that Apple sell.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jan 9 '07 #25

P: n/a
On Tue, 09 Jan 2007 23:29:41 +0000, Mark McIntyre wrote:
On Tue, 09 Jan 2007 16:38:25 GMT, in comp.lang.c , Ivar Rosquist
<IR*******@irq.orgwrote:
>>On Tue, 09 Jan 2007 04:00:10 +0000, Nelu wrote:
>>J. Sommers <js******@sessrimnir.netwrote:

How many friends do you have that are mathematicians?

Who the hell cares? It was a casual comment, not a bloody indictment
of a class.
The OP has already apologized - no harm done.
If you want to start moaning, why not complain about
something sensible, like the overpriced rubbish that Apple sell.
This is WAY off topic, but kind of amusing. If you think that
Apple sells rubbish (overpriced or not) here is an idea for you to
explore: Don't buy Apple products.
Jan 10 '07 #26

P: n/a
On 8 Jan 2007 20:11:46 -0800, "dm******@cox.net" <dm******@cox.net>
wrote:
snip 150 lines
>Thanks, Jack. Bear with me as I am relearning a lot of this. I got away
with not having to program for about a year and a half after I got my
degree so I'm probably due.
Your next lesson is to learn to quote only relevant material. You
don't need to quote a whole message just to say thanks.
Remove del for email
Jan 10 '07 #27

P: n/a
<dm******@cox.netwrote in message
news:11**********************@42g2000cwt.googlegro ups.com...
>I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.
Just some general advice for you.

The other posters have pointed out the errors.

a)You might want to obtain the classic K&R book by Messrs. Kernighan and
Ritchie.

b)You might want to upgrade to Microsoft Visual C++. I understand it is
about $110 these days (a far cry from when Visual Studio used to be $1,500).
It is more current and has good help, both online and built in.

c)I don't want to discourage you from programming in 'C', but maybe a
scripting language with more "protection" (built into Mathematica?) would
be appropriate. 'C' compiles very efficiently, but you also need to be
careful: pointer errors, array subscripting errors, and mismatched function
arguments are deadly ... these translate into triggering memory protection
faults on the machine, mismatched stack frames, etc.

d)In general in 'C', you want to give the compiler all the information it
needs to check definitions against invocations. This means that you use
header files correctly to do this. (If this doesn't make sense, please
write me at dt*@e3ft.com and make it past my SPAM reply/ack process and I'll
send you more information.)

Dave.
Jan 10 '07 #28

P: n/a

Ivar Rosquist wrote:
On Tue, 09 Jan 2007 04:00:10 +0000, Nelu wrote:
J. Sommers <js******@sessrimnir.netwrote:
On Tue, 09 Jan 2007 03:31:55 +0000, Nelu wrote:

I realize that being a mathematician you won't care about some of the
advice I'll give you,

Didn't your mummy teach you to leave your prejudices at home? Or
perhaps you are so small minded that just can't?
It's not prejudice.

It is prejudice.
I have friends that are mathematicians and none of them cares about
checking the return value of scanf, malloc, and any other checks, in
general.

How many friends do you have that are mathematicians? Ten? Twenty? How
does that compare with the thousands of mathematicians world over? Even
those ten or twenty systematically, and in every single occasion were to
ignore your advice that would not imply that all mathematicians would (but
it would probably imply that there is something systematically weird with
your advice).

I also have a number a software engineer friends who entertain astounding
superstitions, but that is no evidence that all software engineers do so.
You just can't generalize from such small samples and anecdotal evidence.
They usually rely on the fact that those functions will not
fail for their needs. That's all.

Which might be a sensible thing to do for them.
Which I took to be Nelu's point.

Jan 10 '07 #29

P: n/a
Richard Heathfield wrote:
Ivar Rosquist said:
>On Tue, 09 Jan 2007 17:10:25 +0000, Richard Heathfield wrote:
>>Ivar Rosquist said:

<snip>

You just can't generalize from such small samples and anecdotal
evidence.

Oh yes he can - as he just proved. :-)

Pray tell us: What exactly has been proved?

He has proved that he can generalise from small samples and anecdotal
evidence. All humans can do this.
"Everybody generalises from single examples. I know I do." Vlad Taltos,
probably in /Issola/ but I'm not sure and my Brust collection is at
home.

--
Chris "first on the Underground!" Dollin
"Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/

Jan 10 '07 #30

P: n/a
Clever Monkey <cl**************@hotmail.com.invalidwrote:
Richard Heathfield wrote:
Al Balmer said:
On Tue, 09 Jan 2007 16:38:25 GMT, Ivar Rosquist <IR*******@irq.org>
wrote:

It's not prejudice.
It is prejudice.
In general, programmers are more interested in programming than in
mathematics. In general, mathematicians are more interested in
mathematics than programming.
They're different states of the same element. Mathematics is frozen
programming. (Or, if you prefer, programming is defrosted mathematics.)
So, if you leave your math alone for too long, does it become freezer burnt?
No. It starts growing legs and walks out of the fridge, after having
eaten the cheese and leaving footprints in the butter.

(And then it starts infecting backdoored MS-Windows systems.)

Richard
Jan 10 '07 #31

P: n/a
Y'know, you guys ought to grow up. I don't see any actual
mathematicians complaining about Nelu implying they're not
programmers.
I don't see the big deal; I know I'm not a programmer and I don't claim
to be. I just thought being able to use C would help me with some of my
numerical calculations, which is most of my practice; I rarely do any
theoretical math.

Dave

Jan 10 '07 #32

P: n/a
In article <q6********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.netwrote:
>Y'know, you guys ought to grow up. I don't see any actual
mathematicians complaining about Nelu implying they're not
programmers.
According to my degree, I'm a Mathematician. Or a Scientist,
depending how you interpret the piece of paper. Either way,
I spend most of my time programming in some form or another
(and is configuring network equipment not a form of programming?)

Whatever it is you call me: of course I check the results of
malloc() and library routines. (Though admittedly, perhaps not
the result of -every- read or write when I'm coding up something
for my own use.)
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Jan 10 '07 #33

P: n/a
>>>>"WR" == Walter Roberson <ro******@ibd.nrc-cnrc.gc.cawrites:

WRAccording to my degree, I'm a Mathematician.

According to my degree(s), I'm a musician. I'm not convinced that
that has any relevance.

Charlton
--
Charlton Wilbur
cw*****@chromatico.net
Jan 10 '07 #34

P: n/a

"Walter Roberson":
According to my degree, I'm a Mathematician. Or a Scientist,
depending how you interpret the piece of paper. Either way,
I spend most of my time programming in some form or another
(and is configuring network equipment not a form of programming?)

Whatever it is you call me: of course I check the results of
malloc() and library routines. (Though admittedly, perhaps not
the result of -every- read or write when I'm coding up something
for my own use.)
I'm always surprised by the programming acumen of mathematicians. Some use
a language like C as their stock and trade, using calculation to inform
their practice. Others don't. The prof I had for enumerative combinatorics
was proud that he had only hand written instructions to a Turing machine as
a grad student decades ago. I had a bet with a colleague of his that I
couldn't get him to compile a program. I have yet to collect.

Dr. Griess at Michigan calculated the order of the Fischer-Griess group
without the help of a computer (=10^54). Go figure. LS
Jan 10 '07 #35

P: n/a
On Wed, 10 Jan 2007 01:01:06 GMT, in comp.lang.c , Ivar Rosquist
<IR*******@irq.orgwrote:
>If you think that
Apple sells rubbish (overpriced or not) here is an idea for you to
explore: Don't buy Apple products.
Thanks for the tip, once someone starts making Indesign and Quark
(that work) for some other platform. I'll have a choice.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jan 11 '07 #36

P: n/a
Mark McIntyre <ma**********@spamcop.netwrote:
On Wed, 10 Jan 2007 01:01:06 GMT, in comp.lang.c , Ivar Rosquist
<IR*******@irq.orgwrote:
If you think that
Apple sells rubbish (overpriced or not) here is an idea for you to
explore: Don't buy Apple products.

Thanks for the tip, once someone starts making Indesign and Quark
(that work) for some other platform.
Indesign for M$ Windows works. IME. (I spent most of this week using
it.)

Richard
Jan 11 '07 #37

P: n/a
On Thu, 11 Jan 2007 13:52:12 GMT, in comp.lang.c ,
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>Mark McIntyre <ma**********@spamcop.netwrote:
>On Wed, 10 Jan 2007 01:01:06 GMT, in comp.lang.c , Ivar Rosquist
<IR*******@irq.orgwrote:
>If you think that
Apple sells rubbish (overpriced or not) here is an idea for you to
explore: Don't buy Apple products.

Thanks for the tip, once someone starts making Indesign and Quark
(that work) for some other platform.

Indesign for M$ Windows works. IME. (I spent most of this week using
it.)
<OT>
They both work fine independently. However from experience I can tell
you that the PC and Mac versions are subtly incompatible in ways which
only become apparent when you send something back to someone who uses
the Mac version , and the entire book reflows. Trust me, this is not a
cheap discovery...
<ot>
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jan 11 '07 #38

This discussion thread is closed

Replies have been disabled for this discussion.