473,378 Members | 1,434 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Segfault, please help!

BT
Ok, for a school assignment we have to use a pointer for an array of
ints, intstead of the usual X[i] 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

Mar 29 '06 #1
7 1456
BT wrote:
Ok, for a school assignment we have to use a pointer for an array of
ints, intstead of the usual X[i] 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

Mar 29 '06 #2
"BT" <af****@hotmail.com> writes:
Ok, for a school assignment we have to use a pointer for an array of
ints, intstead of the usual X[i] 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.
for(i=0; i< size; i++) {
*d = rand() % 99 + 1;
d++;
}
}


--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 29 '06 #3
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?

Mar 29 '06 #4
BT
Ok, I think I figured it out, i'm going to need to use malloc(), right?
Thanks for you help again

Mar 29 '06 #5
"BT" <af****@hotmail.com> writes:
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.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 29 '06 #6
BT wrote:

<snip>

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!
--
==============
Not a pedant
==============
Mar 29 '06 #7
"BT" <af****@hotmail.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
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
Mar 29 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Phil Powell | last post by:
Consider these two classes. Class Accepter in placement_classes.inc.php works as a form validation object, and it works like a charm: PHP: // placement_classes.inc.php - THIS ONE WORKS! ...
12
by: Nathaniel Echols | last post by:
I've written a function in C to perform protein sequence alignment. This works fine in a standalone C program. I've added the necessary packaging to use it in Python; it returns three strings and...
10
by: Arthur J. O'Dwyer | last post by:
I'm seeing a bug at the moment that I can't track down. It's part of a moderately large program, but here is a small program that exhibits the bug on gcc. (The program code follows at the bottom...
4
by: Jim Strathmeyer | last post by:
Under what circumstances would closing a istream object (such as 'in.close()') SEGFAULT?
3
by: Paul | last post by:
Situation: Running a MPI program that segfaults. Cannot open program up in debugger due to nature of MPI system. Using g++ 3.2.3. Goal: Find out which line it segfaults on; ideally which...
9
by: Bin Lu | last post by:
I keep getting this malloc problem when my program tries to allocate memory for some pointer. The statement is like: rsv_cache = (rsvcache *) malloc (sizeof(rsvcache)); It goes through the...
7
by: Parahat Melayev | last post by:
I am programming simple TCP server. Some packets causes server to get segmentation fault. Is it possible to handle segmentation fault and log that packet and make server to continue running? If...
162
by: Richard Heathfield | last post by:
I found something interesting on the Web today, purely by chance. It would be funny if it weren't so sad. Or sad if it weren't so funny. I'm not sure which. ...
49
by: comp.lang.php | last post by:
/** * Class for grayscaling image * * @author Phil Powell * @version 1.2.1 * @package IMAGE_CATALOG::IMAGE */ class ImageGrayscaleGenerator extends ImageResizeComponents { /
1
by: dikanst | last post by:
I'm getting a segmentation fault in one of my functions, which basically adds an employee structure to an array of employee pointers. Here's a condensed version of my C program: typedef struct {...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.