I have a question regarding some memory management in C.
I have to allocate some memory to create multidimensional arrays so I use malloc. For example here:
par.prueba[0][0] = (int *)malloc( 4 * sizeof ( int ) );
Some of them I have to dynamically assign the memory inside a loop like this:
for(;j<=par.param1[0][0];j++){
pj = -Np * cP + (j - 1) * cP; //aquí va de -NpcP hasta NpcP
par.prueba[0][j-1][2] = floor((Cx + pj) * par.Bn + 0.5) + par.Bn/2; //xb
if(j!=par.param1[0][0])
par.prueba[0][j] = (int *)malloc( 4 * sizeof ( int ) );
}
I have several lines like this through my code. I also check if the allocation worked like this:
if(par.prueba[0][0]==NULL){
printf("Fail\n");
exit(2);
}
The thing is that sometimes malloc fails to allocate the requested memory so my program exits. It exists in different places everytime I compile and run again. If I don’t check the allocated space my program runs with no problem but eventually crashes.
I used valgrind to try and find the problem but I get a lot of errors of this type:
5,080 bytes in 5 blocks are possibly lost in loss record 5,640 of 5,670
and the error normally traces back to a similar line like this
par.prueba[0][0] = (int *)malloc( 4 * sizeof ( int ) );
In the end this is what valgrind tells me:
==13628== LEAK SUMMARY:
==13628== definitely lost: 2,856 bytes in 10 blocks
==13628== indirectly lost: 10,120 bytes in 505 blocks
==13628== possibly lost: 614,134 bytes in 6,119 blocks
==13628== still reachable: 497,948 bytes in 6,432 blocks
==13628== suppressed: 0 bytes in 0 blocks
So I’m pretty sure I’m not running out of memory but I get a lot of memory leaks. My question is, what is it I’m doing wrong? Am I using malloc correctly? Is there a problem when using malloc so much and very frequently? Any tips to improve that part of the code?
Thanks in advance
EDIT
Thanks for the quick answers!
To answer George’s questions
Maybe I should write more of my code here to make it clearer, the code is a little long so I obviate some of the things just to make it more clear.
prueba[0][0] is intialized before that loop. The thing is that I have several such loops across my code but I’m pretty sure I initialize all the values. This is the code
I first declare a struct like this
typedef struct parametros{
int cT;
float Bn;
int **prueba[360];
int param1[360][3];
int ***pixels[360];
}
struct parametros par;
Then I use it like this
par.param1[0][0] = 2*Np + 1; //Np is initialized before
par.prueba[0] = (int **)malloc( par.param1[0][0]* sizeof ( int * ) );
if(par.prueba[0]==NULL){
printf("Fail\n");
exit(2);
}
par.prueba[0][0] = (int *)malloc( 4 * sizeof ( int ) );
if(par.prueba[0][0]==NULL){
printf("Fail\n");
exit(2);
}
Then I assign some values to the array like this
par.prueba[0][0][1] = floor(Nhe/par.cT);//Nhe and par.cT are initialized before
par.prueba[0][0][0] = -par.prueba[0][0][1];
par.param1[0][1] = 0;
par.param1[0][2] = par.cT * par.Bn; //par.Bn is also initialized before
par.prueba[0][0][3] = floor((Cy + par.prueba[0][0][0] * par.cT) * par.Bn + 0.5) + par.Bn/2; //Cy is initialized before
And then the loop
int j = 1;
for(;j<=par.param1[0][0];j++){
pj = -Np * cP + (j - 1) * cP; //cP is initialized before and pj is declared before
par.prueba[0][j-1][2] = floor((Cx + pj) * par.Bn + 0.5) + par.Bn/2; //xb
if(j!=par.param1[0][0]){
par.prueba[0][j] = (int *)malloc( 4 * sizeof ( int ) );
if(par.prueba[0][j]==NULL){
printf("Fail\n");
exit(2);
}
}
}
I do similar things for all the 360 values of par.prueba
To answer PaulP.R.O.’s question.
No I don’t free the memory. I did that before and it crashed when I tried to free it. I’ll try again to see if something changes. I think my problem was trying to figure out when to free it.
To answer AoeAoe’s
I think my code is equivalent to what you did right?
To answer Chris Lutz’s question.
I don’t necessarily want to exit for every failure I get but if I let the code run without exiting it runs fine but then it crashes randomly…
EDIT 2
I added the code to free memory after I finish using the “arrays” and same problem it exits because it fails to allocate some memory with malloc
for(y=0;y<360;y++){
for(t=0;t<par.param1[y][0];t++){
free(par.prueba[y][t]);
}
free(par.prueba[y]);
}
EDIT 3 — Complete code
Ok here is the complete code, sorry if it is a little confusing. I will explain if I have to. Basically I need the multidimensional arrays to store some parameters I will afterwards use to calculate some things using the values of the pixels of some image. The code is not finished but I was trying to test how it worked with all the dynamically allocated memory. This code compiles without a problem and, at first runs without a problem if I don’t check the memory allocations after using malloc. But once I start using the program it randomly crashes.
#include <stdio.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <magick/MagickCore.h>
void ClickCallback(GtkWidget *widget, GdkEventButton *event, gpointer callback_data);
void computar_transformadas(GtkWidget *widget, GdkEventButton *event, gpointer callback_data);
void ext_parametros(GtkImage *img);
static void destroy_event(GtkWidget *widget, gpointer data);
static gboolean delete_event(GtkWidget *widget, GdkEvent *event, gpointer data);
void suma(int** trazo,int o,int entra);
GtkWidget *window, *caja, *button1, *button2, *file_selection_box;
GtkImage *imagen = NULL;
GdkPixbuf *pixbuf = NULL;
guchar *pixs = NULL;
guchar *p = NULL;
int rowstride, n_channels;
typedef struct parametros{
int cT;
float Bn;
int **prueba[360];
int param1[360][3];
int ***pixels[360];
};
struct parametros par;
int main (int argc, char *argv[]){
/*-- Initialize GTK --*/
gtk_init (&argc, &argv);
/*-- Create the new window --*/
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
button1 = gtk_button_new_with_label("Abrir.");
button2 = gtk_button_new_with_label("Analizar.");
caja = gtk_vbox_new(0,0);
gtk_window_set_default_size(window,200,200);
/*-- Agrega funcionalidad al botón -- */
g_signal_connect(G_OBJECT(button1), "button_press_event", G_CALLBACK(ClickCallback), NULL);
/*-- Agrega funcionalidad al botón -- */
g_signal_connect(G_OBJECT(button2), "button_press_event", G_CALLBACK(computar_transformadas), NULL);
/*-- Agrega el botón a la ventana -- */
gtk_container_add(GTK_CONTAINER(window), caja);
gtk_box_pack_end(caja,button1,FALSE,FALSE,1);
gtk_box_pack_end(caja,button2,FALSE,FALSE,1);
/*-- Display the window con el botón --*/
gtk_widget_show_all(window);
/*-- Start the GTK event loop --*/
gtk_main();
/* -- Cierra los eventos -- */
g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(delete_event), NULL);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy_event), NULL);
/*-- Return 0 if exit is successful --*/
return 0;
}
void open_file(char *file){
if(imagen != NULL)
gtk_image_clear(imagen);
imagen = gtk_image_new_from_file(file);
gtk_box_pack_end(caja,imagen,FALSE,FALSE,1);
gtk_widget_show_all(window);
ext_parametros(imagen);
}
void ext_parametros(GtkImage *img){
pixbuf = gtk_image_get_pixbuf(img);
n_channels = gdk_pixbuf_get_n_channels(pixbuf);
pixs=gdk_pixbuf_get_pixels(pixbuf);
p=gdk_pixbuf_get_pixels(pixbuf);
rowstride=gdk_pixbuf_get_rowstride(pixbuf);
int pT = 1;
int pj;
int M = gdk_pixbuf_get_width(pixbuf);
int N = gdk_pixbuf_get_height(pixbuf);
int cP = 1; //paso en P
par.cT = 1;
par.Bn = pow(2,12); //division de cada pixel
float Cx = (float) (M-1)/2; //centro de la imagen eje x
float Cy = (float) (N-1)/2; //centro de la imagen eje y
float Mh = Cx + 0.5; //pixel central
float Nh = Cy + 0.5; //pixel central
double Pmax = pow((pow(Mh,2)+pow(Nh,2)),0.5)-0.001;
double E = (1/(2*par.Bn))*(((2*Pmax)/pT)+1);
double Mhe = Mh - E; //largo tomando en cuenta el error
double Nhe = Nh - E; //ancho tomando en cuenta el error
int MBn = M * par.Bn;
int NBn = N * par.Bn;
/* φ = 0 */
int Np = floor(Mhe/cP);
par.param1[0][0] = 2*Np + 1; //NP
par.prueba[0] = (int **)malloc( par.param1[0][0]* sizeof ( int * ) );
if(par.prueba[0]==NULL){
printf("¡Fallo al asignar memoria! 0\n");
exit(2);
}
par.prueba[0][0] = (int *)malloc( 4 * sizeof ( int ) );
if(par.prueba[0][0]==NULL){
printf("¡Fallo al asignar memoria! 0\n");
exit(2);
}
par.prueba[0][0][1] = floor(Nhe/par.cT);//tend
par.prueba[0][0][0] = -par.prueba[0][0][1];//tbegin
par.param1[0][1] = 0; //xinc
par.param1[0][2] = par.cT * par.Bn; //yinc
par.prueba[0][0][3] = floor((Cy + par.prueba[0][0][0] * par.cT) * par.Bn + 0.5) + par.Bn/2; //yb
//------------ -NpcP < p < NpcP ----------------//
int j = 1;
for(;j<=par.param1[0][0];j++){
pj = -Np * cP + (j - 1) * cP; //aquí va de -NpcP hasta NpcP
par.prueba[0][j-1][2] = floor((Cx + pj) * par.Bn + 0.5) + par.Bn/2; //xb
if(j!=par.param1[0][0]){
par.prueba[0][j] = (int *)malloc( 4 * sizeof ( int ) );
if(par.prueba[0][j]==NULL){
printf("¡Fallo al asignar memoria! 0 %d\n",j);
exit(2);
}
}
}
//----------- Precomputar y calcular 0 < φ < 90 --------------//
int phi = 1;
double t1x;
double t1y;
double t2x;
double t2y;
for(;phi<90;phi++){
Pmax = Mhe * fabs(cos(phi)) + Nhe * fabs(sin(phi));
Np = floor(Pmax/cP);
par.param1[phi][0] = 2 * Np + 1; //NP
par.prueba[phi] = (int **)malloc( par.param1[phi][0]* sizeof ( int * ) );
if(par.prueba[phi]==NULL){
printf("¡Fallo al asignar memoria! %d\n",phi);
exit(2);
}
par.prueba[phi][0] = (int *)malloc( 4 * sizeof ( int ) );
if(par.prueba[phi][0]==NULL){
printf("¡Fallo al asignar memoria! %d\n",phi);
exit(2);
}
par.param1[phi][1] = -par.cT*sin(phi)*par.Bn; //xinc
par.param1[phi][2] = par.cT*cos(phi)*par.Bn; //yinc
for(j=1;j<=par.param1[phi][0];j++){
pj = -Np * cP + (j - 1) * cP;
t1y = -((Mhe - (pj * cos(phi)))/sin(phi));
t1x = -((Nhe + (pj * sin(phi)))/cos(phi));
t2y = ((Mhe + (pj * cos(phi)))/sin(phi));
t2x = ((Nhe - (pj * sin(phi)))/cos(phi));
par.prueba[phi][j-1][0] = ceil(fmax(t1y,t1x)/par.cT); //tbegin
par.prueba[phi][j-1][1] = floor(fmin(t2y,t2x)/par.cT); //tend
par.prueba[phi][j-1][2] = floor((Cx + pj * cos(phi)) * par.Bn + 0.5 + (par.param1[phi][1] * par.prueba[phi][j-1][0])) + par.Bn/2; //xb
par.prueba[phi][j-1][3] = floor((Cy + pj * sin(phi)) * par.Bn + 0.5 + (par.param1[phi][2] * par.prueba[phi][j-1][0])) + par.Bn/2; //yb
if(j!=par.param1[phi][0]){
par.prueba[phi][j] = (int *)malloc( 4 * sizeof ( int ) );
if(par.prueba[phi][j]==NULL){
printf("¡Fallo al asignar memoria! %d %d\n",phi,j);
exit(2);
}
}
}
}
//----------- Precomputar φ = 90 --------------//
int tendbeg;
Np = floor(Nhe/cP);
par.param1[90][0] = 2 * Np + 1; //NP
par.prueba[90] = (int **)malloc( par.param1[90][0]* sizeof ( int * ) );
if(par.prueba[90]==NULL){
printf("¡Fallo al asignar memoria! 90\n");
exit(2);
}
par.prueba[90][0] = (int *)malloc( 4 * sizeof ( int ) );
if(par.prueba[90][0]==NULL){
printf("¡Fallo al asignar memoria! 90\n");
exit(2);
}
par.prueba[90][0][1] = floor(Mhe/par.cT); //tend
par.prueba[90][0][0] = -par.prueba[90][0][1]; //tbegin
tendbeg = par.prueba[90][0][1] - par.prueba[90][0][0];
par.param1[90][1] = -par.cT * par.Bn; //xinc
par.param1[90][2] = 0; //yinc
par.prueba[90][0][2] = floor(Cx * par.Bn + par.param1[90][1] * par.prueba[90][0][0] + 0.5) + par.param1[90][1] * par.Bn + par.Bn/2; //xb
//----------- Calcular φ = 90 --------------//
for(j=1;j<=par.param1[90][0];j++){
pj = -Np * cP + (j - 1) * cP; //aquí va de -NpcP hasta NpcP
par.prueba[90][j-1][3] = floor((Cy + pj) * par.Bn + 0.5) + par.Bn/2; //se guarda cada valor de cada línea //yb
if(j!=par.param1[90][0]){
par.prueba[90][j] = (int *)malloc( 4 * sizeof ( int ) );
if(par.prueba[90][j]==NULL){
printf("¡Fallo al asignar memoria! 90 %d\n",j);
exit(2);
}
}
}
//----------- Precomputar y calcular 90 < φ < 180 --------------//
phi = 91;
for(;phi<180;phi++){
Pmax = Mhe * fabs(cos(phi)) + Nhe * fabs(sin(phi));
Np = floor(Pmax/cP);
par.param1[phi][0] = 2 * Np + 1; //NP
par.prueba[phi] = (int **)malloc( par.param1[phi][0]* sizeof ( int * ) );
if(par.prueba[phi]==NULL){
printf("¡Fallo al asignar memoria! %d\n",phi);
exit(2);
}
par.prueba[phi][0] = (int *)malloc( 4 * sizeof ( int ) );
if(par.prueba[phi][0]==NULL){
printf("¡Fallo al asignar memoria! %d\n",phi);
exit(2);
}
par.param1[phi][1] = -(par.cT*sin(phi)*par.Bn); //xinc
par.param1[phi][2] = par.cT*cos(phi)*par.Bn; //yinc
for(j=1;j<=par.param1[phi][0];j++){
pj = -Np * cP + (j - 1) * cP;
t1y = -((Mhe - (pj * cos(phi)))/sin(phi));
t1x = ((Nhe - (pj * sin(phi)))/cos(phi));
t2y = ((Mhe + (pj * cos(phi)))/sin(phi));
t2x = -((Nhe + (pj * sin(phi)))/cos(phi));
par.prueba[phi][j-1][0] = ceil(fmax(t1y,t1x)/par.cT); //tbegin
par.prueba[phi][j-1][1] = floor(fmin(t2y,t2x)/par.cT); //tend
par.prueba[phi][j-1][2] = floor((Cx + pj * cos(phi)) * par.Bn + 0.5 + (par.param1[phi-1][1] * par.prueba[phi][j-1][0])) + par.Bn/2; //xb
par.prueba[phi][j-1][3] = floor((Cy + pj * sin(phi)) * par.Bn + 0.5 + (par.param1[phi-1][2] * par.prueba[phi][j-1][0])) + par.Bn/2; //yb
if(j!=par.param1[phi][0]){
par.prueba[phi][j] = (int *)malloc( 4 * sizeof ( int ) );
if(par.prueba[phi][j]==NULL){
printf("¡Fallo al asignar memoria! %d %d\n",phi,j);
exit(2);
}
}
}
}
//----------- Precomputar φ = 180 --------------//
Np = floor(Mhe/cP);
par.param1[180][0] = 2*Np + 1; //NP de 0 grados
par.prueba[180] = (int **)malloc( par.param1[180][0]* sizeof ( int * ) );
if(par.prueba[180]==NULL){
printf("¡Fallo al asignar memoria! 180\n");
exit(2);
}
par.prueba[180][0] = (int *)malloc( 4 * sizeof ( int ) );
if(par.prueba[180][0]==NULL){
printf("¡Fallo al asignar memoria! 180\n");
exit(2);
}
par.prueba[180][0][0] = -floor(Nhe/par.cT); //tbegin //-tend de 0 grados
par.prueba[180][0][1] = -par.prueba[180][0][0]; //tend //-tbegin de 0 grados
par.param1[180][1] = 0; //xinc
par.param1[180][2] = -(par.cT * par.Bn); //yinc
par.prueba[180][0][3] = floor((Cy - par.prueba[180][0][0] * par.cT) * par.Bn + 0.5) + par.Bn/2; //yb
//------------ Calcular φ = 180 ----------------//
for(j=1;j<=par.param1[180][0];j++){
pj = -Np * cP + (j - 1) * cP; //aquí va de -NpcP hasta NpcP
par.prueba[180][j-1][2] = floor((Cx - pj) * par.Bn + 0.5) + par.Bn/2; //se guarda cada valor de cada línea //xb
if(j!=par.param1[180][0]){
par.prueba[180][j] = (int *)malloc( 4 * sizeof ( int ) );
if(par.prueba[180][j]==NULL){
printf("¡Fallo al asignar memoria! 180 %d\n",j);
exit(2);
}
}
}
//----------- Precomputar y calcular 180 < φ < 270 --------------//
phi = 181;
for(;phi<270;phi++){
Pmax = Mhe * fabs(cos(phi)) + Nhe * fabs(sin(phi));
Np = floor(Pmax/cP);
par.param1[phi][0] = 2 * Np + 1; //NP
par.prueba[phi] = (int **)malloc( par.param1[phi][0]* sizeof ( int * ) );
if(par.prueba[phi]==NULL){
printf("¡Fallo al asignar memoria! %d\n",phi);
exit(2);
}
par.prueba[phi][0] = (int *)malloc( 4 * sizeof ( int ) );
if(par.prueba[phi][0]==NULL){
printf("¡Fallo al asignar memoria! %d 0\n",phi);
exit(2);
}
par.param1[phi][1] = -(par.cT*sin(phi)*par.Bn); //xinc
par.param1[phi][2] = par.cT*cos(phi)*par.Bn; //yinc
for(j=1;j<=par.param1[phi][0];j++){
pj = -Np * cP + (j - 1) * cP;
t1y = ((Mhe + (pj * cos(phi)))/sin(phi));
t1x = ((Nhe - (pj * sin(phi)))/cos(phi));
t2y = -((Mhe - (pj * cos(phi)))/sin(phi));
t2x = -((Nhe + (pj * sin(phi)))/cos(phi));
par.prueba[phi][j-1][0] = ceil(fmax(t1y,t1x)/par.cT); //tbegin
par.prueba[phi][j-1][1] = floor(fmin(t2y,t2x)/par.cT); //tend
par.prueba[phi][j-1][2] = floor((Cx + pj * cos(phi)) * par.Bn + 0.5 + (par.param1[phi-1][1] * par.prueba[phi][j-1][0])) + par.Bn/2; //xb
par.prueba[phi][j-1][3] = floor((Cy + pj * sin(phi)) * par.Bn + 0.5 + (par.param1[phi-1][2] * par.prueba[phi][j-1][0])) + par.Bn/2; //yb
if(j!=par.param1[phi][0]){
par.prueba[phi][j] = (int *)malloc( 4 * sizeof ( int ) );
if(par.prueba[phi][j]==NULL){
printf("¡Fallo al asignar memoria! %d %d\n",phi,j);
exit(2);
}
}
}
}
//----------- Precomputar φ = 270 --------------//
Np = floor(Nhe/cP);
par.param1[270][0] = 2 * Np + 1; //NP //NP del angulo de 90 param1[90][0]
par.prueba[270] = (int **)malloc( par.param1[270][0]* sizeof ( int * ) );
if(par.prueba[270]==NULL){
printf("¡Fallo al asignar memoria! 270\n");
exit(2);
}
par.prueba[270][0] = (int *)malloc( 4 * sizeof ( int ) );
if(par.prueba[270][0]==NULL){
printf("¡Fallo al asignar memoria! 270\n");
exit(2);
}
par.prueba[270][0][1] = floor(Mhe/par.cT); //tend //-tbegin del angulo de 90
par.prueba[270][0][0] = -par.prueba[270][0][1]; //tbegin //-tend del angulo de 90
par.param1[270][1] = par.cT * par.Bn; //xinc
par.param1[270][2] = 0; //yinc
par.prueba[270][0][2] = floor(Cx * par.Bn + par.param1[270][1] * par.prueba[270][0][0] + 0.5) + par.Bn/2; //xb
//----------- Calcular φ = 270 --------------//
for(j=1;j<=par.param1[270][0];j++){
pj = -Np * cP + (j - 1) * cP; //aquí va de -NpcP hasta NpcP
par.prueba[270][j-1][3] = floor((Cy - pj) * par.Bn + 0.5) + par.Bn/2; //se guarda cada valor de cada línea //yb
if(j!=par.param1[270][0]){
par.prueba[270][j] = (int *)malloc( 4 * sizeof ( int ) );
if(par.prueba[270][j]==NULL){
printf("¡Fallo al asignar memoria! 270 %d\n",j);
exit(2);
}
}
}
//----------- Precomputar y calcular 270 < φ < 360 --------------//
for(;phi<360;phi++){
Pmax = Mhe * fabs(cos(phi)) + Nhe * fabs(sin(phi));
Np = floor(Pmax/cP);
par.param1[phi][0] = 2 * Np + 1; //NP
par.prueba[phi] = (int **)malloc( par.param1[phi][0]* sizeof ( int * ) );
if(par.prueba[phi]==NULL){
printf("¡Fallo al asignar memoria! %d\n",phi);
exit(2);
}
par.prueba[phi][0] = (int *)malloc( 4 * sizeof ( int ) );
if(par.prueba[phi][0]==NULL){
printf("¡Fallo al asignar memoria! %d\n",phi);
exit(2);
}
par.param1[phi][1] = -(par.cT*sin(phi)*par.Bn); //xinc
par.param1[phi][2] = par.cT*cos(phi)*par.Bn; //yinc
for(j=1;j<=par.param1[phi][0];j++){
pj = -Np * cP + (j - 1) * cP;
t1y = (Mhe + (pj * cos(phi)))/sin(phi);
t1x = -((Nhe + (pj * sin(phi)))/cos(phi));
t2y = -((Mhe - (pj * cos(phi)))/sin(phi));
t2x = (Nhe - (pj * sin(phi)))/cos(phi);
par.prueba[phi][j-1][0] = ceil(fmax(t1y,t1x)/par.cT); //tbegin
par.prueba[phi][j-1][1] = floor(fmin(t2y,t2x)/par.cT); //tend
par.prueba[phi][j-1][2] = floor((Cx + pj * cos(phi)) * par.Bn + 0.5 + (par.param1[phi-1][1] * par.prueba[phi][j-1][0])) + par.Bn/2; //xb
par.prueba[phi][j-1][3] = floor((Cy + pj * sin(phi)) * par.Bn + 0.5 + (par.param1[phi-1][2] * par.prueba[phi][j-1][0])) + par.Bn/2; //yb
if(j!=par.param1[phi][0]){
par.prueba[phi][j] = (int *)malloc( 4 * sizeof ( int ) );
if(par.prueba[phi][j]==NULL){
printf("¡Fallo al asignar memoria! %d %d\n",phi,j);
exit(2);
}
}
}
}
}
void computar_transformadas(GtkWidget *widget, GdkEventButton *event, gpointer callback_data){
int tendbegin;
int xi;
int yi;
int ti;
int phi;
int j;
int o;
int sale=0;//variable para salir del for de tendbegin en los casos especiales
int entra = 0;
for(phi = 0; phi < 360;phi++){ //todos los ángulos
e = par.cT;
sale = 0;
par.pixels[phi] = (int ***)malloc( par.param1[phi][0]* sizeof ( int ** ) );
if(par.pixels[phi]==NULL){
printf("¡Fallo al asignar memoria! (pixels) %d\n",phi);
exit(2);
}
for(j=0; (sale != 1); j++){ //todas las líneas
tendbegin = par.prueba[phi][j][1]-par.prueba[phi][j][0];
par.pixels[phi][j] = (int **)malloc( tendbegin * sizeof ( int * ) );
if(par.pixels[phi][j]==NULL){
printf("¡Fallo al asignar memoria! (pixels) %d %d\n",phi,j);
exit(2);
}
if(phi==0 || phi==90 || phi==180 || phi==270 || phi==360 || j==(par.param1[phi][0]-1)){
sale = 1;
}
entra=0;
for(o=0; o < tendbegin;o++){
par.pixels[phi][j][o] = (int *)malloc( 2 * sizeof ( int ) );
if(par.pixels[phi][j][o]==NULL){
printf("¡Fallo al asignar memoria! (pixels) %d %d %d\n",phi,j,o);
exit(2);
}
ti = (par.prueba[phi][j][0] + o)*par.cT;
xi = par.prueba[phi][j][2] + floor(o*par.param1[phi][1]);
yi = par.prueba[phi][j][3] + floor(o*par.param1[phi][2]);
par.pixels[phi][j][o][0] = floor(xi/par.Bn);//ii//para enviarlos a las funcionales se guardan en matriz
par.pixels[phi][j][o][1] = floor(yi/par.Bn);//ji
if(o == (tendbegin/2)){
entra=1;
}
if(o==tendbegin-1){
suma(par.pixels[phi][j],o, entra);
}
}
}
}
int y;
int t;
int p;
for(y=0;y<360;y++){
for(t=0;t<par.param1[y][0];t++){
free(par.prueba[y][t]);
for(p=0;p<4;p++){
free(par.pixels[y][t][p]);
}
free(par.pixels[y][t]);
}
free(par.prueba[y]);
free(par.pixels[y]);
}
gdk_pixbuf_save(pixbuf,"/path/to/image/prueba.png","png",NULL,NULL);
}
void suma(int** trazo,int o,int entra){
int i;
int gris;
int temp=0;
for(i=0;i<o;i++){
p = pixs + trazo[i][1]/*y*/ * rowstride + trazo[i][0]/*x*/ * n_channels;//para colocarse en la imagen
gris=0.2989 * p[0] + 0.5870 * p[1] + 0.1140 * p[2]; //para transofrmar de RGB a escala de gris
if(entra ==1){
p[0]=(guchar)50;
p[1]=(guchar)0;
p[2]=(guchar)0;
}
entra = 0;
temp=rowstride;
}
}
void ClickCallback(GtkWidget *widget, GdkEventButton *event, gpointer callback_data)
{
/*-- Create the selector widget --*/
file_selection_box = gtk_file_chooser_dialog_new("Porfavor seleccione un archivo.",NULL,GTK_FILE_CHOOSER_ACTION_OPEN,GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
if (gtk_dialog_run (GTK_DIALOG (file_selection_box)) == GTK_RESPONSE_ACCEPT)
{
// char *filename;
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_selection_box));
open_file(filename);
g_free (filename);
}
gtk_widget_destroy (file_selection_box);
}
static void destroy_event(GtkWidget *widget, gpointer data)
{
gtk_main_quit();
}
static gboolean delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
return FALSE; // must return false to trigger destroy event for window
}
First you need to allocate array for pointers to arrays (char **), then you need alocate each “row” one by one.
To free you will need to do this backwards, eg free each line and then free array of pointers:
Basically, these are not really “true arrays” (by C definition), but instead of having 2D array as whole, you got one array that holds it together (type **) and multiple (type *) arrays that holds informations stored in “row”.
Heres example of 3×3 array, –> stands for (points to).