-
Array for malloc needed
I am trying to allocate the memory of 1KB of size to two variables. I treied with for loop, but i am getting segmentation fault error. I have attached my sample code below. can anyone help to find why that error occurs and whats the solution for my problem
Code:
#include<stdio.h>
#include<stdlib.h>
main()
{
int i,*par[i];
for(i=0;i<=2;i++)
{
par[i]=(int *)malloc(1024);
printf("\n%u",par[i]);
}
free(par);
}
"Thanks in advance"
-
i is not initialized.iterates 3 times.
Code:
printf("\n%u",par[i]);
%u is for unsigned ints, not pointers.
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
you don't free your mallocs.
http://faq.cprogramming.com/cgi-bin/...&id=1043284376
-
int *par[i];
declares array of unknown size
int* par[2]; - declares array of 2 pointers
-
This is NOT supposed to be free()ed. Each element of the array should be free()ed, however.