Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6706351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:30:37+00:00 2026-05-26T07:30:37+00:00

I have a struct like this: typedef struct tgPoligono { int numero_de_lados; CvPoint** cantos;

  • 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++.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:30:38+00:00Added an answer on May 26, 2026 at 7:30 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a struct like this: typedef struct { int sizes[3]; float **vals[3]; //
I have a struct that takes a function pointer, like this: typedef int (*node_transition_func)(
Ok so I have struct like this typedef struct { float x; float y;
Let's say I have a first structure like this: typedef struct { int ivalue;
I have a struct like this typedef struct _somestruct { int a; int b;
I have an array like this: typedef struct INSTR { char* str; int argc;
I have a interface documented like this: typedef struct Tree { int a; void*
I have a short piece of code like this: typedef struct { double sX;
I have a header file including a structure like this: typedef struct { int
I have a structure like this typedef struct{ int stat; }dot; And I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.