Home > Enterprise >  Pointer to an array has it's elements freed once outside scope
Pointer to an array has it's elements freed once outside scope

Time:04-13

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.

CodePudding user response:

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.

I am trying to find a way to tell C to not free up the memory after exiting the function.

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.

void someFunction(std::size_t size) {
  static vector<int> temp;

  // ...
  if(temp.size() < size 2) {
    temp.resize(size 2);
  }
}
  • Related