标签: pointers

在删除之前有没有理由检查NULL指针?

我经常NULL在删除指针之前看到遗留代码检查,类似于

if (NULL != pSomeObject) 
{
    delete pSomeObject;
    pSomeObject = NULL;
}
Run Code Online (Sandbox Code Playgroud)

NULL在删除之前有没有理由检查指针?NULL之后设置指针的原因是什么?

c++ null pointers delete-operator

49
推荐指数
4
解决办法
3万
查看次数

使用boost :: shared_ptr时有什么潜在的危险?

使用时,你有什么方法可以用脚射击自己boost::shared_ptr?换句话说,当我使用时,我必须避免哪些陷阱boost::shared_ptr

c++ boost pointers shared-ptr

49
推荐指数
6
解决办法
1万
查看次数

如何修改已传递到C函数中的指针?

所以,我有一些代码,类似于以下,将结构添加到结构列表:

void barPush(BarList * list,Bar * bar)
{
    // if there is no move to add, then we are done
    if (bar == NULL) return;//EMPTY_LIST;

    // allocate space for the new node
    BarList * newNode = malloc(sizeof(BarList));

    // assign the right values
    newNode->val = bar;
    newNode->nextBar = list;

    // and set list to be equal to the new head of the list
    list = newNode; // This line works, but list only changes inside of this function
}
Run Code Online (Sandbox Code Playgroud)

这些结构定义如下:

typedef …
Run Code Online (Sandbox Code Playgroud)

c pointers pass-by-value

49
推荐指数
4
解决办法
5万
查看次数

49
推荐指数
4
解决办法
12万
查看次数

Box,ref,&和*之间的理解和关系

我对Rust中指针的工作方式感到有些困惑.有ref,Box,&,*,我不知道他们是如何协同工作.

这是我目前的理解方式:

  1. Box 它实际上不是一个指针 - 它是一种在堆上分配数据的方法,并在函数参数中传递未经过类型化的特性(特别是特征).
  2. ref用于模式匹配以借用你匹配的东西,而不是拿它.例如,

    let thing: Option<i32> = Some(4);
    match thing {
        None => println!("none!"),
        Some(ref x) => println!("{}", x), // x is a borrowed thing
    }
    println!("{}", x + 1); // wouldn't work without the ref since the block would have taken ownership of the data
    
    Run Code Online (Sandbox Code Playgroud)
  3. &用来借(借来的指针).如果我有一个函数,fn foo(&self)那么我正在引用自己的函数将在函数终止后过期,只留下调用者的数据.我也可以传递我想要保留所有权的数据bar(&mydata).

  4. *用于制作原始指针:例如,let y: i32 = 4; let x = &y as *const i32 …

pointers rust

49
推荐指数
4
解决办法
9085
查看次数

漏洞利用代码中的指针说明

在获取root shell的一些漏洞中,我经常看到这样一个指针:

int i;
unsigned *p = *(unsigned**)(((unsigned long)&i) & ~8191); 
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释一下这个指针吗?我认为8191是内核堆栈的大小.p指向内核堆栈的底部?以下是指针p的使用方法:

int i; 
unsigned *p = *(unsigned**)(((unsigned long)&i) & ~8191); 
for (i = 0; i < 1024-13; i++) { 
    if (p[0] == uid && p[1] == uid && 
        p[2] == uid && p[3] == uid && 
        p[4] == gid && p[5] == gid && 
        p[6] == gid && p[7] == gid) { 
            p[0] = p[1] = p[2] = p[3] = 0; 
            p[4] = …
Run Code Online (Sandbox Code Playgroud)

c linux pointers exploit

49
推荐指数
2
解决办法
2355
查看次数

为什么[object doSomething]而不是[*object doSomething]?

在Objective-C中,为什么[object doSomething]?不是[*object doSomething]因为你在对象上调用一个方法吗?这意味着你应该取消引用指针?

pointers objective-c

48
推荐指数
5
解决办法
5592
查看次数

指向2d数组的指针

我有一个关于指向2d数组的指针的问题.如果数组是这样的

int a[2][3];
Run Code Online (Sandbox Code Playgroud)

那么,这是一个指向数组的指针a吗?

int (*p)[3] = a;
Run Code Online (Sandbox Code Playgroud)

如果这是正确的,我想知道什么[3]意思int(*p)[3]

c++ arrays pointers multidimensional-array

48
推荐指数
4
解决办法
5万
查看次数

C++中的声明

根据我的理解,C++中的声明/初始化是具有"基本类型"的语句,后跟逗号分隔的声明符列表.

请考虑以下声明:

int i = 0, *const p = &i; // Legal, the so-called base type is 'int'.
                          // i is an int while p is a const pointer to an int.

int j = 0, const c = 2;   // Error: C++ requires a type specifier for all declarations.
                          // Intention was to declare j as an int and c an as const int.

int *const p1 = nullptr, i1 = 0; // p1 is a const pointer to …
Run Code Online (Sandbox Code Playgroud)

c++ pointers const declaration language-lawyer

48
推荐指数
2
解决办法
2020
查看次数

通过从本地C风格的数组返回指针来获取悬空指针

我对以下代码感到有点困惑:

#include <iostream>

const char* f()
{
    const char* arr[]={"test"};
    return arr[0];
}

int main()
{
    auto x = f();
    std::cout << x;
}
Run Code Online (Sandbox Code Playgroud)

在我看来,这段代码应该是UB(未定义的行为).我们返回一个指向本地范围内的C风格数组元素的指针.事情应该出问题.但是,我测试过的编译器都没有抱怨(我用过-Wall -Wextra -pedanticg ++和clang).valgrind也不抱怨.

上面的代码是有效的还是人们会想到的UB?

PS:运行它似乎产生"正确"的结果,即显示"测试",但这并不表示正确性.

c++ memory pointers undefined-behavior

48
推荐指数
1
解决办法
2491
查看次数