null-pointer

Is (int *)0 a null pointer?

假装没事ソ 提交于 2019-11-27 01:07:41
问题 This could be thought of as an extension to this question (I'm interested in C only, but adding C++ to complete the extension) The C11 standard at 6.3.2.3.3 says: An integer constant expression with the value 0, or such an expression cast to type void * , is called a null pointer constant. What my take on this personally is that 0 and (void *)0 represent the null pointer, whose integer value may not actually be 0, but that doesn't cover 0 cast to any other type. But, the standard then

Calling a method on an uninitialized object (null pointer)

别来无恙 提交于 2019-11-27 00:30:51
What is the normal behavior in Objective-C if you call a method on an object (pointer) that is nil (maybe because someone forgot to initialize it)? Shouldn't it generate some kind of an error (segmentation fault, null pointer exception...)? If this is normal behavior, is there a way of changing this behavior (by configuring the compiler) so that the program raises some kind of error / exception at runtime? To make it more clear what I am talking about, here's an example. Having this class: @interface Person : NSObject { NSString *name; } @property (nonatomic, retain) NSString *name; - (void

What is a void pointer and what is a null pointer?

不羁岁月 提交于 2019-11-27 00:26:39
So I was going through some interview questions and I came across one about void and null pointers , which claims: a pointer with no return type is called a null pointer. It may be any kind of datatype. This confused me thoroughly! It seems void and null could be used interchangeably according to this question, and I don't believe that to be correct. I assumed void to be a return type and null to be a value. But I am just a code-rookie and am not sure I am right. Please express your views as to what a null pointer is and a void pointer is. I am not looking for difference between null and void.

Does not evaluating the expression to which sizeof is applied make it legal to dereference a null or invalid pointer inside sizeof in C++?

别等时光非礼了梦想. 提交于 2019-11-26 20:47:21
First of all, I've seen this question about C99 and the accepted answer references operand is not evaluated wording in the C99 Standard draft. I'm not sure this answer applies to C++03. There's also this question about C++ that has an accepted answer citing similar wording and also In some contexts, unevaluated operands appear. An unevaluated operand is not evaluated. wording. I have this code: int* ptr = 0; void* buffer = malloc( 10 * sizeof( *ptr ) ); The question is - is there a null pointer dereference (and so UB) inside sizeof() ? C++03 5.3.3/1 says The sizeof operator yields the number

Can I use if (pointer) instead of if (pointer != NULL)?

烂漫一生 提交于 2019-11-26 19:38:21
Is it safe to check a pointer to not being NULL by writing simply if(pointer) or do I have to use if(pointer != NULL) ? You can; the null pointer is implicitly converted into boolean false while non-null pointers are converted into true. From the C++11 standard, section on Boolean Conversions: A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool . A zero value, null pointer value, or null member pointer value is converted to false ; any other value is converted to true . A prvalue of type std::nullptr_t can be converted to

Is dereferencing null pointer valid in sizeof operation [duplicate]

主宰稳场 提交于 2019-11-26 19:06:08
This question already has an answer here: Why doesn't my program seg fault when I dereference a NULL pointer inside of malloc? 4 answers I've come across a snippet of code that to me should crash with a segmentation fault , and yet it works without a hitch. The code in question plus relevant data structure is as follows (with associated comment found right above): typedef struct { double length; unsigned char nPlaced; unsigned char path[0]; } RouteDefinition* Alloc_RouteDefinition() { // NB: The +nBags*sizeof.. trick "expands" the path[0] array in RouteDefinition // to the path[nBags] array

Fortran array of derived types and memory leaks despite finalization

↘锁芯ラ 提交于 2019-11-26 18:36:08
问题 I defined a derived type and encountered some problems with memory deallocation although I had written the final procedure. The code is as follows module ModuleCoordinate implicit none type :: TCoordinate real(8),dimension(:),pointer :: Coordinate => NULL() contains procedure :: TCoordinateAssignment generic,public :: Assignment(=) => TCoordinateAssignment final :: TCoordinateDel end type TCoordinate interface TCoordinate module procedure :: TCoordinateInit end interface TCoordinate contains

How do we check if a pointer is NULL pointer?

自闭症网瘾萝莉.ら 提交于 2019-11-26 17:45:56
问题 I always think simply if(p != NULL){..} will do the job. But after reading this Stack Overflow question, it seems not. So what's the canonical way to check for NULL pointers after absorbing all discussion in that question which says NULL pointers can have non-zero value? 回答1: I always think simply if(p != NULL){..} will do the job. It will. 回答2: First, to be 100% clear, there is no difference between C and C++ here. And second, the Stack Overflow question you cite doesn't talk about null

Is it guaranteed to be safe to perform memcpy(0,0,0)?

那年仲夏 提交于 2019-11-26 13:06:19
问题 I am not so well-versed in the C standard, so please bear with me. I would like to know if it is guaranteed, by the standard, that memcpy(0,0,0) is safe. The only restriction I could find is that if the memory regions overlap, then the behavior is undefined... But can we consider that the memory regions overlap here ? 回答1: I have a draft version of the C standard (ISO/IEC 9899:1999), and it has some fun things to say about that call. For starters, it mentions (§7.21.1/2) in regards to memcpy

Does not evaluating the expression to which sizeof is applied make it legal to dereference a null or invalid pointer inside sizeof in C++?

 ̄綄美尐妖づ 提交于 2019-11-26 07:45:58
问题 First of all, I\'ve seen this question about C99 and the accepted answer references operand is not evaluated wording in the C99 Standard draft. I\'m not sure this answer applies to C++03. There\'s also this question about C++ that has an accepted answer citing similar wording and also In some contexts, unevaluated operands appear. An unevaluated operand is not evaluated. wording. I have this code: int* ptr = 0; void* buffer = malloc( 10 * sizeof( *ptr ) ); The question is - is there a null