#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 = newint;
*(uk[i])->b = 5;
}
for (int i = 0; i < MAX_DELKA; ++i) {
uk[i]->a = i ;
}
return 0;
}
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 = newint;
*(*uk)[i].b = 5;
(*uk)[i].a = i ;
}
#include <stdio.h>
#include <iostream>
// #include "check.h" <-- you did't give us this part of code - hope it's irrelevant
constexprint 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 = newint;
*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:
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