Using malloc on structs / arrays in other files












0














UPDATE: The problem with the segmentation fault is not within this function as described below, it is within another function of the same program.



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.



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.



Any help is greatly appreciated!



object_t *create_object(SDL_Surface *surface, triangle_t *model, int numtriangles){
object_t *new=malloc(sizeof(object_t));
new->surface = surface;
new->model = malloc(sizeof(triangle_t)*numtriangles);
*new->model= *model;
new->numtriangles = numtriangles;
new->tx = surface->w/2;
new->ty = surface->h/2;
new->scale = 0.1;
new->rotation = 0.0;

return new;
}


NB! The triangle_t *model pointer points to an array wich describes multiple triangles.



EDIT:
Including struct of object:



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 */
};


And struct of triangles:



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;
};









share|improve this question




















  • 1




    you should include the definition of your struct.
    – Christian Gibbons
    Nov 12 '18 at 18:13










  • Of course! I have included them now
    – Peter
    Nov 12 '18 at 18:21










  • While technically not wrong, calling a variable new in C can be confusing, since new is a reserved word in C++. Suggest calling it something else.
    – dbush
    Nov 12 '18 at 18:22
















0














UPDATE: The problem with the segmentation fault is not within this function as described below, it is within another function of the same program.



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.



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.



Any help is greatly appreciated!



object_t *create_object(SDL_Surface *surface, triangle_t *model, int numtriangles){
object_t *new=malloc(sizeof(object_t));
new->surface = surface;
new->model = malloc(sizeof(triangle_t)*numtriangles);
*new->model= *model;
new->numtriangles = numtriangles;
new->tx = surface->w/2;
new->ty = surface->h/2;
new->scale = 0.1;
new->rotation = 0.0;

return new;
}


NB! The triangle_t *model pointer points to an array wich describes multiple triangles.



EDIT:
Including struct of object:



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 */
};


And struct of triangles:



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;
};









share|improve this question




















  • 1




    you should include the definition of your struct.
    – Christian Gibbons
    Nov 12 '18 at 18:13










  • Of course! I have included them now
    – Peter
    Nov 12 '18 at 18:21










  • While technically not wrong, calling a variable new in C can be confusing, since new is a reserved word in C++. Suggest calling it something else.
    – dbush
    Nov 12 '18 at 18:22














0












0








0







UPDATE: The problem with the segmentation fault is not within this function as described below, it is within another function of the same program.



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.



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.



Any help is greatly appreciated!



object_t *create_object(SDL_Surface *surface, triangle_t *model, int numtriangles){
object_t *new=malloc(sizeof(object_t));
new->surface = surface;
new->model = malloc(sizeof(triangle_t)*numtriangles);
*new->model= *model;
new->numtriangles = numtriangles;
new->tx = surface->w/2;
new->ty = surface->h/2;
new->scale = 0.1;
new->rotation = 0.0;

return new;
}


NB! The triangle_t *model pointer points to an array wich describes multiple triangles.



EDIT:
Including struct of object:



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 */
};


And struct of triangles:



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;
};









share|improve this question















UPDATE: The problem with the segmentation fault is not within this function as described below, it is within another function of the same program.



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.



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.



Any help is greatly appreciated!



object_t *create_object(SDL_Surface *surface, triangle_t *model, int numtriangles){
object_t *new=malloc(sizeof(object_t));
new->surface = surface;
new->model = malloc(sizeof(triangle_t)*numtriangles);
*new->model= *model;
new->numtriangles = numtriangles;
new->tx = surface->w/2;
new->ty = surface->h/2;
new->scale = 0.1;
new->rotation = 0.0;

return new;
}


NB! The triangle_t *model pointer points to an array wich describes multiple triangles.



EDIT:
Including struct of object:



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 */
};


And struct of triangles:



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;
};






c struct malloc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 18:45

























asked Nov 12 '18 at 18:11









Peter

112




112








  • 1




    you should include the definition of your struct.
    – Christian Gibbons
    Nov 12 '18 at 18:13










  • Of course! I have included them now
    – Peter
    Nov 12 '18 at 18:21










  • While technically not wrong, calling a variable new in C can be confusing, since new is a reserved word in C++. Suggest calling it something else.
    – dbush
    Nov 12 '18 at 18:22














  • 1




    you should include the definition of your struct.
    – Christian Gibbons
    Nov 12 '18 at 18:13










  • Of course! I have included them now
    – Peter
    Nov 12 '18 at 18:21










  • While technically not wrong, calling a variable new in C can be confusing, since new is a reserved word in C++. Suggest calling it something else.
    – dbush
    Nov 12 '18 at 18:22








1




1




you should include the definition of your struct.
– Christian Gibbons
Nov 12 '18 at 18:13




you should include the definition of your struct.
– Christian Gibbons
Nov 12 '18 at 18:13












Of course! I have included them now
– Peter
Nov 12 '18 at 18:21




Of course! I have included them now
– Peter
Nov 12 '18 at 18:21












While technically not wrong, calling a variable new in C can be confusing, since new is a reserved word in C++. Suggest calling it something else.
– dbush
Nov 12 '18 at 18:22




While technically not wrong, calling a variable new in C can be confusing, since new is a reserved word in C++. Suggest calling it something else.
– dbush
Nov 12 '18 at 18:22












1 Answer
1






active

oldest

votes


















2














This line is copying only the first triangle:



*new->model = *model;


From the point of view of your function model is only a pointer to an object. The compiler doesn't know it points to an array of triangles, hence we need to pass the number of triangles in there as an argument.



Replace it for:



memcpy( new->model, model, sizeof(triangle_t)*numtriangles);


Additional comments:




  • Remember to free the model when freeing the object

  • Replace new for something else like newObj if you ever consider to compile this with a c++ compiler


More info:




  • https://linux.die.net/man/3/memcpy

  • https://en.cppreference.com/w/c/string/byte/memcpy


[EDIT]
Regarding to the segmentation fault: your function is correct now and it is not causing SEGFAULT unless you are running out of memory, what is very unlikely. Anyway, if you are running out of memory and getting a SEGFAULT in that function then the problem is either:




  • you are not deallocating memory correctly somewhere else and then you have a memory leak making you run out of memory improperly.

  • your platform needs more memory what, despite unlikely, is possible especially if it is a limited embedded platform


Post another question with the backtrace of the segfault.






share|improve this answer























  • Thanks! It did definetly make the triangles translate correctly and they now have the correct values! The segmentation fault still stands though?
    – Peter
    Nov 12 '18 at 18:26










  • the segmentation fault is not caused by that function
    – olivecoder
    Nov 12 '18 at 18:32










  • Yeah I'm starting to realise that it may not be the issue at all! But thanks to everyone!
    – Peter
    Nov 12 '18 at 18:43











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267814%2fusing-malloc-on-structs-arrays-in-other-files%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














This line is copying only the first triangle:



*new->model = *model;


From the point of view of your function model is only a pointer to an object. The compiler doesn't know it points to an array of triangles, hence we need to pass the number of triangles in there as an argument.



Replace it for:



memcpy( new->model, model, sizeof(triangle_t)*numtriangles);


Additional comments:




  • Remember to free the model when freeing the object

  • Replace new for something else like newObj if you ever consider to compile this with a c++ compiler


More info:




  • https://linux.die.net/man/3/memcpy

  • https://en.cppreference.com/w/c/string/byte/memcpy


[EDIT]
Regarding to the segmentation fault: your function is correct now and it is not causing SEGFAULT unless you are running out of memory, what is very unlikely. Anyway, if you are running out of memory and getting a SEGFAULT in that function then the problem is either:




  • you are not deallocating memory correctly somewhere else and then you have a memory leak making you run out of memory improperly.

  • your platform needs more memory what, despite unlikely, is possible especially if it is a limited embedded platform


Post another question with the backtrace of the segfault.






share|improve this answer























  • Thanks! It did definetly make the triangles translate correctly and they now have the correct values! The segmentation fault still stands though?
    – Peter
    Nov 12 '18 at 18:26










  • the segmentation fault is not caused by that function
    – olivecoder
    Nov 12 '18 at 18:32










  • Yeah I'm starting to realise that it may not be the issue at all! But thanks to everyone!
    – Peter
    Nov 12 '18 at 18:43
















2














This line is copying only the first triangle:



*new->model = *model;


From the point of view of your function model is only a pointer to an object. The compiler doesn't know it points to an array of triangles, hence we need to pass the number of triangles in there as an argument.



Replace it for:



memcpy( new->model, model, sizeof(triangle_t)*numtriangles);


Additional comments:




  • Remember to free the model when freeing the object

  • Replace new for something else like newObj if you ever consider to compile this with a c++ compiler


More info:




  • https://linux.die.net/man/3/memcpy

  • https://en.cppreference.com/w/c/string/byte/memcpy


[EDIT]
Regarding to the segmentation fault: your function is correct now and it is not causing SEGFAULT unless you are running out of memory, what is very unlikely. Anyway, if you are running out of memory and getting a SEGFAULT in that function then the problem is either:




  • you are not deallocating memory correctly somewhere else and then you have a memory leak making you run out of memory improperly.

  • your platform needs more memory what, despite unlikely, is possible especially if it is a limited embedded platform


Post another question with the backtrace of the segfault.






share|improve this answer























  • Thanks! It did definetly make the triangles translate correctly and they now have the correct values! The segmentation fault still stands though?
    – Peter
    Nov 12 '18 at 18:26










  • the segmentation fault is not caused by that function
    – olivecoder
    Nov 12 '18 at 18:32










  • Yeah I'm starting to realise that it may not be the issue at all! But thanks to everyone!
    – Peter
    Nov 12 '18 at 18:43














2












2








2






This line is copying only the first triangle:



*new->model = *model;


From the point of view of your function model is only a pointer to an object. The compiler doesn't know it points to an array of triangles, hence we need to pass the number of triangles in there as an argument.



Replace it for:



memcpy( new->model, model, sizeof(triangle_t)*numtriangles);


Additional comments:




  • Remember to free the model when freeing the object

  • Replace new for something else like newObj if you ever consider to compile this with a c++ compiler


More info:




  • https://linux.die.net/man/3/memcpy

  • https://en.cppreference.com/w/c/string/byte/memcpy


[EDIT]
Regarding to the segmentation fault: your function is correct now and it is not causing SEGFAULT unless you are running out of memory, what is very unlikely. Anyway, if you are running out of memory and getting a SEGFAULT in that function then the problem is either:




  • you are not deallocating memory correctly somewhere else and then you have a memory leak making you run out of memory improperly.

  • your platform needs more memory what, despite unlikely, is possible especially if it is a limited embedded platform


Post another question with the backtrace of the segfault.






share|improve this answer














This line is copying only the first triangle:



*new->model = *model;


From the point of view of your function model is only a pointer to an object. The compiler doesn't know it points to an array of triangles, hence we need to pass the number of triangles in there as an argument.



Replace it for:



memcpy( new->model, model, sizeof(triangle_t)*numtriangles);


Additional comments:




  • Remember to free the model when freeing the object

  • Replace new for something else like newObj if you ever consider to compile this with a c++ compiler


More info:




  • https://linux.die.net/man/3/memcpy

  • https://en.cppreference.com/w/c/string/byte/memcpy


[EDIT]
Regarding to the segmentation fault: your function is correct now and it is not causing SEGFAULT unless you are running out of memory, what is very unlikely. Anyway, if you are running out of memory and getting a SEGFAULT in that function then the problem is either:




  • you are not deallocating memory correctly somewhere else and then you have a memory leak making you run out of memory improperly.

  • your platform needs more memory what, despite unlikely, is possible especially if it is a limited embedded platform


Post another question with the backtrace of the segfault.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 18:55

























answered Nov 12 '18 at 18:18









olivecoder

1,8961215




1,8961215












  • Thanks! It did definetly make the triangles translate correctly and they now have the correct values! The segmentation fault still stands though?
    – Peter
    Nov 12 '18 at 18:26










  • the segmentation fault is not caused by that function
    – olivecoder
    Nov 12 '18 at 18:32










  • Yeah I'm starting to realise that it may not be the issue at all! But thanks to everyone!
    – Peter
    Nov 12 '18 at 18:43


















  • Thanks! It did definetly make the triangles translate correctly and they now have the correct values! The segmentation fault still stands though?
    – Peter
    Nov 12 '18 at 18:26










  • the segmentation fault is not caused by that function
    – olivecoder
    Nov 12 '18 at 18:32










  • Yeah I'm starting to realise that it may not be the issue at all! But thanks to everyone!
    – Peter
    Nov 12 '18 at 18:43
















Thanks! It did definetly make the triangles translate correctly and they now have the correct values! The segmentation fault still stands though?
– Peter
Nov 12 '18 at 18:26




Thanks! It did definetly make the triangles translate correctly and they now have the correct values! The segmentation fault still stands though?
– Peter
Nov 12 '18 at 18:26












the segmentation fault is not caused by that function
– olivecoder
Nov 12 '18 at 18:32




the segmentation fault is not caused by that function
– olivecoder
Nov 12 '18 at 18:32












Yeah I'm starting to realise that it may not be the issue at all! But thanks to everyone!
– Peter
Nov 12 '18 at 18:43




Yeah I'm starting to realise that it may not be the issue at all! But thanks to everyone!
– Peter
Nov 12 '18 at 18:43


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267814%2fusing-malloc-on-structs-arrays-in-other-files%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma