title: Using malloc on structs / arrays in other files
body: <p>UPDATE: <em>The problem with the segmentation fault is not within this function as described below, it is within another function of the same program.</em></p>
  
  <p>Im trying to make a program that animates bouncing balls, however I am quite stuck and can't figur out what I am doing wrong. I believe I have isolated the problem to be within the function below. I have sort of figured out that it has something to do with the new-model statements. </p>
  
  <p>Anyway, upon running the code I get segmentation fault and the values drawn by the function (in terms of triangles) are way out of where they should be. I should be getting values between 0 and 1600 but I end up with 94 miillion sometimes. </p>
  
  <p>Any help is greatly appreciated! </p>
  
  <pre><code>object_t *create_object(SDL_Surface *surface, triangle_t *model, int numtriangles){
      object_t *new=malloc(sizeof(object_t));
      new-&gt;surface = surface;
      new-&gt;model = malloc(sizeof(triangle_t)*numtriangles);
      *new-&gt;model= *model;
      new-&gt;numtriangles = numtriangles;
      new-&gt;tx = surface-&gt;w/2;
      new-&gt;ty = surface-&gt;h/2;
      new-&gt;scale = 0.1;
      new-&gt;rotation = 0.0;
  
      return new;
  }
  </code></pre>
  
  <p>NB! The triangle_t *model pointer points to an array wich describes multiple triangles.</p>
  
  <p>EDIT:
  Including struct of object:</p>
  
  <pre><code>typedef struct object object_t;
  
  struct object {
         float       scale;          /* Object scale */
         float       rotation;       /* Object rotation */
         float       tx, ty;         /* Position on screen */
  
         float       speedx, speedy; /* Object speed in x and y direction */
         unsigned int ttl;           /* Time till object should be removed from screen */
  
         int         numtriangles;   /* Number of triangles in model */
         triangle_t  *model;         /* Model triangle array */
  
         SDL_Surface *surface;       /* SDL screen */
  };
  </code></pre>
  
  <p>And struct of triangles:</p>
  
  <pre><code>typedef struct triangle triangle_t;
  
  struct triangle {
      /* Model coordinates, where each pair resemble a corner  */
      int x1, y1;
      int x2, y2;
      int x3, y3;
  
      /* The color the triangle is to be filled with */
      unsigned int fillcolor;
  
      /* Scale factor, meaning 0.5 should half the size, 1 keep, and 2.0 double */
      float scale;
  
      /* The point (tx, ty) where the center of the teapot should be placed on-screen */
      int tx, ty;
  
      /* The degrees the triangle is supposed to be rotated at the current frame */
      float rotation;
  
      /* 
       * Bounding box of on-screen coordinates:
       * rect.x - x-coordinate of the bounding box' top left corner
       * rect.y - y-coordinate of the bounding box' top left corner
       * rect.w - width of the bounding box
       * rect.h - height of the bounding box
       */
       SDL_Rect rect;
  
      /* On-screen coordinates, where each pair resemble a corner */
      int sx1, sy1;
      int sx2, sy2;
      int sx3, sy3;
  };
  </code></pre>