and then assign it null right?
what are you talking about? you should free the memory first before assigning it to null
ohh god i just wanted to initialize a newly dynamically allocated pointer with something, so I thought to do it with null. Just like we initialize int a = 0. But now i realise null literally means "no valid memory location".
null ptr is a pointer which points to the address 0
If this is the case int *ptr = (int*)malloc(sizeof(int)); ptr = NULL. Will ptr be called a null pointer? Does it still hold the address it was allocated on heap? Can ptr after being nullified still go back and become equal to heap address?
you could initialize it to zero by *ptr = {}; but this is limited as it won't work with arrays, etc.
ptr will br a null pointer, the memory you assigned will remain there (it will not be freed) until the end of the program. Even if you try to free it, you will have a hard time as you have no handle to the memory region to free it
you should still check in case of arrays, i am quite sure it should not happen but it will not be surprising if it does zero the whole array.
very clearly explained 👌
1. will ptr be a null pointer? Yes as it 0, 2. Does it still hold the address..? No it doesn't, it used to store the address of the start of the memory region which was assigned but by doing ptr = NULL you overwrote it to 0, so you have a case where memory allocated by malloc is still there but nothing points to it 3 can ptr after being nullified go back to heap address? yes it definitely can if you still had the address of the memory region assigned by malloc somewhere you can do ptr = address or you can have ptr point to new memory assigned by malloc ptr = malloc(..); Don't do either of these, in the first case you can have dangling pointer while in the 2nd case it's always better to use a new pointer instead of using an existing pointer
you seem to forget the most important part that pointers store addresses, that is what it means by pointing to something. Keep reminding it to yourself and you'll have less doubts
you are a saviour ...thankyou very very much
By pointing to the first* of something accurately
not really, you can use pointers to move along buffers of arbitrary sizes. In that case the pointer won't be pointing to the beginning of the buffer.
It's pointing to the first of buffer by default
no, you can start at the end and go to the font.
Обсуждают сегодня