Segmentation fault

Discussion in 'C++' started by LL, Mar 2, 2009.

  1. LL

    LL Guest

    #include <stdio.h>

    float* dup(float* farr[], int n) {
    float* fdarr;
    for (int i=0; i<n; i++) {
    fdarr=*farr;
    }
    return fdarr;
    }

    main() {
    float *f1=new float(1.0);
    float *f2=new float(2.0);
    float *f3=new float(3.0);
    float* farr_s[]={f1,f2,f3};

    float* fdarr_s;
    fdarr_s=dup(farr_s,3);

    for (int i; i<3; i++) {
    printf("%f ", fdarr_s); // Segmentation fault
    }
    }
    LL, Mar 2, 2009
    #1
    1. Advertising

  2. * LL:
    > #include <stdio.h>
    >
    > float* dup(float* farr[], int n) {
    > float* fdarr;
    > for (int i=0; i<n; i++) {
    > fdarr=*farr;
    > }
    > return fdarr;
    > }


    Indicates basic lack of understanding of pointers.

    Just Say No to raw pointers.

    At least at this point in your learning curve. :)


    > main() {


    'main' must have result type 'int', and C++ does not in general support the old
    implicit 'int' of C.


    > float *f1=new float(1.0);
    > float *f2=new float(2.0);
    > float *f3=new float(3.0);
    > float* farr_s[]={f1,f2,f3};
    >
    > float* fdarr_s;
    > fdarr_s=dup(farr_s,3);
    >
    > for (int i; i<3; i++) {
    > printf("%f ", fdarr_s); // Segmentation fault
    > }
    > }


    Solution: use std::vector for the arrays, use C++ iostreams (cout, <<) for i/o.


    Cheers & hth.,

    - Alf

    --
    Due to hosting requirements I need visits to [http://alfps.izfree.com/].
    No ads, and there is some C++ stuff! :) Just going there is good. Linking
    to it is even better! Thanks in advance!
    Alf P. Steinbach, Mar 2, 2009
    #2
    1. Advertising

  3. LL

    Ivan Guest

    On 3ÔÂ2ÈÕ, ÏÂÎç3ʱ01·Ö, LL <> wrote:
    > #include <stdio.h>
    >
    > float* dup(float* farr[], int n) {
    > float* fdarr;
    > for (int i=0; i<n; i++) {
    > fdarr=*farr;
    > }
    > return fdarr;
    >
    > }
    >
    > main() {
    > float *f1=new float(1.0);
    > float *f2=new float(2.0);
    > float *f3=new float(3.0);
    > float* farr_s[]={f1,f2,f3};
    >
    > float* fdarr_s;
    > fdarr_s=dup(farr_s,3);
    >
    > for (int i; i<3; i++) {
    > printf("%f ", fdarr_s); // Segmentation fault
    > }
    >
    > }
    >
    >


    Do you know the type of fdarr_s? its float *.
    So the printf statement should be:
    printf("%f ", *fdarr_s);
    or
    printf("%p ", fdarr_s);
    Ivan, Mar 2, 2009
    #3
  4. LL

    zr Guest

    On Mar 2, 9:01 am, LL <> wrote:
    > #include <stdio.h>
    >
    > float* dup(float* farr[], int n) {
    >   float* fdarr;
    >   for (int i=0; i<n; i++) {
    >     fdarr=*farr;
    >   }
    >   return fdarr;
    >
    > }
    >
    > main() {
    >   float *f1=new float(1.0);
    >   float *f2=new float(2.0);
    >   float *f3=new float(3.0);
    >   float* farr_s[]={f1,f2,f3};
    >
    >   float* fdarr_s;
    >   fdarr_s=dup(farr_s,3);
    >
    >   for (int i; i<3; i++) {
    >     printf("%f ", fdarr_s); // Segmentation fault
    >   }
    >
    > }


    replace:
    float* fdarr;
    with:
    float* fdarr = new float[n];
    zr, Mar 2, 2009
    #4
  5. LL

    LL Guest

    On Mon, 02 Mar 2009 00:36:05 -0800, zr wrote:

    > On Mar 2, 9:01 am, LL <> wrote:
    >> #include <stdio.h>
    >>
    >> float* dup(float* farr[], int n) {
    >>   float* fdarr;
    >>   for (int i=0; i<n; i++) {
    >>     fdarr=*farr;
    >>   }
    >>   return fdarr;
    >>
    >> }
    >>
    >> main() {
    >>   float *f1=new float(1.0);
    >>   float *f2=new float(2.0);
    >>   float *f3=new float(3.0);
    >>   float* farr_s[]={f1,f2,f3};
    >>
    >>   float* fdarr_s;
    >>   fdarr_s=dup(farr_s,3);
    >>
    >>   for (int i; i<3; i++) {
    >>     printf("%f ", fdarr_s); // Segmentation fault
    >>   }
    >>
    >> }

    >
    > replace:
    > float* fdarr;
    > with:
    > float* fdarr = new float[n];


    Still gives segmentation error. I made three changes.

    #include <stdio.h>

    float* dup(float* farr[], int n) {
    float* fdarr=new float[n]; // Change 1
    for (int i=0; i<n; i++) {
    fdarr=*(farr); // Change 2
    }
    return fdarr;
    }

    main() {
    float *f1=new float(1.0);
    float *f2=new float(2.0);
    float *f3=new float(3.0);
    float* farr_s[]={f1,f2,f3};

    float* fdarr_s=new float[3]; // Change 3
    fdarr_s=dup(farr_s,3);

    for (int i; i<3; i++) {
    printf("%f ", fdarr_s); // should be 1.0 2.0 3.0
    }
    }
    LL, Mar 2, 2009
    #5
  6. LL

    LL Guest

    On Mon, 02 Mar 2009 07:01:52 +0000, LL wrote:

    > #include <stdio.h>
    >
    > float* dup(float* farr[], int n) {
    > float* fdarr;
    > for (int i=0; i<n; i++) {
    > fdarr=*farr;
    > }
    > return fdarr;
    > }
    >
    > main() {
    > float *f1=new float(1.0);
    > float *f2=new float(2.0);
    > float *f3=new float(3.0);
    > float* farr_s[]={f1,f2,f3};
    >
    > float* fdarr_s;
    > fdarr_s=dup(farr_s,3);
    >
    > for (int i; i<3; i++) {
    > printf("%f ", fdarr_s); // Segmentation fault
    > }
    > }

    The following code works. Now I need to do the same via a function.

    #include <stdio.h>

    main() {
    float* f1=new float(1.0);
    float* f2=new float(2.0);
    float* f3=new float(3.0);
    float* lf[]={f1,f2,f3};

    printf("%f ", *(lf[1])); // 2.0
    }
    LL, Mar 2, 2009
    #6
  7. LL

    LL Guest

    On Mon, 02 Mar 2009 07:01:52 +0000, LL wrote:

    > #include <stdio.h>
    >
    > float* dup(float* farr[], int n) {
    > float* fdarr;
    > for (int i=0; i<n; i++) {
    > fdarr=*farr;
    > }
    > return fdarr;
    > }
    >
    > main() {
    > float *f1=new float(1.0);
    > float *f2=new float(2.0);
    > float *f3=new float(3.0);
    > float* farr_s[]={f1,f2,f3};
    >
    > float* fdarr_s;
    > fdarr_s=dup(farr_s,3);
    >
    > for (int i; i<3; i++) {
    > printf("%f ", fdarr_s); // Segmentation fault
    > }
    > }

    I fixed it.

    #include <stdio.h>

    float* dup(float* farr[], int n) {
    float* fdarr=new float[n];
    for (int i=0; i<n; i++) {
    fdarr=*(farr);
    }
    return fdarr;
    }

    main() {
    float *f1=new float(1.0);
    float *f2=new float(2.0);
    float *f3=new float(3.0);
    float* farr_s[]={f1,f2,f3};

    float* fdarr_s=dup(farr_s,3);

    for (int i=0; i<3; i++) {
    printf("%f ", fdarr_s); // 1.0 2.0 3.0
    }
    }
    LL, Mar 2, 2009
    #7
  8. Alf P. Steinbach schrieb:
    > * LL:
    >> #include <stdio.h>
    >>
    >> float* dup(float* farr[], int n) {
    >> float* fdarr;
    >> for (int i=0; i<n; i++) {
    >> fdarr=*farr;
    >> }
    >> return fdarr;
    >> }

    >
    > Indicates basic lack of understanding of pointers.
    >
    > Just Say No to raw pointers.


    Good advice :)

    However, I find it curious how competent people (like yourself) tend to
    answer such threads in which the OP does neither care to tell what the
    problem seems to be ("This code produces a segfault, but it shouldn't")
    nor what his name is. Just posting code with "Segfault" as a subject is
    not just rude, it borders on insolence.

    Still, it made me wonder what a response might be with "Segfault" as a
    subject and only

    int main() { return *((int*)NULL); }

    as the body :)

    Kind regards,
    Johannes

    --
    "Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,
    verlästerung von Gott, Bibel und mir und bewusster Blasphemie."
    -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik
    <48d8bf1d$0$7510$>
    Johannes Bauer, Mar 2, 2009
    #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. Alex Hunsley
    Replies:
    17
    Views:
    865
  2. Pud
    Replies:
    0
    Views:
    574
  3. Replies:
    0
    Views:
    527
  4. Ivan Vecerina
    Replies:
    0
    Views:
    483
    Ivan Vecerina
    Jun 29, 2003
  5. Vasileios Zografos

    Re: segmentation fault exception handling

    Vasileios Zografos, Jun 30, 2003, in forum: C++
    Replies:
    5
    Views:
    15,605
    Pete Becker
    Jul 1, 2003
Loading...

Share This Page