Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > newbie Q, segmentation fault

Reply
Thread Tools

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];

}

 
Reply With Quote
 
 
 
 
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)

 
Reply With Quote
 
 
 
 
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];
>
> }
>

 
Reply With Quote
 
Jack
Guest
Posts: n/a
 
      03-04-2005
hi

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

 
Reply With Quote
 
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
"Reply" at the bottom of the article headers." - Keith Thompson


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Core Dump - Segmentation Fault -Newbie johnericaturnbull@yahoo.com Python 5 07-18-2008 09:57 PM
Newbie needing some help with segmentation fault Hendrik Maryns C Programming 20 12-07-2007 02:58 PM
Segmentation fault using Firefox 15.0.2 Keith Lee Firefox 3 04-29-2006 05:45 PM
Xerces XML Parser Segmentation fault with Java Pud XML 0 11-06-2003 05:07 PM
Intel Xeon + Linux + IBM sdk 1.3.1 - getting Segmentation fault Alex Hunsley Java 17 11-06-2003 12:12 AM



Advertisments