Похожие чаты

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

and then assign it null right?

17 ответов

13 просмотров

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.

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

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

а через ESC-код ?
Alexey Kulakov
29
30500 за редактор? )
Владимир
47
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
Чёт не понял, я ж правильной функцией воспользовался чтобы вывести отладочную информацию? но что-то она не ловится
notme
18
Привет)) уже кажется эту тему перемусолили, но вот я так и не понял. Я сейчас сижу на 27дюймов 2к мониторе. На Актуальной макоси, если я куплю 27д 4к монитор: - будет ли изобр...
Vladislav Piskunov
15
Hi guys, any problem with Pulsebrige? Trying to transfer from wETH to ETH. First it tells me to connect my metamask "through mobile app" not desktop. Then I did and confirmed ...
Snowflakecrypto
13
any reference of this implementation?
BitBuddha
29
У меня есть функция где происходит это: write_bit(buffer, 1); write_bit(buffer, 0); write_bit(buffer, 1); write_bit(buffer, 1); write_bit(buffer, 1); w...
~
13
Страшнейшая правда про списки ЦБ. С первых дней жизни P2P сферы, молодые человеки, начитавшись законодательной базы и "внутренних" документов, решили, что им противостоит сер...
Foxcool
3
&"C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.42.34433\bin\Hostx64\x64\dumpbin.exe" /EXPORTS C:\Users\Yeet\Tauon\vcpkg\installed\x64-window...
Martin Rys
6
Карта сайта