指向数组的指针一旦超出范围就会释放其元素
我正在尝试从二进制文件中读取数据,然后将二进制信息从 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;
我想让 int 的 temp[] 在退出函数后保持不变。
我是 C++ 新手,我不确定是否有其他方法可以完成我想要做的事情。
I am trying to read from a binary file, and then convert the binary information from a char[]
pointer to an int[]
pointer.
I was wondering, is there something similar to static int var[]
, but for dynamically allocated arrays?
I am trying to find a way to tell C++ to not free up the memory after exiting the function.
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;
I want to get the temp[]
of ints to persist after exiting the function.
I am new to C++, and I'm not sure of other ways to accomplish what I'm trying to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:这很可能不是OP所需的,而是他们要求的,因此我将保留此答案,而不是删除它,以防万一其他人降落在这里。
人们希望这是在函数的多个调用中回收动态分配的内存的正常原因。它“有时”具有有趣的性能优势,以防止函数同时使用多个线程。
如果那不是您试图做的,那就停止阅读。否则...
如果您想以这种方式回收临时动态存储,那么静态局部变量将为您带来想要的东西。
这就是使用
std :: vector<>
的样子,您确实应该代替原始数组指示。Edit: This is most likely not what the OP needs, but it is what they asked, so I'll keep this answer around instead of deleting it in case anyone else lands here.
The normal reason people want this is in to recycle the dynamically allocated memory across multiple invocations of the function. It "sometimes" has interesting performance benefits, at the cost of preventing the function from being used in multiple threads concurrently.
If that's not what you are attempting to do, then stop reading. Otherwise...
If you want to recycle temporary dynamic storage this way, then a static local variable will get you what you want.
Here's what it would look like using a
std::vector<>
, which you really should instead of raw array pointers.