指向数组的指针一旦超出范围就会释放它的元素
Pointer to an array has it's elements freed once outside scope
我正在尝试从二进制文件中读取,然后将二进制信息从 char[]
指针转换为 int[]
指针。
我想知道,是否有类似于 static int var[]
的东西,但用于动态分配的数组?
我正在尝试找到一种方法告诉 C++ 在退出函数后不要释放内存。
imagein.open(loc, ios::binary);
//Creates a pointer to a list of chars with a length of 8, which is just enough for 2 ints
char* timechar = new char[8];
//Reads 8 bytes of data from the file
imagein.read(timechar, 8);
//Creates an int pointer with enough room for 2 vars
int* ints = new int[2];
//Tells C that everything within timechar is now an int, and copies the contents to the ints pointer
ints = (int*)timechar;
//gets the width, and height of the image from the 1st 2 bytes
width = ints[0];
height = ints[1];
//Creates the size var
size = width * height;
//sets the ammout of ints to read to the width * height
times = size;
//creates a pointer to a list of chars so that we can read the data from the file
char* chars = new char[times * 4];
//Reads the rest of the file into chars
imagein.read(chars,times*4);
//reinitilizes buffer to be the correct size to the compiler
int* temp= new int[size+2];
//shifts the pointer to the buffer over twice
++temp;
++temp;
//takes everything from chars and tells C that all the bits within the chars var are copied over as int's
temp = (int*)chars;
//Shifts the buffer pointer back to where it should be
--temp;
--temp;
//sets the 1st, and 2nd buffer vars to the width, and height of the given image
temp[0] = width;
temp[1] = height;
//increases size to the correct size of the array buffer
size += 2;
我想让 temp[]
的整数在退出函数后继续存在。
我是 C++ 的新手,我不确定是否有其他方法可以完成我想做的事情。
编辑: 这很可能不是 OP 需要的,但这是他们要求的,所以我会保留这个答案而不是删除它以防其他人降落在这里。
I am trying to find a way to tell C++ to not free up the memory after exiting the function.
人们想要这样做的正常原因是为了在函数的多次调用中回收动态分配的内存。它“有时”具有有趣的性能优势,但代价是阻止该函数在多个线程中并发使用。
如果这不是您想要做的,请停止阅读。否则...
如果你想以这种方式回收临时动态存储,那么静态局部变量就能满足你的需求。
这是使用 std::vector<>
的样子,你真的应该用它来代替原始数组指针。
void someFunction(std::size_t size) {
static vector<int> temp;
// ...
if(temp.size() < size+2) {
temp.resize(size+2);
}
}
我正在尝试从二进制文件中读取,然后将二进制信息从 char[]
指针转换为 int[]
指针。
我想知道,是否有类似于 static int var[]
的东西,但用于动态分配的数组?
我正在尝试找到一种方法告诉 C++ 在退出函数后不要释放内存。
imagein.open(loc, ios::binary);
//Creates a pointer to a list of chars with a length of 8, which is just enough for 2 ints
char* timechar = new char[8];
//Reads 8 bytes of data from the file
imagein.read(timechar, 8);
//Creates an int pointer with enough room for 2 vars
int* ints = new int[2];
//Tells C that everything within timechar is now an int, and copies the contents to the ints pointer
ints = (int*)timechar;
//gets the width, and height of the image from the 1st 2 bytes
width = ints[0];
height = ints[1];
//Creates the size var
size = width * height;
//sets the ammout of ints to read to the width * height
times = size;
//creates a pointer to a list of chars so that we can read the data from the file
char* chars = new char[times * 4];
//Reads the rest of the file into chars
imagein.read(chars,times*4);
//reinitilizes buffer to be the correct size to the compiler
int* temp= new int[size+2];
//shifts the pointer to the buffer over twice
++temp;
++temp;
//takes everything from chars and tells C that all the bits within the chars var are copied over as int's
temp = (int*)chars;
//Shifts the buffer pointer back to where it should be
--temp;
--temp;
//sets the 1st, and 2nd buffer vars to the width, and height of the given image
temp[0] = width;
temp[1] = height;
//increases size to the correct size of the array buffer
size += 2;
我想让 temp[]
的整数在退出函数后继续存在。
我是 C++ 的新手,我不确定是否有其他方法可以完成我想做的事情。
编辑: 这很可能不是 OP 需要的,但这是他们要求的,所以我会保留这个答案而不是删除它以防其他人降落在这里。
I am trying to find a way to tell C++ to not free up the memory after exiting the function.
人们想要这样做的正常原因是为了在函数的多次调用中回收动态分配的内存。它“有时”具有有趣的性能优势,但代价是阻止该函数在多个线程中并发使用。
如果这不是您想要做的,请停止阅读。否则...
如果你想以这种方式回收临时动态存储,那么静态局部变量就能满足你的需求。
这是使用 std::vector<>
的样子,你真的应该用它来代替原始数组指针。
void someFunction(std::size_t size) {
static vector<int> temp;
// ...
if(temp.size() < size+2) {
temp.resize(size+2);
}
}