D
dragoncoder
Please have a look at the following code snippet.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void* func ( int i )
{
void* ptr = malloc(sizeof(int));
memset(ptr, i, sizeof(int));
return ptr;
}
int main()
{
int i = 123;
int res = *((int*)func(i));
printf("i is %d\n", res);
return 0;
}
[l34@premier rnd]$ gcc p1.c
[l34@premier rnd]$ ./a.out
i is 2071690107
I don't understand what the problrm is, as I was expecting 123 to get
printed. Can somebody tell why this garbage is being printed.
Thanks.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void* func ( int i )
{
void* ptr = malloc(sizeof(int));
memset(ptr, i, sizeof(int));
return ptr;
}
int main()
{
int i = 123;
int res = *((int*)func(i));
printf("i is %d\n", res);
return 0;
}
[l34@premier rnd]$ gcc p1.c
[l34@premier rnd]$ ./a.out
i is 2071690107
I don't understand what the problrm is, as I was expecting 123 to get
printed. Can somebody tell why this garbage is being printed.
Thanks.