std::map<std::string, std::string> myMap;
std::map<std::string, std::string>::iterator i = m_myMap.find(some_key_string);
if(i == m_imagesMap.end())
return NULL;
string *p = &i->first;
Run Code Online (Sandbox Code Playgroud)
最后一行有效吗?我想将这个指针p存储在其他地方,它对整个程序生命有效吗?但是如果我向这个地图添加更多元素(使用其他唯一键)或删除其他一些键会发生什么情况呢,它不会重新分配这个字符串(键值对),那么p将变为无效?
int* p = 0;
int* q = &*p;
Run Code Online (Sandbox Code Playgroud)
这是未定义的行为吗?我浏览了一些相关问题,但这个具体方面没有显示出来.
gcc 4.4.4
我究竟做错了什么?
char x[10];
char y[] = "Hello";
while(y != NULL)
*x++ = *y++;
Run Code Online (Sandbox Code Playgroud)
非常感谢任何建议.
对于C++新手来说,允许const成员函数在类引用的对象(通过指针或引用)上调用非const方法通常会让人感到困惑.例如,以下内容完全正确:
class SomeClass
{
class SomeClassImpl;
SomeClassImpl * impl_; // PImpl idiom
public:
void const_method() const;
};
struct SomeClass::SomeClassImpl
{
void non_const_method() { /*modify data*/ }
};
void SomeClass::const_method() const
{
impl_->non_const_method(); //ok because impl_ is const, not *impl_
};
Run Code Online (Sandbox Code Playgroud)
但是,如果constness传播到尖头对象,它有时会非常方便(我自愿使用PImpl习语,因为它是我认为"constness传播"非常有用的情况之一).
使用指针时,可以通过使用某种智能指针轻松实现这一点,操作符在constness上重载:
template < typename T >
class const_propagating_ptr
{
public:
const_propagating_ptr( T * ptr ) : ptr_( ptr ) {}
T & operator*() { return *ptr_; }
T const & operator*() const { return *ptr_; }
T * operator->() …Run Code Online (Sandbox Code Playgroud) 有没有办法在C中对齐指针?假设我正在将数据写入数组堆栈(因此指针向下移动)并且我希望我写入的下一个数据是4对齐的,因此数据写入的内存位置是4的倍数,我该怎么做那?
我有
uint8_t ary[1024];
ary = ary+1024;
ary -= /* ... */
Run Code Online (Sandbox Code Playgroud)
现在假设ary在位置点0x05.我希望它指向0x04.现在我可以做到
ary -= (ary % 4);
Run Code Online (Sandbox Code Playgroud)
但是C不允许模数指针.有没有与架构无关的解决方案?
昨天我参加了一家知名欧洲公司的CTO的演讲,直到最近他才知道java有指针.在面对他时,他说他绝对肯定java中存在指针/不安全代码.
我遇到了C#的问题,我想在我的代码中得到一个方法指针,但似乎不可能.我需要方法的指针,因为我想使用WriteProcessMemory来禁止它.我怎么得到指针?
示例代码
main()
{
function1();
function2();
}
function1()
{
//get function2 pointer
//use WPM to nop it (I know how, this is not the problem)
}
function2()
{
Writeline("bla"); //this will never happen because I added a no-op.
}
Run Code Online (Sandbox Code Playgroud) 所有程序应该做的一个常见条件是检查是否分配了变量.
采取以下声明:
(1)
if Assigned(Ptr) then
begin
// do something
end;
Run Code Online (Sandbox Code Playgroud)
(2)
if Ptr <> nil then
begin
// do something
end;
Run Code Online (Sandbox Code Playgroud)
Assigned(Ptr)和之间有什么区别Ptr <> nil?
为了理解C编程语言中free的用法,我尝试在Ubuntu上运行此代码,但是在运行EXE文件时,我收到了SIGABRT错误.为什么程序没有正常退出?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int ret;
int *ptr;
ptr = (int *)malloc(sizeof(int)*10);
free(ptr);
ptr = &ret;
free(ptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud) C++草案标准(N3337)有关于指针转换的以下内容:
4.10指针转换
2类型的右值"指针CV
T",其中T是对象类型,可被转化为式的一个右值"指针CVvoid".一个"指针转换的结果CVT"发送给"指针CVvoid"指向类型对象T所在的存储位置的起始位置,就好像该对象是类型最派生的对象(1.8)T(即不是基类子对象).
和
4.12布尔转换
1算术,枚举,指针或指向成员类型的指针的右值可以转换为类型的右值
bool.零值,空指针值或空成员指针值转换为false; 任何其他值都转换为true
基于以上所述,将函数指针或指针转换int为a void*也是完全可以的bool.
但是,考虑到两者的选择,指针应该转换为哪一个?
然后,为什么指向函数的指针转换为a bool和指向int转换为void*?的指针?
程序:
#include <iostream>
using namespace std;
void foo(const void* ptr)
{
std::cout << "In foo(void*)" << std::endl;
}
void foo(bool b)
{
std::cout << "In foo(bool)" << std::endl;
}
void bar()
{
}
int main()
{
int i …Run Code Online (Sandbox Code Playgroud)