array of pointer

In the following piece of code, I have a problem of reading memory. When assigning value to adjncy It says "unable to read the memory". I can pass the adjncy variable as a vector of integer with reference and there will be no problem. But due to some reason I need to pass an array of pointer into the function. any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void SparseMatrixStruct::adjncy(VecVecIdx_t &network, idx_t *adjncy_)
{
	int k = 0;
	VecVecIdx_t::size_type total = 0;
	for (const auto &v : network) total += v.size();

	int *adjncy = new int[total];
	for (int i = 0; i < network.size(); i++) {

		for (int j = 0; j < network[i].size(); j++) {

			adjncy[k]=network[i][j];
			k++;
		}
	}

	adjncy_ = adjncy;

	delete[] adjncy;
}
Last edited on
any change that you do to adjncy_ inside your function are lost
perhaps you intended to do
1
2
3
void SparseMatrixStruct::adjncy(VecVecIdx_t &network, idx_t **adjncy_){
  //...
  *adjncy_ = adjncy;


also, you are shooting your foot on line 19
there you are releasing the memory, so adjncy_ becomes invalid

> But due to some reason I need to pass an array of pointer into the function
1
2
3
4
int an_array[42] = {6, 9, 54, 13};
int *a_pointer = nullptr;
int *a_pointer_pointing_to_the_start_of_an_array = array;
int* an_array_of_pointers[42] = {nullptr, NULL, new int(42)};
¿which one do you need?

also, you may work with std::vector and use .data() member function to obtain a pointer to the stored array.
https://www.cplusplus.com/reference/vector/vector/data/
Thank you for your help. What is the problem in line 19?I'm releasing the memory after assigning the adjncy to adjncy_. I thought it's not causing any problem. I need to do something like third option.
int *a_pointer_pointing_to_the_start_of_an_array = array;
What is happening exactly in the line below?
int* an_array_of_pointers[42] = {nullptr, NULL, new int(42)};
> What is the problem in line 19?
> I'm releasing the memory after assigning the adjncy to adjncy_.
a pointer holds a value, that value is a memory address
say that you doint *a = new int[100];, then check the value of `a' and has 0xfece5
starting there are 100 integers that you may use.

then you do b = a; now both `a' and `b' contain 0xfece5, and you may access those 100 integers using any of them

you release the memory with delete []a;, now 0xfece5 is no longer available for you, 0xfece5 is no longer valid
`b' still points there so it becomes an invalid pointer, so when you try to access your 100 integers with `b' you incur in undefined behaviour.

basically, then burned your house and you're trying to go to the bathroom upstairs.


> I need to do something like third option.
then you may use vector::data() (note that the vector should not be destroyed or the pointer will become invalid)


perhaps the last example wasn't good
1
2
3
4
5
int a;
int b;
int c;
int* ptr[] = {&a, &b, &c};
*ptr[1] = 42; //modifies the content of `1' 
Thanks for you explanation. I got it.
Topic archived. No new replies allowed.