Skip to main content
added 5 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Is passing arrays to functions memory safe? Allocating matrices for in-place modification

My headerThis code seems to be working. I am allocating matrices on the stack and passing them to functions to modify in place. Is this a standard practice, or am I doing it wrong?

Header:

my implementationImplementation:

Code seems to be working. I am allocating matrices on stack and passing them to function to modify in place. Is this a standard practice, or am I doing it wrong?

Is passing arrays to functions memory safe?

My header:

my implementation

Code seems to be working. I am allocating matrices on stack and passing them to function to modify in place. Is this a standard practice, or am I doing it wrong?

Allocating matrices for in-place modification

This code seems to be working. I am allocating matrices on the stack and passing them to functions to modify in place. Is this a standard practice, or am I doing it wrong?

Header:

Implementation:

Source Link
yasar
  • 1.6k
  • 14
  • 30

Is passing arrays to functions memory safe?

My header:

#ifndef _MATRIX_H #define _MATRIX_H typedef enum {MT_OK, MT_INVAL} MT_RESULT; typedef float mat4[4][4]; typedef float vec4[4]; MT_RESULT mat4multvec4(vec4 target, mat4 m, vec4 v); MT_RESULT zero4(mat4 target); MT_RESULT id4(mat4 target); MT_RESULT translate(mat4 target, float x, float y, float z); MT_RESULT scale(mat4 target, float x, float y, float z); MT_RESULT perspective(mat4 target, float fov, float aspectratio, float nearclip, float farclip); #endif 

my implementation

#include <stdlib.h> #include <stdio.h> #include <string.h> /* memcpy */ #include <math.h> #include "matrix.h" #undef PI #define PI 3.14159265 /* Multiply 4x4 matrix with 1x4 vector returns MT_OK */ MT_RESULT mat4multvec4(vec4 target, mat4 m, vec4 v) { int i, j, sum; for(i = 0; i < 4; i++) { sum = 0; for(j=0; j<4; j++) { sum = sum + m[i][j] * v[j]; } target[i] = sum; } return MT_OK; } /* make a 4x4 zeros matrix returns MT_OK */ MT_RESULT zero4(mat4 target) { memset(target, 0, sizeof(float) * 16); return MT_OK; } /* make a 4x4 identity matrix returns MT_OK */ MT_RESULT id4(mat4 target) { int i; zero4(target); for(i = 0; i < 4; i++) target[i][i] = 1; return MT_OK; } /* Create 4x4 translation matrix for opengl return MT_OK */ MT_RESULT translate(mat4 target, float x, float y, float z) { id4(target); target[0][3] = x; target[1][3] = y; target[2][3] = z; return MT_OK; } /* Create 4x4 scale matrix for opengl return MT_OK */ MT_RESULT scale(mat4 target, float x, float y, float z) { zero4(target); target[0][0] = x; target[1][1] = y; target[2][2] = z; target[3][3] = 1; return MT_OK; } /* mat4 perspective(float fov, float aspectratio, float nearclip, float farclip) { mat4 m = new_mat4(); float frustumdepth = farclip - nearclip; float inversefrustumdepth = 1 / frustumdepth; m[] }*/ void main(void) { int i; mat4 myMatrix; vec4 myVector = {10.0f, 10.0f, 10.0f, 1.0f}; vec4 myTVector; translate(myMatrix, 10.0f, 0.0f, 0.0f); mat4multvec4(myTVector, myMatrix, myVector); printf("Original Vector\n"); for(i=0;i<4;i++) { printf(" %f", myVector[i]); } printf("\nTransformed Vector\n"); for(i=0;i<4;i++) { printf(" %f", myTVector[i]); } } 

Code seems to be working. I am allocating matrices on stack and passing them to function to modify in place. Is this a standard practice, or am I doing it wrong?