Pointer to pointer to structure

The problem is described in a comment in the code. Thank you for help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <iostream>
#include "check.h"

#define MAX_DELKA 5

using std::cout;
using std::endl;

struct MyStruct
{
	int a;
	int *b;
};

int alokuj(MyStruct** uk);

int main() {
	MyStruct *vec = nullptr;
	int RetValue = alokuj(&vec);
	memory_stat();
	file_stat();
	stat();
	return 0;
}

int alokuj(MyStruct** uk) {
	if (*uk != nullptr) {
		cout << "Chybny vstupni pointer" << endl;
		return 1;
	}
	try
	{
		*uk= new  MyStruct[MAX_DELKA];
//this is my problem, the program does not allocate elements according to 
//MAX_DELKA macro, but only one element. What is the problem and what should I do differently?
	}
	catch (std::bad_alloc)
	{
		cout << "Nenaalokovalo se pole struktur" << endl;
	}
	
	for (int i = 0; i < MAX_DELKA; ++i) {
		uk[i]->b = new  int;
		*(uk[i])->b = 5;
	}
	for (int i = 0; i < MAX_DELKA; ++i) {
		uk[i]->a = i ;						
	}
		
	return 0;
}
Last edited on
The way you're using the pointer on lines 44, 45, and 49 is inconsistent.

Think about what the pointers contain:
     memory location       |           data
---------------------------+--------------------------
&uk                        | A 'MyStruct **' (value x)
x                          | A 'MyStruct *' (value y)
x + sizeof(MyStruct *)     | UNDEFINED
x + sizeof(MyStruct *) * 2 | UNDEFINED
x + sizeof(MyStruct *) * 3 | UNDEFINED
y                          | A MyStruct
y + sizeof(MyStruct)       | A MyStruct
y + sizeof(MyStruct) * 2   | A MyStruct
y + sizeof(MyStruct) * 3   | A MyStruct
y + sizeof(MyStruct) * 4   | A MyStruct
y + sizeof(MyStruct) * 5   | UNDEFINED
y + sizeof(MyStruct) * 6   | UNDEFINED
y + sizeof(MyStruct) * 7   | UNDEFINED
In the above table, x is equal to the value in uk when alokuj() begins, and y is equal to the value in *uk after line 34.
Therefore, to initialize the structs at y + sizeof(MyStruct) * n, the correct way to dereference the pointer is not uk[i]->b, but rather (*uk)[i].b:
1
2
3
4
5
for (int i = 0; i < MAX_DELKA; ++i) {
	(*uk)[i].b = new  int;
	*(*uk)[i].b = 5;
	(*uk)[i].a = i ;						
}
Last edited on
Thanks for help, I tried what you said, however the code still does not work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <iostream>
#include "check.h"

#define MAX_DELKA 5

using std::cout;
using std::endl;

struct MyStruct
{
	int a;
	int *b;
};

int alokuj(MyStruct** uk);

int main() {
	MyStruct *vec = nullptr;
	int RetValue = alokuj(&vec);
	memory_stat();
	file_stat();
	stat();
	return 0;
}

int alokuj(MyStruct** uk) {
	if (*uk != nullptr) {
		cout << "Chybny vstupni pointer" << endl;
		return 1;
	}
	try
	{
		*uk= new  MyStruct[MAX_DELKA];

	}
	catch (std::bad_alloc)
	{
		cout << "Nenaalokovalo se pole struktur" << endl;
	}
	/*
	for (int i = 0; i < MAX_DELKA; ++i) {
		uk[i]->b = new  int;
		*(uk[i])->b = 5;
	}
	*/
	for (int i = 0; i < MAX_DELKA; ++i) {
		(*uk)[i].b = new  int;
		*(*uk)[i].b = 5;
		(*uk)[i].a = i;
		cout << "element a= " << uk[i]->a << ",element b= " << *(uk)[i]->b << endl;
 	}
/*
	for (int i = 0; i < MAX_DELKA; ++i) {
		uk[i]->a = i ;						
	}
*/		
	return 0;
}
Last edited on
Do you really need such a complicated code?

Here is the solution to your issue (if I’ve understood it correctly):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <iostream>
// #include "check.h" <-- you did't give us this part of code - hope it's irrelevant

constexpr int MAX_DELKA = 5;

struct MyStruct {
    int a  {};
    int *b { nullptr };
};

int alokuj(MyStruct** uk);

int main()
{
    MyStruct* vec = nullptr;
    int RetValue = alokuj(&vec);
    // memory_stat();
    // file_stat();
    // stat();
    return 0;
}

int alokuj(MyStruct** uk) {
    if (*uk != nullptr) {
        std::cout << "Chybny vstupni pointer\n";
        return 1; // -1 is often use as error code
    }
    try {
        uk = new MyStruct*[MAX_DELKA];
    } catch (std::bad_alloc) {
        std::cout << "Nenaalokovalo se pole struktur\n";
        // shouldn't you return an error code here?
    }
    for (int i = 0; i < MAX_DELKA; ++i) {
        uk[i] = new MyStruct;
        uk[i]->b = new int;
        *uk[i]->b = 5;
        uk[i]->a = i;
        std::cout << "element a = " << uk[i]->a << ", element b = " << *uk[i]->b << '\n';
    }
    return 0;
}


Output:
element a = 0, element b = 5
element a = 1, element b = 5
element a = 2, element b = 5
element a = 3, element b = 5
element a = 4, element b = 5


And here is a simpler code which seems to do exactly the same thing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <cstdio>
#include <iostream>
// #include "check.h" <-- you did't give us this part of code - hope it's irrelevant

constexpr int MAX_DELKA = 5;

struct MyStruct {
    int a  {};
    int *b { nullptr };
};

int alokuj(MyStruct* uk, size_t howmany);

int main()
{
    MyStruct* vec = nullptr;
    int RetValue = alokuj(vec, MAX_DELKA);
    // memory_stat();
    // file_stat();
    // stat();
    return 0;
}

int alokuj(MyStruct* uk, size_t howmany) {
    if (uk != nullptr) {
        std::cout << "Chybny vstupni pointer\n";
        return 1; // -1 is often use as error code
    }
    try {
        uk = new MyStruct[howmany];
    } catch (std::bad_alloc) {
        std::cout << "Nenaalokovalo se pole struktur\n";
        // shouldn't you return an error code here?
    }
    for (size_t i = 0; i < howmany; ++i) {
        uk[i].b = new int;
        *uk[i].b = 5;
        uk[i].a = i;
        std::cout << "element a = " << uk[i].a << ", element b = " << *uk[i].b << '\n';
    }
    return 0;
}


Output:
element a = 0, element b = 5
element a = 1, element b = 5
element a = 2, element b = 5
element a = 3, element b = 5
element a = 4, element b = 5

The reason I am using **pointer in int alokuj() is that I need pointer vec in main to point to an array made of five MyStruct structures allocated in alokuj() so I could work with it in main....maybe better explenation: I need alokuj() to create this array and once the function ends I need pointer vec in main to point to this array
Pass argument by reference:
int alokuj( MyStruct* & uk, size_t howmany );
Much appreciated, this is just what I need and it is far more simple than ** pointer :)
Topic archived. No new replies allowed.