I'm learning C, but a friend of mine asked for help with this code. I have debugged it with C::B and actually it crashes when getting out of the switch and starting to print out the menu again, after selection the first option (1. Polinomio)
I 'watched' the polinomio pointer (float polinomio[n]) and it seems to be working, so i don't know where the segmentation fault is coming from. What do i ignore?

#include <iostream>

using namespace std;

int main() {
    int opcion;
    do
    {
         cout << "1. Polinomio \n\n";
         cout << "2. Algoritmo de Horner \n\n";
         cout << "3. Salir \n\n\n";

         cout << "Opcion: ";
         cin >> opcion;
         cout << "\n\n";

         switch(opcion)
         {
             case 1:
                  cout << "Opcion polinomio: \n\n";
                  cout << "Grado de polinomio (n): ";
                  unsigned n;
                  cin >> n;
                  cout << "Introduzca los coeficientes de p(x): \n";
                  float polinomio[n];
                  for (unsigned i=0; i<n; i++) {
                      cout << "a(" << i << ") = ";
                      cin >> polinomio[i];
                      cout << "i = " << i << ", polinomio["<<i<<"] = " << polinomio[i] << endl;
                  }
                  break;
             case 2:
                  cout << "Opcion Algoritmo de Horner \n\n";
                  cout << "ENTRADA: \n";
                  cout << "Orden de la derivada (m>=0): ";
                  int m;
                  cin >> m;
                  cout << "Punto donde evaluar la derivada (z): ";
                  float z;
                  cin >> z;
                  cout << "\n\n\nSALIDA: \n";
                  cout << "Ultimo cociente (q-n-m-1(x)) = ";
                  // CODIGO
                  cout << "\nValor de la derivada (p(m)(z)) = ";
                  // CODIGO
                  break;
             case 3:
                  cout << "Bye bye\n";
                  return 0;
             default:
                  cout << "Opcion incorrecta";
                  break;
         }
    } while (1);
    return 0;
}

Recommended Answers

All 3 Replies

I get an odd warning:

switch ( opcion ) // warning: unreachable code at beginning of switch statement
{
case 1:

I think this has something to do with using the language extension VLA:

float polinomio[n];

I don't know exactly what the issue here is -- gcc is trying to clue me in, but I'm slow.

What I do know is that if I change the scope of polinomio such that it only exists in case 1 , the SIGSEGV does not occur:

case 1:
         [B]{[/B]
            cout << "Opcion polinomio: \n\n";
            cout << "Grado de polinomio (n): ";
            unsigned n;
            cin >> n;
            cout << "Introduzca los coeficientes de p(x): \n";
            float polinomio[n];
            for ( unsigned i=0; i<n; i++ )
            {
               cout << "a(" << i << ") = ";
               cin >> polinomio[i];
               cout << "i = " << i << ", polinomio["<<i<<"] = " << polinomio[i] << endl;
            }
         [B]}[/B]
         break;

So I'm tending to think that the segfault is related to the scoping of the VLA.

[edit]Hm. Like a goto, the switch statement allows jumping outside of the scope of a VLA?

http://groups.google.com/group/comp.lang.c/msg/761ae75bd2c1f1ff

Except for VLA types. They may be and probably are _allocated_ (only) when the declaration is 'executed', and jumping 'past' the declaration (formally, into the scope) is a Constraint Violation.

I get an odd warning:
I think this has something to do with using the language extension VLA: float polinomio[n]; I don't know exactly what the issue here is -- gcc is trying to clue me in, but I'm slow.
What I do know is that if I change the scope of polinomio such that it only exists in case 1 , the SIGSEGV does not occur. So I'm tending to think that the segfault is related to the scoping of the VLA.

[edit]Hm. Like a goto, the switch statement allows jumping outside of the scope of a VLA?

http://groups.google.com/group/comp.lang.c/msg/761ae75bd2c1f1ff

I got that warning too but, i don't see any unreached code. I mean if I follow the code thinking step by step i don't see the leak.
I would see the logic if it was the code showed in that google link, but in this code there's nothing right after the switch (opcion) { but the cases.

So yes, this is the same thing:

Except for VLA types. They may be and probably are _allocated_ (only)
when the declaration is 'executed', and jumping 'past' the declaration
(formally, into the scope) is a Constraint Violation.

I see it weird, that it's not doable this way but whatever.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.