可能重复:
通过非const指针修改const
我正在学习C++,对指针非常有趣.而我试图改变一个恒定值的值(我的老师叫它backdoor,请澄清我是不是错了)像这样:
const int i = 0;
const int* pi = &i;
int hackingAddress = (int)pi;
int *hackingPointer = (int*)pi;
*hackingPointer = 1;
cout << "Address:\t" << &i << "\t" << hackingPointer << endl;
cout << "Value: \t" << i << "\t\t" << *hackingPointer << endl;
system("PAUSE");
return 0;
Run Code Online (Sandbox Code Playgroud)
但是,结果很奇怪.虽然两个地址相同,但值不同.

我的代码是如何执行的?哪里0和1价值准确存储?
我想创建一个数据向量,但我想设置它的大小并在子函数中填充它的元素.这是使用新运营商的合适时机吗?有没有更好的方法呢?这似乎是一个合适的时间,但我犹豫不决,因为为什么C++程序员应该尽量减少"新"的使用?
int main()
{
vector<double> *array1;
vector<double> *array2;
OtherArgs otherArgs;
FillArrays(array1,array2,otherArgs);
//Do other stuff
delete array1;
delete array2;
}
void FillArrays(vector<double> *&array1, vector<double> *&array2, OtherArgs &otherArgs)
{
int size=GetSize(otherArgs);
array1 = new vector<double>(size);
array2 = new vector<double>(size);
//Other code to fill the arrays
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我对C++数组有一个非常奇怪的问题.一切顺利,直到我将它传递给函数将其写入磁盘,之后,更改数组而不执行任何操作.
int saveMatrix(Long64_t*, unsigned int, const char*);
void resetArray(Long64_t* array, unsigned int size)
{
for (unsigned long int i = 0; i < size; i++)
array[i] = 0;
}
int saveMatrix(Long64_t* array, unsigned int size, const char* filename)
{
// Returns:
// 0 - Exit
// 1 - Error
ofstream out(filename, ios::out);
if (!out) {
cout << "Cannot open file.";
return 1;
}
for (unsigned long int i = 0; i < size; i++) {
out << array[i] << " …Run Code Online (Sandbox Code Playgroud) 当我打印一个char指针时printf(),它使用转换说明符决定是应该打印地址还是根据%u或%s打开整个字符串.
但是当我想做同样的事情时cout,如何cout决定在地址和整个字符串中应该打印什么?这是一个示例来源:
int main()
{
char ch='a';
char *cptr=&ch;
cout<<cptr<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在我的GNU编译器中,cout尝试将ch输出为字符串.
我怎样才能得到的地址ch通过cptr使用cout?
#include <iostream>
using namespace std;
class Base {
public:
Base() {
cout << "In Base" << endl;
cout << "Virtual Pointer = " << (int*)this << endl;
cout << "Address of Vtable = "
<< (int*)*(int*)this << endl;
cout << "Value at Vtable = "
<< (int*)*(int*)*(int*)this << endl;
cout << endl;
}
virtual void f1() { cout << "Base::f1" << endl; }
};
class Drive : public Base {
public:
Drive() {
cout << "In Drive" << endl;
cout …Run Code Online (Sandbox Code Playgroud) 我是一个电子产品的人,请好好对待我.
我读到这一篇关于实时操作系统的8位微控制器.我遇到了一个奇怪的观点,我遇到了这个函数.我无法理解它在做什么.我知道这void意味着"没有类型".我猜这(*Task)是铸造.我真的不知道那之后那些括号是做什么的.
这个函数的论点包括什么?
另外,我无法理解做*(int*)((NewTCB->Stack) + (STACK_DEPTH-2)) = (int)Task;什么?

我知道c中有以下两个相同的东西(因为偏移和数组)
someArray[i] //ith element of someArray
*(someArray + i) //ith element of someArray
Run Code Online (Sandbox Code Playgroud)
但是对于结构体,相同的语法似乎并没有很好地保持......
someStruct[i]->*(someArray + j) //compiler error
*(someStruct + i)->someArray[j] //Also compiler error
Run Code Online (Sandbox Code Playgroud)
无论如何使用指针/偏移表示法(第二个)来表示结构的元素?
#include<stdio.h>
#include<stdlib.h>
char* re()
{
char *p = "hello";
return p;
}
int main()
{
char* tem = re();
printf("%s", tem);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的编译器是Dev-C++.我认为,"重"的功能完成时,"P"的指针将被删除,也将被删除其中"P" HAVS指向的堆栈空间.因此'tem'的指针不能访问'p'指向的堆栈空间.在我看来,这段代码会出现一些错误.但为什么不呢?
这个问题困扰了我很久.如果你能告诉我原因,我将感激你的善良心.
以下代码是否包含字符串中前三个字符的内存泄漏?
char * str = (char*)malloc(21 * sizeof(char));
strcpy(str, "01234567890123456879");
str = str + 3;
free(str);
Run Code Online (Sandbox Code Playgroud)
谢谢.
我有返回一个对象的函数,但我很困惑,我应该返回对象本身还是指向对象的指针?
这是我的功能的一个例子:
CImage CDocument::AddImage(string Name, string fileName)
{
CImage img = CImage();
img.Name = Name;
img.Path = fileName;
img.IwImage = Iw2DCreateImage(&fileName[0]);
Images.push_back(&img);
return img;
}
Run Code Online (Sandbox Code Playgroud)
这是正确的还是应该返回指向对象的指针:
CImage * CDocument::AddImage(string Name, string fileName)
{
CImage * img = new CImage();
img->Name = Name;
img->Path = fileName;
img->IwImage = Iw2DCreateImage(&fileName[0]);
Images.push_back(img);
return img;
}
Run Code Online (Sandbox Code Playgroud)
虽然最后一个代码无法正确编译,因为我收到此错误:
error C2440: 'initializing' : cannot convert from 'CImage' to 'CImage *'
Run Code Online (Sandbox Code Playgroud)
我认为这可能是一个非常简单的问题.我对c ++很新,所以请耐心等待.