Segfault, please help!

B

BT

Ok, for a school assignment we have to use a pointer for an array of
ints, intstead of the usual X way, it compiles fine but when i run
it I am getting a seg fault that i can't figure out how to fix. It
occurs at this line:

*d = rand() % 99 + 1

Here is the code for the first part of the program, the line that
causes the seg fault is

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAXSIZE 11

int main()
{
int DATASIZE = MAXSIZE;
int *narray;

void getdata(int *d, int size);
int largest(int *d, int size);

getdata(narray, DATASIZE);

exit(0);
}

void getdata(int *d, int size)
{
int i;

srand(time(0));
for(i=0; i< size; i++) {
*d = rand() % 99 + 1;
d++;
}
}



Thanks in advance
 
E

Eric

BT said:
Ok, for a school assignment we have to use a pointer for an array of
ints, intstead of the usual X way, it compiles fine but when i run
it I am getting a seg fault that i can't figure out how to fix. It
occurs at this line:

*d = rand() % 99 + 1

Here is the code for the first part of the program, the line that
causes the seg fault is

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAXSIZE 11

int main()
{
int DATASIZE = MAXSIZE;
int *narray;

void getdata(int *d, int size);
int largest(int *d, int size);

getdata(narray, DATASIZE);

exit(0);
}

void getdata(int *d, int size)
{
int i;

srand(time(0));
for(i=0; i< size; i++) {
*d = rand() % 99 + 1;
d++;
}
}



Thanks in advance


Well, i dont want to tell you the whole story because its a school
assignment, but, where did you allocate space for the array called narray?
Eric
 
K

Keith Thompson

BT said:
Ok, for a school assignment we have to use a pointer for an array of
ints, intstead of the usual X way, it compiles fine but when i run
it I am getting a seg fault that i can't figure out how to fix. It
occurs at this line:

*d = rand() % 99 + 1

Here is the code for the first part of the program, the line that
causes the seg fault is

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAXSIZE 11

int main()


Ok, but "int main(void)" is better.
{
int DATASIZE = MAXSIZE;
int *narray;

void getdata(int *d, int size);
int largest(int *d, int size);

Function declarations inside another function are usually considered
poor style. (Nested function *definitions*, of course, are illegal.)
Either move the declarations outside your main function, or re-order
your function definitions so everything is visible where it needs to
be without separate declarations (this doesn't work if you have
recursive calls), or, if your program becomes more complex, put the
function declarations in a header file.
getdata(narray, DATASIZE);

narray is a pointer variable. It needs to point to something. What
is it pointing to here? You want an array of MAXSIZE ints; where do
you allocate the memory for that array?
exit(0);
}

void getdata(int *d, int size)
{
int i;

srand(time(0));

I prefer srand(time(NULL)); it makes it more explicit that the
argument is a pointer.
 
B

BT

Yes I understand about where to put the functions, my professor insists
on us doing it his way, declaring them in main(). Here is what he says
about this: "function prototypes are like data type declarations and
hence, should be declared in the module calling the function". He's
kind of weird about a lot of things.
But about my actual problem, I think i see what you two are getting at.
I need to do something in main after i declare int *narray, so that
enough space in memory is allocated. We have done a little memory
allocation in assembly, but I can't figure out how to do it here. I
tried declaring an int x and then having narray point to it before
getdata, but that didn't work, am i missing something obvious here?
 
B

BT

Ok, I think I figured it out, i'm going to need to use malloc(), right?
Thanks for you help again
 
K

Keith Thompson

BT said:
Ok, I think I figured it out, i'm going to need to use malloc(), right?
Thanks for you help again

If you had been following this newsgroup for any length of time (which
is generally a good thing to do before posting), you would have seen
dozens of references to <http://cfaj.freeshell.org/google/>.

Read it now.
 
P

pemo

BT wrote:

int main()
{
int DATASIZE = MAXSIZE;
int *narray;

void getdata(int *d, int size);
int largest(int *d, int size);

getdata(narray, DATASIZE);

exit(0);
}

<snip>

When I first looked at the code above, I initially 'mentally parsed' it as:

'Wow, he's declaring a /variable/ as a /parameter/ in a function call!'.

*But*, I then saw that this was a function prototype /living/ in non
file-scope, i.e., not placed outside of any function definitions, but within
one.

It's quite unusal to see this, and I never do such a thing - but I think
that's more through habit, than reason, and maybe there's quite some sense
behind using this to restrict scope - *if* the scope of the prototype *is*
restricted to the block/function in which it is declared [which the std
seems to suggest is the case?], and, given that we don't yet have nested
functions in std c, it could be quite useful.

So, irrespective of what the current std says: time to hit the compiler and
see what it/they think! Here's some code then...

#include <stdio.h>

int main(void)
{
void func2(void);

{
void func1(void);

func1();
}

func2();

// See below - problems here?: not happy about func1().
//
func1();

return 0;;
}


void func1(void)
{
// See below - problems here?: not happy about func2().
//
func2();

return;
}


void func2(void)
{
return;
}

---

So, with a few different compilers ...


gcc [v4.0.2]:

[Warning] implicit declaration of func-1/2 (2 from func1(), 1 from main)

[Error] incompatible declaration of func-1/2 (2 from func1(), 1 from
main)

---

bcc 5 [v5.5] [Borland CBuilder]:

[Error] type mismatch of func-1/2

---

lcc [v3.8]

Warnings only

---

MSVC6 and MSVC7:

Compiles ok.


So, the compilers /differ/ rather a lot in their opinions about this!

What do others think? Should there be warnings/errors? If 'yes' to either,
should 'style' [as this is often thought of as being 'bad style'] be
re-thought - seems to me that it could be a reasonable habit to adopt.

P.S. Would be interesting to hear what other compilers make of it too!
 
W

William J. Leary Jr.

BT said:
Yes I understand about where to put the functions, my professor insists
on us doing it his way, declaring them in main(). Here is what he says
about this: "function prototypes are like data type declarations and
hence, should be declared in the module calling the function". He's
kind of weird about a lot of things.

The quote says "...in the module..."

Putting them above "int main()" would still have them in the module, but would
get them out of the function.

- Bill
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top