Note that there are some explanatory texts on larger screens.

plurals
  1. POC: error using struct with variable arrays
    text
    copied!<p>I have a struct like this:</p> <pre><code>typedef struct tgPoligono { int numero_de_lados; CvPoint** cantos; float* lados; double* angulos; } *tgPoligono; </code></pre> <p>In a function, I'm using it like this:</p> <pre><code>tgPoligono criar_tgPoligono(CvSeq* poligono){ int i; tgPoligono tg_poligono = (tgPoligono) malloc(sizeof(tgPoligono)); tg_poligono-&gt;numero_de_lados = poligono-&gt;total; tg_poligono-&gt;cantos = (CvPoint**) malloc(tg_poligono-&gt;numero_de_lados * sizeof(CvPoint*)); for(i= 0; i &lt; tg_poligono-&gt;numero_de_lados; i++){ tg_poligono-&gt;cantos[i] = (CvPoint*)cvGetSeqElem(poligono, i); } tg_poligono-&gt;lados = (float*) malloc(tg_poligono-&gt;numero_de_lados * sizeof(float)); tg_poligono-&gt;angulos = (double*) malloc(tg_poligono-&gt;numero_de_lados * sizeof(double)); double* angulos = (double*) malloc(tg_poligono-&gt;numero_de_lados * sizeof(double)); for(i = 0; i &lt; tg_poligono-&gt;numero_de_lados; i++){ CvPoint* pt_0 = tg_poligono-&gt;cantos[((i == 0)?(tg_poligono-&gt;numero_de_lados - 1):(i - 1))]; //Canto anterior CvPoint* pt_1 = tg_poligono-&gt;cantos[i]; //Canto atual CvPoint* pt_2 = tg_poligono-&gt;cantos[(i + 1) % tg_poligono-&gt;numero_de_lados]; //Canto seguinte //Calculo a distancia entre dois cantos, o que retorna o comprimento do lado do poligono tg_poligono-&gt;lados[i] = sqrt(((pt_1-&gt;x - pt_2-&gt;x)*(pt_1-&gt;x - pt_2-&gt;x)) + ((pt_1-&gt;y - pt_2-&gt;y)*(pt_1-&gt;y - pt_2-&gt;y))); //Calculo o cosseno do angulo correspondente ao ponto atualmente avaliado tg_poligono-&gt;angulos[i] = cosseno(pt_0, pt_2, pt_1); angulos[i] = cosseno(pt_0, pt_2, pt_1); } return tg_poligono; } </code></pre> <p>Ok, my problem is with the tg_poligono->angulos tg_poligono->lados works fine but tg_poligono->angulos fails with apparently no reason!</p> <p>when I use de function in my program (inside a for statment), the code above works for a while and then fails with no message.</p> <p>If I comment the following lines, the program work fine, with no errors:</p> <pre><code>tg_poligono-&gt;angulos = (double*) malloc(tg_poligono-&gt;numero_de_lados * sizeof(double)); ... tg_poligono-&gt;angulos[i] = cosseno(pt_0, pt_2, pt_1); </code></pre> <p>If you look at my code, you'll see a test that i do:</p> <pre><code>double* angulos = (double*) malloc(tg_poligono-&gt;numero_de_lados * sizeof(double)); ... angulos[i] = cosseno(pt_0, pt_2, pt_1); </code></pre> <p>Its the same thing but not using the struct and it works fine.</p> <p>ps.: I'm using opencv functions on this code, but it's unrelated with the problem. the function <code>cosseno(pt_0, pt_2, pt_1)</code> works fine, I sure of that. It's a C code, I can't use C++.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload