Segfault, please help!

Discussion in 'C Programming' started by BT, Mar 29, 2006.

  1. BT

    BT Guest

    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
    BT, Mar 29, 2006
    #1
    1. Advertising

  2. BT

    Eric Guest

    BT wrote:

    > 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
    Eric, Mar 29, 2006
    #2
    1. Advertising

  3. "BT" <> writes:
    > 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.

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


    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Mar 29, 2006
    #3
  4. BT

    BT Guest

    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?
    BT, Mar 29, 2006
    #4
  5. BT

    BT Guest

    Ok, I think I figured it out, i'm going to need to use malloc(), right?
    Thanks for you help again
    BT, Mar 29, 2006
    #5
  6. "BT" <> 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) <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.
    Keith Thompson, Mar 29, 2006
    #6
  7. BT

    pemo Guest

    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
    ==============
    pemo, Mar 29, 2006
    #7
  8. "BT" <> wrote in message
    news:...
    > 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
    William J. Leary Jr., Mar 29, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    488
    Chris Uppal
    May 5, 2005
  2. KK
    Replies:
    2
    Views:
    510
    Big Brian
    Oct 14, 2003
  3. MuZZy
    Replies:
    7
    Views:
    1,715
    Mike Hewson
    Jan 7, 2005
  4. SkyBlue

    Simple code leads to segfault.. help

    SkyBlue, Sep 16, 2006, in forum: C Programming
    Replies:
    41
    Views:
    749
    Sjouke Burry
    Sep 17, 2006
  5. Andrey Vul
    Replies:
    8
    Views:
    669
    Richard Bos
    Jul 30, 2010
Loading...

Share This Page