Segmentation fault

L

LL

#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
}
}
 
A

Alf P. Steinbach

* 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' 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
 
I

Ivan

#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);
 
Z

zr

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

LL

#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
}
}
 
L

LL

#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
}
 
L

LL

#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
}
}
 
J

Johannes Bauer

Alf said:
* 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
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top