[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

seg fault

Bill Cunningham

6/4/2011 10:14:00 PM

I get a segmentation fault with this code. I'm not quite sure what's
wrong with it I've looked and looked. It might help if I knew what the
nature of a seg fault was.

#include <stdio.h>

double mean(double *avg, int num)
{
double sum, average;
int i;
sum = average = 0;
for (i = 0; i < num; ++num) {
sum = sum + avg[num];
average = sum / num;
}
return average;
}

int main()
{
double a[] = { 2.5, 3, 4.6 };
printf("%.2f\n", mean(a, 3));
return 0;
}


11 Answers

Sjouke Burry

6/4/2011 10:33:00 PM

0

Bill Cunningham wrote:
> I get a segmentation fault with this code. I'm not quite sure what's
> wrong with it I've looked and looked. It might help if I knew what the
> nature of a seg fault was.
>
> #include <stdio.h>
>
> double mean(double *avg, int num)
> {
> double sum, average;
> int i;
> sum = average = 0;
> for (i = 0; i < num; ++num) {
> sum = sum + avg[num];


here you might mean avg[i]??

avg[num] or avg[3] is illegal,only avg[0],avg[1],avg[2] exist.



> average = sum / num;
> }
> return average;
> }
>
> int main()
> {
> double a[] = { 2.5, 3, 4.6 };
> printf("%.2f\n", mean(a, 3));
> return 0;
> }
>
>

Bill Cunningham

6/4/2011 10:38:00 PM

0

Sjouke Burry wrote:

> here you might mean avg[i]??
>
> avg[num] or avg[3] is illegal,only avg[0],avg[1],avg[2] exist.

Ok I feel stupid now. Thanks for your sharp eye. I'm having to relearn
some of this all over again. I'd like to do C from sun up to sun down but I
get away sometimes.

Bill


Lew Pitcher

6/4/2011 10:43:00 PM

0

On June 4, 2011 18:14, in comp.lang.c, nospam@nspam.invalid wrote:

> I get a segmentation fault with this code. I'm not quite sure what's
> wrong with it I've looked and looked.

Bill, you didn't look hard enough.

> It might help if I knew what the nature of a seg fault was.

A "segfault" or "segmentation violation" is a form of addressing exception,
typically encountered when your code tries to access memory outside the
bounds of the program.

> #include <stdio.h>
>
> double mean(double *avg, int num)
> {
> double sum, average;
> int i;
> sum = average = 0;
> for (i = 0; i < num; ++num) {
> sum = sum + avg[num];

This is just plain wrong.

1) why set and test i, if you don't actually use it
2) num is initially a count of the number of entries in your avg table
why are you incrementing it?
3) why are you accessing avg[] using num as a subscript? It won't be
right, ever.

You apparently meant to code
for (i = 0; i < num; ++i) {
sum = sum + avg[i];

As to your segfault, think of this:
Given that you call mean() with num set to 3, then
what does avg[num] reference? It doesn't reference part of the
already-established array, as that array extends from avg[0] to avg[2].
What are you referencing when you
++num
and then access avg[num]
?

> average = sum / num;
> }
> return average;
> }
>
> int main()
> {
> double a[] = { 2.5, 3, 4.6 };
> printf("%.2f\n", mean(a, 3));
> return 0;
> }
>
>

--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfr... | Just Linux: http://jus...
---------- Slackware - Because I know what I'm doing. ------


Bill Cunningham

6/4/2011 10:52:00 PM

0

Lew Pitcher wrote:

> A "segfault" or "segmentation violation" is a form of addressing
> exception, typically encountered when your code tries to access
> memory outside the bounds of the program.

Ok that went right over my head Lew, sorry. Does thi have anything to do
with the actual source code? Is this error from the compiler or OS? On some
level from the OS anyway.

Bill


Joe Pfeiffer

6/4/2011 11:00:00 PM

0

"Bill Cunningham" <nospam@nspam.invalid> writes:

> I get a segmentation fault with this code. I'm not quite sure what's
> wrong with it I've looked and looked. It might help if I knew what the
> nature of a seg fault was.
>
> #include <stdio.h>
>
> double mean(double *avg, int num)
> {
> double sum, average;
> int i;
> sum = average = 0;
> for (i = 0; i < num; ++num) {
> sum = sum + avg[num];
> average = sum / num;
> }
> return average;
> }
>
> int main()
> {
> double a[] = { 2.5, 3, 4.6 };
> printf("%.2f\n", mean(a, 3));
> return 0;
> }

A segmentation fault is an attempt to access memory you don't have
access rights to. When a program segfaults, there is a near certainty
that there is either a bad pointer, or an array access out of range.

Your for-loop sets i to 0, and then checks to make sure it's less than
num before continuing. It then uses num to index the array, and
increments num. This is probably not what you want to be doing.
--
"Erwin, do you know what happened to the cat?" -- Mrs. Shroedinger

Bill Cunningham

6/4/2011 11:09:00 PM

0

Shao Miller wrote:

> Given that your code example resembles the following previously-posted
> code (which you've previously read and previously responded to), but
> given that you've got errors, _please_ explain why you did not refer
> to the previously-posted code and examine where the differences are.

[...] example

The post has already left my news reader. I looked for "error code" post
and there's only a couple of posts left in the thread. I hope I'm making
myself clear.

Bill


Bill Cunningham

6/4/2011 11:12:00 PM

0

Joe Pfeiffer wrote:

> A segmentation fault is an attempt to access memory you don't have
> access rights to. When a program segfaults, there is a near certainty
> that there is either a bad pointer, or an array access out of range.

Oh I understand. I see.

> Your for-loop sets i to 0, and then checks to make sure it's less than
> num before continuing. It then uses num to index the array, and
> increments num. This is probably not what you want to be doing.

I fixed the thing and now it works great. What I coded was not what I
meant.

Bill


Shao Miller

6/4/2011 11:46:00 PM

0

On 6/4/2011 5:14 PM, Bill Cunningham wrote:
> I get a segmentation fault with this code. I'm not quite sure what's
> wrong with it I've looked and looked. It might help if I knew what the
> nature of a seg fault was.
>
> #include<stdio.h>
>
> double mean(double *avg, int num)
> {
> double sum, average;
> int i;
> sum = average = 0;
> for (i = 0; i< num; ++num) {

I think you mean '++i' instead of '++num'.

> sum = sum + avg[num];

I think you mean 'avg[i]' instead of 'avg[num]'.

> average = sum / num;

I think you can take the line just above outside of your 'for' loop's
curly braces.

> }
> return average;
> }
>
> int main()

Please use:

int main(void)

if you don't care about having parameters for the 'main' function.

> {
> double a[] = { 2.5, 3, 4.6 };
> printf("%.2f\n", mean(a, 3));
> return 0;
> }
>

Given that your code example resembles the following previously-posted
code (which you've previously read and previously responded to), but
given that you've got errors, _please_ explain why you did not refer to
the previously-posted code and examine where the differences are.

On 5/28/2011 4:12 PM, Bill Cunningham wrote:
> Shao Miller wrote:
>> ...
>> "A cycle" might also be called "a loop".
>>
>> double mean(double * values, int periods) {
>> double accumulator;
>> int i;
>> double average;
>>
>> accumulator = 0;
>> for (i = 0; i< periods; ++i)
>> accumulator += values[i];<-
>> average = accumulator / periods;
>> return average;
>> }
>
> I'm a little confused here. accumulator+accumulator=values[i];
> The i is being iterated. The accumulator is where the value is being stored
> right?

Shao Miller

6/5/2011 12:29:00 AM

0

On 6/4/2011 6:08 PM, Bill Cunningham wrote:
> Shao Miller wrote:
>
>> Given that your code example resembles the following previously-posted
>> code (which you've previously read and previously responded to), but
>> given that you've got errors, _please_ explain why you did not refer
>> to the previously-posted code and examine where the differences are.
>
> [...] example
>
> The post has already left my news reader. I looked for "error code" post
> and there's only a couple of posts left in the thread.

Fortunately, Sjouke Burry, Lew Pitcher, Joe Pfeiffer, myself and others
seem to provid you with assistance, despite your news-reading
limitation. If you'd please try this link and keep the method in mind
for next time, I'd appreciate it:

http://lmgtfy.com/?q=bill+cunningham+error+in+co...

> I hope I'm making myself clear.

Please see below.

bill[1]:
....
3. The entertainment offered by a theater.
....

cunning[2]:
adj.
1. Marked by or given to artful subtlety and deceptiveness.
2. Executed with or exhibiting ingenuity.
....
n.
1. Skill in deception; guile.
2. Skill or adeptness in execution or performance; dexterity.

ham[3]:
....
6. A performer who overacts or exaggerates.
....

clear[4]:
....
3. Easily seen through; transparent: clear water.
....

[1] http://www.thefreedictionar...
[2] http://www.thefreedictionary.c...
[3] http://www.thefreedictiona...
[4] http://www.thefreedictionary...

Bill Cunningham

6/5/2011 2:57:00 AM

0

Shao Miller wrote:

> Fortunately, Sjouke Burry, Lew Pitcher, Joe Pfeiffer, myself and
> others seem to provid you with assistance, despite your news-reading
> limitation. If you'd please try this link and keep the method in mind
> for next time, I'd appreciate it:
>
> http://lmgtfy.com/?q=bill+cunningham+error+in+co...

Okay.

Bill