我经常NULL在删除指针之前看到遗留代码检查,类似于
if (NULL != pSomeObject)
{
delete pSomeObject;
pSomeObject = NULL;
}
Run Code Online (Sandbox Code Playgroud)
NULL在删除之前有没有理由检查指针?NULL之后设置指针的原因是什么?
使用时,你有什么方法可以用脚射击自己boost::shared_ptr?换句话说,当我使用时,我必须避免哪些陷阱boost::shared_ptr?
所以,我有一些代码,类似于以下,将结构添加到结构列表:
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) 我对Rust中指针的工作方式感到有些困惑.有ref,Box,&,*,我不知道他们是如何协同工作.
这是我目前的理解方式:
Box 它实际上不是一个指针 - 它是一种在堆上分配数据的方法,并在函数参数中传递未经过类型化的特性(特别是特征).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)&用来借(借来的指针).如果我有一个函数,fn foo(&self)那么我正在引用自己的函数将在函数终止后过期,只留下调用者的数据.我也可以传递我想要保留所有权的数据bar(&mydata).
*用于制作原始指针:例如,let y: i32 = 4; let x = &y as *const i32 …在获取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) 在Objective-C中,为什么[object doSomething]?不是[*object doSomething]因为你在对象上调用一个方法吗?这意味着你应该取消引用指针?
我有一个关于指向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++中的声明/初始化是具有"基本类型"的语句,后跟逗号分隔的声明符列表.
请考虑以下声明:
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) 我对以下代码感到有点困惑:
#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:运行它似乎产生"正确"的结果,即显示"测试",但这并不表示正确性.
pointers ×10
c++ ×5
c ×3
arrays ×1
boost ×1
char ×1
const ×1
declaration ×1
exploit ×1
linux ×1
memory ×1
null ×1
objective-c ×1
rust ×1
shared-ptr ×1