Velocity Reviews > newbie Q, segmentation fault

newbie Q, segmentation fault

Elinore
Guest
Posts: n/a

 03-03-2005
hi

I am practicing whole structure passing.
Array 'c' add two arrays 'a' and 'b' in 'sum' function..
Result is okay, but see segmentation fault (core dumped). Can anyone
help me with this? Thanks

#include <stdio.h>
#include <time.h>

#define N 10
#define Rand_MAX 5

struct par{
int a[N];
int b[N];
int c[N];
};
void sum(struct par *);

main()
{
int i;
struct par *inte;

srand((unsigned)time(NULL));

for(i=0;i<N;i++){
inte->a[i] = (int)rand() % Rand_MAX;
inte->b[i] = (int)rand() % Rand_MAX;
printf("a[%d]=%d\n",i,inte->a[i]);
printf("b[%d]=%d\n",i,inte->b[i]);
}

sum(inte);

for(i=0;i<N;i++)
printf("c[%d]=%d\n",i,inte->c[i]);
}

void sum(intel)
struct par *intel;
{
int i;
for(i=0;i<N;i++)
intel->c[i]=intel->a[i] + intel->b[i];

}

Eric Sosman
Guest
Posts: n/a

 03-03-2005

Elinore wrote:
> hi
>
> I am practicing whole structure passing.
> Array 'c' add two arrays 'a' and 'b' in 'sum' function..
> Result is okay, but see segmentation fault (core dumped). Can anyone
> help me with this? Thanks
>
>
> #include <stdio.h>
> #include <time.h>
>
> #define N 10
> #define Rand_MAX 5
>
> struct par{
> int a[N];
> int b[N];
> int c[N];
> };
> void sum(struct par *);
>
> main()
> {
> int i;
> struct par *inte;

`inte' is a variable that can point to `struct par'
objects. Fine. However, `inte' has not been given any
value yet, so (just like `i') its value is indeterminate;
it's a "garbage pointer."

> srand((unsigned)time(NULL));
>
> for(i=0;i<N;i++){
> inte->a[i] = (int)rand() % Rand_MAX;

.... and here you use the "garbage pointer" to reference
the non-existent `struct par' object that it doesn't
point to. All bets are off; anything at all can happen.
If you're lucky, your program will crash.

> [remainder snipped, and not examined for further trouble]

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

Martin Ambuhl
Guest
Posts: n/a

 03-03-2005
Elinore wrote:
> hi
>
> I am practicing whole structure passing.
> Array 'c' add two arrays 'a' and 'b' in 'sum' function..
> Result is okay, but see segmentation fault (core dumped). Can anyone
> help me with this? Thanks

This is the usual problem of a pointer never initialized to point to
anything.
Below are first a changelog, then a modified version of your code. The
original code is at EOM.

[changes]
4a5
> #include <stdlib.h>

17c18
< main()
---
> int main(void)

20c21
< struct par *inte;
---
> struct par inte;

25,28c26,29
< inte->a[i] = (int) rand() % Rand_MAX;
< inte->b[i] = (int) rand() % Rand_MAX;
< printf("a[%d]=%d\n", i, inte->a[i]);
< printf("b[%d]=%d\n", i, inte->b[i]);
---
> inte.a[i] = Rand_MAX * (rand() / (1. + RAND_MAX));
> inte.b[i] = Rand_MAX * (rand() / (1. + RAND_MAX));
> printf("a[%d]=%d\n", i, inte.a[i]);
> printf("b[%d]=%d\n", i, inte.b[i]);

31c32
< sum(inte);
---
> sum(&inte);

34c35,36
< printf("c[%d]=%d\n", i, inte->c[i]);
---
> printf("c[%d]=%d\n", i, inte.c[i]);
> return 0;

38,39c40
< void sum(intel)
< struct par *intel;
---
> void sum(struct par *intel)

[resulting code]
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define N 10
#define Rand_MAX 5

struct par
{
int a[N];
int b[N];
int c[N];
};
void sum(struct par *);

int main(void)
{
int i;
struct par inte;

srand((unsigned) time(NULL));

for (i = 0; i < N; i++) {
inte.a[i] = Rand_MAX * (rand() / (1. + RAND_MAX));
inte.b[i] = Rand_MAX * (rand() / (1. + RAND_MAX));
printf("a[%d]=%d\n", i, inte.a[i]);
printf("b[%d]=%d\n", i, inte.b[i]);
}

sum(&inte);

for (i = 0; i < N; i++)
printf("c[%d]=%d\n", i, inte.c[i]);
return 0;
}

void sum(struct par *intel)
{
int i;
for (i = 0; i < N; i++)
intel->c[i] = intel->a[i] + intel->b[i];

}

[original code]
>
> #include <stdio.h>
> #include <time.h>
>
> #define N 10
> #define Rand_MAX 5
>
> struct par{
> int a[N];
> int b[N];
> int c[N];
> };
> void sum(struct par *);
>
> main()
> {
> int i;
> struct par *inte;
>
> srand((unsigned)time(NULL));
>
> for(i=0;i<N;i++){
> inte->a[i] = (int)rand() % Rand_MAX;
> inte->b[i] = (int)rand() % Rand_MAX;
> printf("a[%d]=%d\n",i,inte->a[i]);
> printf("b[%d]=%d\n",i,inte->b[i]);
> }
>
> sum(inte);
>
> for(i=0;i<N;i++)
> printf("c[%d]=%d\n",i,inte->c[i]);
> }
>
>
> void sum(intel)
> struct par *intel;
> {
> int i;
> for(i=0;i<N;i++)
> intel->c[i]=intel->a[i] + intel->b[i];
>
> }
>

Jack
Guest
Posts: n/a

 03-04-2005
hi

it is simple and i was careless...
thankyou for comment and correction

CBFalconer
Guest
Posts: n/a

 03-04-2005
Jack wrote:
>
> it is simple and i was careless...
> thankyou for comment and correction

Another of these silly orphan and useless posts. What is simple,
what comment, what correction, etc. See sig. below. Some people
really do manage to use google without posting such nonsense.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the