Похожие чаты

So it doesn't make sense to dynamically allocate a pointer

and then assign it null right?

17 ответов

14 просмотров

what are you talking about? you should free the memory first before assigning it to null

none- Автор вопроса
Manav | avoid unnecessary messaging me
what are you talking about? you should free the me...

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".

none- Автор вопроса
Manav | avoid unnecessary messaging me
null ptr is a pointer which points to the address ...

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?

none
ohh god i just wanted to initialize a newly dynami...

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

none
Cool thanks

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.

none
If this is the case int *ptr = (int*)malloc(sizeof...

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

none
very clearly explained 👌

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

none- Автор вопроса

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.

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта