kinda wierd stuff with pointers...
I'm trying to learn how to do dynamic allocation stuff, so I'm trying to make a program that uses this. I don't get any errors when I compile, and none of the malloc checks print anything, but apparantly the program never gets to my printf line that comes before all the free() calls.
Nothing prints except for the first line.
Code:
#include "windows.h"
#include "stdio.h"
#include "time.h"
#include "math.h"
#include "string.h"
typedef struct UNIT{
void **p;
int pS;
}UNIT;
UNIT **u;
int uS=10;
int l[10];
void main(void){
printf("Only line that prints");
u=malloc(uS*sizeof(UNIT *));
if(u==NULL){
printf("\n\tu=malloc failed");
return;
}
for(l[0]=0;l[0]<uS;l[0]++){
(*u[l[0]]).pS=10;
(*u[l[0]]).p=malloc((*u[l[0]]).pS*sizeof(void *));
if((*u[l[0]]).p==NULL){
printf("\n\t(*u[%d]).p=malloc failed",(l[0]));
continue;
}
for(l[1]=0;l[1]<(*u[l[0]]).pS;l[1]++){
(*u[l[0]]).p[l[1]]=malloc(64);
if((*u[l[0]]).p[l[1]]==NULL){
printf("\n\t(*u[%d]).p[%d]=malloc failed",(l[0]),(l[1]));
continue;
}
((char *)(*u[l[0]]).p[l[1]])[0]='p';
}
}
printf("Why doesn't this print?"); // doesn't print
printf("a thing = %c",(((char *)(*u[3]).p[3])[0])); // doesn't print either
for(l[0]=0;l[0]<uS;l[0]++){
free((*u[l[0]]).p);
for(l[1]=0;l[1]<(*u[l[0]]).pS;l[1]++){
free((*u[l[0]]).p[l[1]]);
}
}
free(u);
}