allocate perfect size for malloc
This commit is contained in:
parent
caa56946d0
commit
1e237fbaeb
1 changed files with 15 additions and 6 deletions
|
@ -1,4 +1,5 @@
|
||||||
#include "../includes/plateau.h"
|
#include "../includes/plateau.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
Table *generate_table(GLuint width, GLuint height, Pos origin, Uint32 color) {
|
Table *generate_table(GLuint width, GLuint height, Pos origin, Uint32 color) {
|
||||||
Table *table = malloc(sizeof(Table));
|
Table *table = malloc(sizeof(Table));
|
||||||
|
@ -10,15 +11,23 @@ Table *generate_table(GLuint width, GLuint height, Pos origin, Uint32 color) {
|
||||||
|
|
||||||
table->nb_columns = table->nb_row = 7;
|
table->nb_columns = table->nb_row = 7;
|
||||||
|
|
||||||
// FIX: Allocate too much memory here
|
GLuint wPas = table->width / table->nb_columns,
|
||||||
table->lines = malloc(table->nb_row * table->nb_columns * 4 * sizeof(int));
|
hPas = table->height / table->nb_row, wMax = table->width - wPas,
|
||||||
|
hMax = table->height - hPas;
|
||||||
|
|
||||||
|
// Calcul l'espace neccessaire
|
||||||
|
int space_needed = 0;
|
||||||
|
for (GLuint i = wPas; i < wMax; i += wPas, space_needed += 4) {
|
||||||
|
for (GLuint j = hPas; j < hMax; j += hPas, space_needed += 4) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
table->lines = malloc(space_needed * sizeof(int));
|
||||||
assert(table->lines != NULL);
|
assert(table->lines != NULL);
|
||||||
|
|
||||||
int *tmp = table->lines;
|
int *tmp = table->lines;
|
||||||
for (GLuint wPas = table->width / table->nb_columns, i = wPas;
|
for (GLuint i = wPas; i < wMax; i += wPas, tmp += 4) {
|
||||||
i < table->width - wPas; i += wPas, tmp += 4) {
|
for (GLuint j = hPas; j < hMax; j += hPas, tmp += 4) {
|
||||||
for (GLuint hPas = table->height / table->nb_row, j = hPas;
|
|
||||||
j < table->height - hPas; j += hPas, tmp += 4) {
|
|
||||||
*tmp = table->width;
|
*tmp = table->width;
|
||||||
*(tmp + 1) = j;
|
*(tmp + 1) = j;
|
||||||
*(tmp + 2) = origin.x;
|
*(tmp + 2) = origin.x;
|
||||||
|
|
Reference in a new issue