seg fault

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

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;
}
 
S

Sjouke Burry

Bill said:
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??

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;
}
 
B

Bill Cunningham

Sjouke said:
here you might mean avg??

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
 
L

Lew Pitcher

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;

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;
}
 
B

Bill Cunningham

Lew said:
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
 
J

Joe Pfeiffer

Bill Cunningham said:
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.
 
B

Bill Cunningham

Shao said:
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
 
B

Bill Cunningham

Joe said:
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
 
S

Shao Miller

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' 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.

Shao said:
...
"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;<-
average = accumulator / periods;
return average;
}


I'm a little confused here. accumulator+accumulator=values;
The i is being iterated. The accumulator is where the value is being stored
right?
 
S

Shao Miller

Shao said:
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+code&l=1
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.thefreedictionary.com/bill
[2] http://www.thefreedictionary.com/cunning
[3] http://www.thefreedictionary.com/Ham
[4] http://www.thefreedictionary.com/clear
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top