In order to complete the previous valid answers, I will try to expand the answer:
Have I done it correctly?
Well, in order to pass data between functions: yes, doing it with pointers is an option. But it is commonly discouraged by the community because it makes troublesome the memory management.
When you work with pointers pointing dynamic memory, you must have a clear image of WHERE the memory is created and WHERE the memory will be deleted, in other words: the cycle of life of the memory must be clear and straightforward and for this reason, pass pointers between functions is commonly discouraged.
For example, in your case: the unsigned int* step1(...)
function returns a pointer but looking at it, a new programmer or someone that works with you wouldn't tell if the pointer returned is dynamic memory and if calling step1 must delete
or delete []
the memory after the call, same goes for unsigned char* step2(unsigned int* savedData, ...)
and would be more confusing and troublesome because someone would ask: step2
would alter the savedData
passed?
In order to fix the step2
issue, you can change the function to:
unsigned char* step2(const unsigned int* const savedData, ...)
By adding the const
you're telling: "Hey! step2
isn't going to alter the contents of savedData
nor changing the address it is pointing to".
But all the previous text is useless because doesn't fix the most important problem: Where the memory is freed?
In the step1
you're creating memory dynamically, in the step2
this memory is readed but... the delete
is hidden somewhere in the code that you don't paste? or there is a step3
waiting for take care of the memory?
To avoid all this memory headaches, is commonly advised the use of STL containers, like std::vector
, the container will take care of the memory management for you, in your case:
typedef std::vector<int> intvector;
typedef std::vector<intvector> intintvector;
void step1(intintvector &Data, ...) {
...
// create data arrays
intvector data0, data1, data2, data3;
// fill data0, data1, data2, data3
// ...
// save data arrays.
Data.push_back(data0);
Data.push_back(data1);
Data.push_back(data2);
Data.push_back(data3);
}
void step2(const intintvector &savedData, ...) {
// read data arrays
intvector data0 = savedData[0];
intvector data1 = savedData[1];
intvector data2 = savedData[2];
intvector data3 = savedData[3];
// ...
}
In brief summary: You aren't using correctly the pointer part if you don't take care of the dynamic memory, so, you must fix this issue or delegate into STL containers.
Hope it helps! :D