Allocating memory to a pointer in C - error when incrementing and decrementing -


int main() {         int *p;       p = (int*)malloc(sizeof(int));     p++;     printf("%d",*p);     p--;free(p);     return 0; } 

could explain why program produces error when free(t) executed ? noticed program doesn't give error when printf("%d",*t); missing.

first declare t;

and don't free(t);it integer variable. free(p); pointer allocated memory.

or-

may wrote t instead of p in program did not declared t.

edit

but if chage t p

printf("%d",*p); 

will give undefined behaviour because in program have not initialized p.

first initialize p.


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -