0

I have a struct like this:

typedef struct tgPoligono { int numero_de_lados; CvPoint** cantos; float* lados; double* angulos; } *tgPoligono; 

In a function, I'm using it like this:

tgPoligono criar_tgPoligono(CvSeq* poligono){ int i; tgPoligono tg_poligono = (tgPoligono) malloc(sizeof(tgPoligono)); tg_poligono->numero_de_lados = poligono->total; tg_poligono->cantos = (CvPoint**) malloc(tg_poligono->numero_de_lados * sizeof(CvPoint*)); for(i= 0; i < tg_poligono->numero_de_lados; i++){ tg_poligono->cantos[i] = (CvPoint*)cvGetSeqElem(poligono, i); } tg_poligono->lados = (float*) malloc(tg_poligono->numero_de_lados * sizeof(float)); tg_poligono->angulos = (double*) malloc(tg_poligono->numero_de_lados * sizeof(double)); double* angulos = (double*) malloc(tg_poligono->numero_de_lados * sizeof(double)); for(i = 0; i < tg_poligono->numero_de_lados; i++){ CvPoint* pt_0 = tg_poligono->cantos[((i == 0)?(tg_poligono->numero_de_lados - 1):(i - 1))]; //Canto anterior CvPoint* pt_1 = tg_poligono->cantos[i]; //Canto atual CvPoint* pt_2 = tg_poligono->cantos[(i + 1) % tg_poligono->numero_de_lados]; //Canto seguinte //Calculo a distancia entre dois cantos, o que retorna o comprimento do lado do poligono tg_poligono->lados[i] = sqrt(((pt_1->x - pt_2->x)*(pt_1->x - pt_2->x)) + ((pt_1->y - pt_2->y)*(pt_1->y - pt_2->y))); //Calculo o cosseno do angulo correspondente ao ponto atualmente avaliado tg_poligono->angulos[i] = cosseno(pt_0, pt_2, pt_1); angulos[i] = cosseno(pt_0, pt_2, pt_1); } return tg_poligono; } 

Ok, my problem is with the tg_poligono->angulos tg_poligono->lados works fine but tg_poligono->angulos fails with apparently no reason!

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.

If I comment the following lines, the program work fine, with no errors:

tg_poligono->angulos = (double*) malloc(tg_poligono->numero_de_lados * sizeof(double)); ... tg_poligono->angulos[i] = cosseno(pt_0, pt_2, pt_1); 

If you look at my code, you'll see a test that i do:

double* angulos = (double*) malloc(tg_poligono->numero_de_lados * sizeof(double)); ... angulos[i] = cosseno(pt_0, pt_2, pt_1); 

Its the same thing but not using the struct and it works fine.

ps.: I'm using opencv functions on this code, but it's unrelated with the problem. the function cosseno(pt_0, pt_2, pt_1) works fine, I sure of that. It's a C code, I can't use C++.

3
  • The cast of the return value of malloc is not needed and may hide an error. I suggest you remove the casts and let the compiler tell you about the possibly relevant error. Commented Oct 19, 2011 at 11:55
  • Did you mean this: tg_poligono->angulos = malloc(tg_poligono->numero_de_lados * sizeof(double)); I'll try! tnks! Commented Oct 19, 2011 at 12:26
  • That's what I mean. fizzer's answer is more relevant though. Commented Oct 19, 2011 at 12:29

2 Answers 2

1

This is how you want to define your structure, I think:

typedef struct _tgPoligono { int numero_de_lados; CvPoint** cantos; float* lados; double* angulos; } tgPoligono; tgPoligono criar_tgPoligono(CvSeq* poligono){ int i; tgPoligono *tg_poligono = (tgPoligono*) malloc(sizeof(tgPoligono)); etc... 

Note the removal of the star from the end of your structure definition and typedef. That's what's confusing you I think.

Sign up to request clarification or add additional context in comments.

1 Comment

Cool! Vote up my answer too if you like ;-)
1

tgPoligono tg_poligono = (tgPoligono) malloc(sizeof(tgPoligono));

only allocates space for the pointer. Use sizeof(*tgPoligono).

6 Comments

But my struct is like this: typedef struct tgPoligono { ... } *tgPoligono; so I don't need this sizeof(*tgPoligono). Right?
In that case, you allocate memory for a pointer, and not for a structure Try checking sizeof(tgPoligono) and compare it to sizeof(*tgPoligono)
Fizzer is right. You are confusing yourself (and others) by hiding a pointer behind a typedef. Creating a variable that differs only in case from its type is not a good habit either. IMHO
ok, but I'm not having problems with the struct when I don't use the tg_poligono->angulos. You guys think my struct malloc are wrong?
You don't have a structure. Only a pointer to a structure that points to an object that has a size of sizeof(void*); probably 4 or 8.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.