精华内容
下载资源
问答
  • When we use "... to pass a pointer to a function, only a copy of the pointer is passed to the function. We can say "pass by pointer" is passing a pointer by value. In most cases, th...

          When we use "pass by pointer" to pass a pointer to a function, only a copy of the pointer is passed to the function. We can say "pass by pointer" is passing a pointer by value. In most cases, this does not present a problem. But the problem comes when you modify the pointer inside the function. Instead of modifying the variable, you are only modifying a copy of the pointer and the original pointer remains unmodified, that is, it still points to the old variable. 

    展开全文
  • 【转载】原文链接 ...Wild Pointer , NULL Pointer , Generic Pointer and Dangling Pointer in C Posted by: Admin Categories: C Wil

    【转载】原文链接 http://www.everybrickmatters.com/2013/11/wild-pointer-null-pointer-generic.html

    Wild Pointer , NULL Pointer , Generic Pointer and Dangling Pointer in C
    Posted by: Admin Categories: C


    Wild pointer:
    A pointer in c which has not been initialized is known as wild pointer.

    Example:
    # include<stdio.h>
    int main(){
    int *ptr;
    printf("%u\n",ptr); 
    printf("%d",*ptr);
    return 0;
    }

    Output: 
    Any address
    Garbage value


    NULL pointer:
    NULL pointer is a pointer which is pointing to nothing. NULL pointer points the base address of segment.

    Examples of NULL pointer:

    1. int *ptr=(char *)0;
    2. float *ptr=(float *)0;
    3. char *ptr=(char *)0;
    4. double *ptr=(double *)0;
    5. char *ptr=’\0’;
    6. int *ptr=NULL;


    We cannot copy any thing in the NULL pointer.

    Example: What will be output of following c program?

    #include <string.h>
    #include <stdio.h>

    int main(){
    char *str=NULL;
    strcpy(str,"Everybrickmatters");
    printf("%s",str);
    return 0;
    }

    Output: (null)

    Difference between NULL Pointer and Wild Pointer 
    There is difference between the NULL pointer and wild pointer. Null pointer points the base address of segment while wild pointer doesn’t point any specific memory location.


    Generic Pointer 
    Void pointer in c is known as generic pointer. Literal meaning of generic pointer is a pointer which can point type of data.

    Example:
    void *ptr;
    Here ptr is generic pointer.

    Important points about generic pointer in c?

    1. We cannot dereference generic pointer.


    #include<stdio.h>
    #include <malloc.h>
    int main(){
    void *ptr;
    printf("%d",*ptr);
    return 0;
    }

    Output: Compiler error

    2. We can find the size of generic pointer using sizeof operator.

    #include <string.h>
    #include<stdio.h>
    int main(){
    void *ptr;
    printf("%d",sizeof(ptr));
    return 0;
    }


    Output: 2
    Explanation: Size of any type of near pointer in c is two byte.


    3. Generic pointer can hold any type of pointers like char pointer, struct pointer, array of pointer etc without any typecasting.

    Example:
    #include<stdio.h>
    int main(){
    char c='A';
    int i=4;
    void *p;
    char *q=&c;
    int *r=&i;
    p=q;
    printf("%c",*(char *)p);
    p=r;
    printf("%d",*(int *)p);
    return 0;
    }

    Output: A4

    4. Any type of pointer can hold generic pointer without any typecasting.

    5. Generic pointers are used when we want to return such pointer which is applicable to all types of pointers. For example return type of malloc function is generic pointer because it can dynamically allocate the memory space to stores integer, float, structure etc. hence we type cast its return type to appropriate pointer type.

    Examples:
    1.char *c;
       c=(char *)malloc(sizeof(char));

    2.double *d;
       d=(double *)malloc(sizeof(double));

    3.Struct student{
       char *name;
       int roll;
       };
       Struct student *stu;
       Stu=(struct student *)malloc(sizeof(struct student));


    Dangling pointer:
    If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.

    Initially ptr points to a .

    Now a has been deleted from the memory but ptr is still pointing to the same . This is dangling pointer .

    See the following example of dangling pointer:

    #include<stdio.h>
    int *call();

    void main(){
    int *ptr;
    ptr=call();
    fflush(stdin);
    printf("%d",*ptr);
    }

    int * call(){
    int x=25;
    ++x;
    return &x;
    }

    Output: Garbage value

    How to solve the problem of Dangling Pointer ? 
    Solution of this problem: Make the variable x is as static variable.
    In other word we can say a pointer whose pointing object has been deleted is called dangling pointer.

    See the following example :
    #include<stdio.h>
    int *call();


    void main(){
    int *ptr;
    ptr=call();
    fflush(stdin);
    printf("%d",*ptr);
    }

    int * call(){
    static int x=25;
    ++x;
    return &x;
    }

    Output: 26

    展开全文
  • from: http://www.codeproject.com/Articles/4894/Pointer-to-Pointer-and-Reference-to-Pointer Introduction This article explains the reason behind using pointer-to-pointer and reference-to-

    from: http://www.codeproject.com/Articles/4894/Pointer-to-Pointer-and-Reference-to-Pointer


    Introduction

    This article explains the reason behind using pointer-to-pointer and reference-to-pointer to modify a pointer passed to a function, so as to understand their usage better. For brevity, I use the terms, ptr-to-ptr and ref-to-ptr to represent them respectively. In this article, I'm not going to discuss how to use ptr-to-ptr as a 2 dimensional array or array of pointers. Please note we can use ptr-to-ptr in both C and C++ but we can use ref-to-ptr only in C++.

    Why We Need Them?

    When we use "pass by pointer" to pass a pointer to a function, only a copy of the pointer is passed to the function. We can say "pass by pointer" is passing a pointer by value. In most cases, this does not present a problem. But problem comes when you modify the pointer inside the function. Instead of modifying the variable, you are only modifying a copy of the pointer and the original pointer remains unmodified, that is, it still points to the old variable. The code below demonstrates this behavior.

    //global variable
    int g_One=1;
    //function prototype
    
    void func(int* pInt);
    
    int main()
    {
      int nvar=2;
      int* pvar=&nvar;
      func(pvar);
      std::cout<<*pvar<<std::endl;//Will still show 2
    
      return 0;
    }
    
    void func(int* pInt)
    {
      pInt=&g_One;
    }

    Syntax of Pointer to Pointer

    This is how you called the function with ptr-to-ptr parameter.

    //function prototype
    void func(int** ppInt);
    
    int main()
    {
      int nvar=2;
      int* pvar=&nvar;
      func(&pvar);
      ....
      return 0;
    }

    Let us see how it to modify the ptr-to-ptr in the function.

    void func(int** ppInt)
    {
      //Modify the pointer, ppInt points to
      *ppInt=&g_One;
    
      //You can also allocate memory, depending on your requirements
      *ppInt=new int;
    
      //Modify the variable, *ppInt points to
      **ppInt=3;
    }

    Let me surmarise what all those dereferencing are,

    • ppInt is the ptr-to-ptr. We will never modify this because if we do, we'll lose our grip on the address of the pointer it is pointing to.

    • *ppInt is the pointed pointer. If we modify this, we are modifying the contents of the pointed pointer, which is an address and in the above example, pvar. In other words, we are effectively modifying whatpvar points to.

    • **ppInt is the dereferenced twice variable which is what pvar points to.

    Syntax of Reference to Pointer

    Now let us look at how you call the function with ref-to-ptr parameter

    //function prototype
    void func(int*& rpInt);
    
    int main()
    {
      int nvar=2;
      int* pvar=&nvar;
      func(pvar);
      ....
      return 0;
    }

    Let us see how it to modify the ref-to-ptr in the function.

    void func(int*& rpInt)
    {
      //Modify what rpInt and pvar is pointing to, to g_One
      rpInt=&g_One;
    
      //You can also allocate memory, depending on your requirements
      rpInt=new int;
    
      //Modify the variable rpInt points to
      *rpInt=3;
    }

    You may wonder whether, in the above func(), the parameter rpInt is pointer to reference. Just take my word for it that it is called ref-to-ptr and it is ref-to-ptr.

    Let me once again summarize what all those dereferencing are,

    • rpInt is the reference for the pointer, pvar in the above example.

    • *rpInt dereferences what pvar point to, so you get the variable the pointer, pvar is pointing to.

    Syntax of Tracking Reference to a Handle(C++/CLI)

    Let us see how to modify a handle in C++/CLI function using the "tracking reference to a handle" parameter. This C++/CLI handle has nothing to do with Win32 HANDLEs and this handle is a reference to a managed object on the CLI heap. I use an object this time, instead of a Plain Old Data(POD)/primitive data/value type because you can only change a reference to an object.

    //function prototype
    void func(ClassA^% thObj);
    
    int main()
    {
      ClassA^ obj = gcnew ClassA;
      ClassA^ obj2=&obj;
      func(obj2);
      ....
      return 0;
    }
    void func(ClassA^% thObj)
    {
      //Modify what obj2 is referencing, to g_obj
      thObj=g_obj; // g_obj is a global object.
      //You can instantiate a new class
      thObj=gcnew ClassA();
      //Modify the variable thObj is referencing, through a its member function, SetInt().
      thObj->SetInt(3);
    }
    • thObj is the tracking reference for the handle, obj2 in the above example.

    Preference of one over the other?

    Now we have seen the syntax of ptr-to-ptr and ref-to-ptr. Are there any advantages of one over the other? I am afraid, no. The usage of one of both, for some programmers are just personal preferences. Some who use ref-to-ptr say the syntax is "cleaner" while some who use ptr-to-ptr, say ptr-to-ptr syntax makes it clearer to those reading what you are doing.

    Do not Mistake Pointer to Pointer Arguments

    Do not mistake every ptr-to-ptr arguments as purely ptr-to-ptr. An example would be when some write int main(int argc, char *argv[]) as int main(int argc, char **argv) where **argv is actually an array of pointers. Be sure to check out the library documentation first!

    Reference to Pointer type (RTTI)

    You cannot use RTTI to find out the type of ref-to-ptr. As typeid() does not support reference types.

    void test(int*& rpInt)
    {
      std::cout << "type of *&rpInt: " << typeid(rpInt).name() 
        << std::endl;//will show int *
    
    }

    What are other alternatives?

    If you find that the ptr-to-ptr and ref-to-ptr syntax are rather hard to understand, you can just use the "return the pointer" method.

    ClassA* func()
    {
      ClassA* p = new ClassA();
      // do my things with p
      // ...
      return p;
    }

    Conclusion

    You may ask if you would ever use ptr-to-ptr and ref-to-ptr in your projects and if it is necessary to know about them. Well, as developers, we use libraries and technologies developed by others. One example would be COM uses ptr-to-ptr to return an interface pointer using CoCreateInstance() andIUnknown::QueryInterface(). Up to some point in your developer career, you are definitely going to come across them. It is good to know them.


    展开全文
  • pointer(struct)时就会提示要输入c的类型,但是POINTER()就不会
  • ctypes 指针类型 byref pointer POINTER

    千次阅读 2019-12-28 21:49:52
    byref 并不会构造一个指针对象,因此速度比 pointer 快,只能作为导出函数的实参使用。其 _obj 是只读属性,不能更改。 pointer 会构造一个指针对象。.contents 属性即其所指的对象,但是指向是能够变动的。相当于 ...

    byref 并不会构造一个指针对象,因此速度比 pointer 快,只能作为导出函数的实参使用。其 _obj 是只读属性,不能更改。

    pointer 会构造一个指针对象。.contents 属性即其所指的对象,但是指向是能够变动的。相当于

    int *ptr = n;
    ptr = &m;  //指向可以变化

    对于一个包含如下函数,名为 lib.so 动态库:

    void change(float* ptr, float value)
    {
    	*ptr = value;
    }
    

     ptr.contents 更改指针的指向,change 函数调用后,c的值并没有变化,变化的是 ptr.contents 的值。

    ctypes.addressof 返回 C 实例的地址,可以查看区别:

    ctypes.addressof(ptr.contents)
    
    ctypes.addressof(c) 

    POINTER 返回类型对象,用来给 restype 和 argtypes 指定函数的参数和返回值的类型用。

    ctypes.POINTER(ctypes.c_float) == type(ptr)  #True ptr的类型可通过 POINTER 获得

    如果不指定,默认的类型都将被当做是整形。实参类型(除了 None,integer,stringbytes,可隐式转换)和返回值的类型用 ctypes.c_ 系列类型显示的指定。(见ctypes 文档:None, integers, bytes objects and (unicode) strings are the only native Python objects that can directly be used as parameters in these function calls.)

    实际上,最终得到的返回结果的类型已经由显示指定的ctypes.c_ 类型,转化为了对应的 python 类型。比如指定 .restype = c_char_p,函数的返回值的类型将是 bytes 类型。

    根据 ctypes 官方文档的描述:ctypes实例由一个内存块和若干描述符构成,描述符用来访问内存块。内存块并不保存 python 对象,而仅仅保存 python 对象的内容。外部访问保存的内容时,都会构造一个新的 python 对象。

    所以

    c_instanc.value is c_instanc.value   #False
    c_instance.content is c_instance.content  #False

     

     

    展开全文
  • SharedPointer

    2019-02-27 09:13:59
    文章目录1 继承关系类图![在这里插入图片描述]...SharedPointer肯定是类模板,我们需要通过计数机制(ref)标识堆内存: 堆内存被指向时:ref++; 指针被置空时:ref–...
  • smart pointer / shared pointer / normal pointer 文章来源:http://stackoverflow.com/questions/417481/pointers-smart-pointers-or-shared-pointers I am programming with normal pointers, but I have ...
  • C++基类和派生类的智能指针转换:static_pointer_cast、dynamic_pointer_cast、const_pointer_cast、reinterpret_pointer_cast 当我们用“裸”指针进行类层次上的上下行转换时,可以使用dynamic_cast。当然我们也...
  • Pointer Network指针网络

    千次阅读 2020-07-25 11:19:07
    Why Pointer Network Structure of Pointer Network How to process language generation by incorporating Pointer Network Get To The Point: Summarization with Pointer-Generator Networks CopyNet Product ...
  • 【论文笔记】Pointer Networks

    万次阅读 2019-12-18 10:03:14
    Pointer Networks TLDR; The authors propose a new architecture called “Pointer Network”. A Pointer Network is a seq2seq architecture with attention mechanism where the output vocabulary is the set of...
  • smart pointer

    2016-03-26 12:13:41
    smart pointer是一种abstract data type,它能够模仿指针的行为,并且额外提供了一系列诸如自动内存管理、边界检查等特性,这些特性是为了在保证效率的基础上较少由于对指针的不正常使用而带来的bug。smart pointer...
  • http://stackoverflow.com/questions/3523145/pointer-arithmetic-for-void-pointer-in-c ...When a pointer to a particular type (say int, char, float, ..) is incremented, its value is increased by the...
  • void pointer

    2014-11-04 14:31:50
    6.13 — Void pointers BY ALEX, ON JULY 19TH, 2007 ...The void pointer, also known as the generic pointer, is a special type of pointer that can be pointed at objects of any data type! A void p
  • 之前设计的SmartPointer的设计方案: (1)指针生命周期结束时主动释放堆空间; (2)一片堆空间最多只能由一个指针标识; (3)杜绝指针运算和指针比较。 思考: 如何实现 SharedPointer 使得多个智能指针对象...
  • Pointer Sentinel Mixture Models TLDR; The authors combine a standard LSTM softmax with Pointer Networks in a mixture model called Pointer-Sentinel LSTM (PS-LSTM). The pointer networks helps with rare ...
  • COBOL Pointer variable

    2016-10-19 22:45:34
    How to define a pointer variable Define a pointer variable in Working-Storage Section 01 SILLY-POINTER USAGE IS POINTER. or 01 POINTERS-AND-OTHER-STUFF. 05 S
  • pointer-event属性详解

    千次阅读 2019-08-13 22:59:40
    一、pointer-event属性可以指定在什么情况下元素可以成为鼠标事件 二、取值: 1、pointer-event:auto;(默认值),对于svg元素,该值与visiblePainted效果相同 2、pointer-event:none;元素不会成为鼠标事件的...
  • Opaque pointer

    2011-11-20 01:24:43
    Opaque pointer From Wikipedia, the free encyclopedia In computer programming, an opaque pointer is a special case of opaque data type, a datatype that is declared to be a pointer...
  • Go sql invalid memory address or nil pointer dereference 文章目录Go sql invalid memory address or nil pointer dereference1、概述2、解决方法原因解决方法 1、概述 执行数据库初始化之后,查询时出现 runtime...
  • pointer-events 鼠标事件控制开关

    千次阅读 2019-08-28 15:25:50
    禁用鼠标事件,设置pointer-events属性为none: pointer-events:none 开启鼠标事件,设置pointer-events为auto: pointer-events:auto
  • 项目中遇到了需要让自定义的按钮元素禁用并置灰,同时又想让置灰按钮的pointer:default,发现即使加了 !important ,pointer:default也不生效,这是由于pointer-events: none;禁用一切点击事件,导致鼠标pointer不...
  • Pointer Networks简介及其应用

    万次阅读 多人点赞 2019-01-14 21:00:11
    本文介绍15年发表在NIPS上的一篇文章:Pointer Networks[1],以及后续应用了Pointer Networks的三篇文章:Get To The Point: Summarization with Pointer-Generator Networks[2]、Incorporating Cop...
  • two pointer算法归纳总结 create: 2018-10-17 19:27:24 记录two pointer算法的应用场景,算法思想。先进行简单归纳,后期有新的想法或者理解再进行追加。&amp;gt; two pointer的基本思想 two pointer算法本质是...
  • frame pointer及其用途

    千次阅读 2017-10-06 13:01:00
    1 什么是frame pointer frame pointer指向本函数栈帧顶,通过它可以找到本函数在进程栈中的位置。有专门的寄存器保存该值。 2 frame pointer有什么用 主要是backtrace用,每个函数的frame pointer保存在其后调用...
  • 今天做线程的例程出现warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]的警告 编译结果: 问题解决 0 在网站...
  • 深入理解Tagged Pointer

    2015-10-23 10:48:07
    原文地址:深入理解Tagged Pointer 前言 在2013年9月,苹果推出了iPhone5s,与此同时,iPhone5s配备了首个采用64位架构的A7双核处理器,为了节省内存和提高执行效率,苹果提出了Tagged Pointer的概念。...
  • Incompatible integer to pointer conversion assigning to ‘NSInteger *’ (aka ‘int *’) from ‘NSInteger’ (aka ‘int’) 用[NSNumber numberWithInteger:id]转换. Incompatible integer to pointer ...
  • Pointer to Const & const pointer

    千次阅读 2010-11-02 14:08:00
      下面的内容摘自 Thinking in C++ Chapter 8 Constants   Pointer to const   这有两种定义形式 const int* u;   Starting from the identifier, we read “u ...
  • POINTER_64

    2017-05-16 08:34:30
    改动SDK的头文件,不是...在stdafx.h的开头,加上如下代码:#ifndef POINTER_64#if !defined(_MAC) && (defined(_M_MRX000) || defined(_M_AMD64) || defined(_M_IA64)) && (_MSC_VER >= 1100) && !(defined(MIDL_PASS
  • ncnn报null pointer dereference异常

    千次阅读 2020-05-19 16:48:55
    在打包成为so库之前,在Windows系统以及Ubuntu系统利用电脑的camera测试过都能正常运行没有任何异常,打包成so库之后给Android的开发人员调用时,直接报了一个null pointer dereference错误,并且APP直接奔溃 ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 432,955
精华内容 173,182
关键字:

pointer