null-pointer

Assigning a reference by dereferencing a NULL pointer

半城伤御伤魂 提交于 2019-11-28 09:13:10
int& fun() { int * temp = NULL; return *temp; } In the above method, I am trying to do the dereferencing of a NULL pointer. When I call this function it does not give exception. I found when return type is by reference it does not give exception if it is by value then it does. Even when dereferencing of NULL pointer is assinged to reference (like the below line) then also it does not give. int* temp = NULL: int& temp1 = *temp; Here my question is that does not compiler do the dereferencing in case of reference? Dereferencing a null pointer is Undefined Behavior . An Undefined Behavior means

Is (int *)0 a null pointer?

狂风中的少年 提交于 2019-11-28 06:28:50
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 continues: If a null pointer constant is converted to a pointer type, the resulting pointer, called a null

What are the Ruby Win32API Parameters | How do I pass a null pointer?

北城余情 提交于 2019-11-28 06:05:23
问题 I know the following: 'L' - Long 'P' - Pointer 'I' - Integer 'V' - Void My problem is that I can't pass a null pointer when I perform an API call. E.g.: ['L', 'P', 'L'] -> api.call(0, nil, 0) :: ArgumentError: Null pointer given . My question is: Are there more parameter types that I don't know about and what should I do to pass a null pointer as a method parameter? Background I have been searching the internet for native Ruby programming examples of WinForms-based applications. I have

Is performing arithmetic on a null pointer undefined behavior?

风流意气都作罢 提交于 2019-11-28 00:41:59
It looks to me like the following program computes an invalid pointer, since NULL is no good for anything but assignment and comparison for equality: #include <stdlib.h> #include <stdio.h> int main() { char *c = NULL; c--; printf("c: %p\n", c); return 0; } However, it seems like none of the warnings or instrumentations in GCC or Clang targeted at undefined behavior say that this is in fact UB. Is that arithmetic actually valid and I'm being too pedantic, or is this a deficiency in their checking mechanisms that I should report? Tested: $ clang-3.3 -Weverything -g -O0 -fsanitize=undefined

Fortran array of derived types and memory leaks despite finalization

折月煮酒 提交于 2019-11-27 16:16:31
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 subroutine TCoordinateDel(self) type(TCoordinate),intent(inout) :: self if(associated(self%Coordinate)

Uninitialized pointers in code

北城以北 提交于 2019-11-27 12:04:14
I am learning C++ and I came to know that pointers if left uninitialized could point to random locations in memory and create problems that memory might be used by some other program. Now if that is the case we should never have this line in any part of our code: int* ptr; Instead we should have something like int* ptr = NULL; //Is this going to avoid the problem Please suggest because I have seen the first line( int* ptr; ) in many books so I am getting this doubt. If possible give some examples also. Doug T. int* ptr = NULL; //Is this going to avoid the problem This will cause ptr to point

Can't find @Nullable inside javax.annotation.*

橙三吉。 提交于 2019-11-27 09:54:26
问题 I want use @Nullable annotation to eliminate NullPointerExceptions . I found some tutorials on the net, I noticed that this annotation comes from the package javax.annotation.Nullable ; but when I import it a compilation error is generated: cannot find symbol 回答1: You need to include a jar that this class exists in. You can find it here If using Maven, you can add the following dependency declaration: <dependency> <groupId>com.google.code.findbugs</groupId> <artifactId>jsr305</artifactId>

How do we check if a pointer is NULL pointer?

百般思念 提交于 2019-11-27 08:33:33
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? I always think simply if(p != NULL){..} will do the job. It will. James Kanze 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 pointers; it introduces invalid pointers; pointers which, at least as far as the standard is concerned,

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

爱⌒轻易说出口 提交于 2019-11-27 07:09:48
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 ? 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 that Where an argument declared as size_t n specifies the length of the array for a function, n can have the

Is performing arithmetic on a null pointer undefined behavior?

…衆ロ難τιáo~ 提交于 2019-11-27 04:45:51
问题 It looks to me like the following program computes an invalid pointer, since NULL is no good for anything but assignment and comparison for equality: #include <stdlib.h> #include <stdio.h> int main() { char *c = NULL; c--; printf("c: %p\n", c); return 0; } However, it seems like none of the warnings or instrumentations in GCC or Clang targeted at undefined behavior say that this is in fact UB. Is that arithmetic actually valid and I'm being too pedantic, or is this a deficiency in their