What should pointers be initialized to




















Check msdn to learn about nullable types. I am asking what exactly is going on in the memory? Isn't the pointer variable 's' a local variable? Yes or where is the pointer variable stored in the memory?

The local s is in a register or on the stack. Also where is the text constant "Hello, World" stored in memory? Arseny Istlentyev. Only authorized users can answer the question. Please sign in first, or register a free account. Not the answer you're looking for? Related Answers. Strings may be allocated uninitialized with malloc.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Ask Question.

Asked 9 years, 2 months ago. Active 3 years, 3 months ago. Viewed 24k times. Improve this question. A: No, you don't have to.

But it's an extremely good habi t: — paulsm4. It is necessary only when you expect it to have a default value. As pointers, just like other variables will hold garbage value unless it is initialized. Add a comment. Active Oldest Votes. Improve this answer. Levon Levon k 32 32 gold badges silver badges bronze badges.

I am confused in understanding when a pointer should be initialized and when should we allocate memory for that pointer for example a structure pointer — Bionix If you know you're going to overwrite its value to point to somewhere legit then it is kind of fine Same concept when you reference freed memory. FYI, dereferencing a null pointer is undefined behavior. I don't mean to be obtuse, but can you clarify what you mean by the "correct" assignment?

I read your post as saying that, as long as I'm only using file pointers in the way I stated, there is no need to initialize them to NULL After initializing my pointers to NULL, my code seems to run fine with my calls to fclose [although this initialization unfortunately hasn't fixed my buggy values]. As someone mentioned, earlier, a lot of code is written to expect NULL to mean an uninitialized value.

A function might do this: Code:. Originally Posted by Adak. Making an assignment of a pointer to null, is no help to me, so far - the program is still not going to work reliably, until you make the CORRECT assignment. All before you dereference the pointer. Last edited by Elysia; at PM.

It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions. Originally Posted by Salem. You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much. Outside of your DOS world, your header file is meaningless. Thanks, Elysia. That was a very straightforward analysis. Yes, of course just like you need to correctly initialize other type variables. No, it's not necessary to set to 0 after calling fclose.

C Language Pointers Initializing Pointers. Example Pointer initialization is a good way to avoid wild pointers. Caution: The result of dereferencing a NULL pointer is undefined, so it will not necessarily cause a crash even if that is the natural behaviour of the operating system the program is running on.



0コメント

  • 1000 / 1000