Sorry guys before, I’m a newbie, I’m really new to this.
So exactly what you asked before is:
Problem state
I have a template class called Group
which can access unauthorized places in memory. The member variable Group<T>::Items
points to serine of locations on the heap that stores a sequence of data such as std::vector<T>
(C++ Built-in structure). Data can be manipulated to fit a dynamic size; The member function Group<T>::push(T)
is for adding a new item, and the member function Group& Group<T>::operator*(int)
can make copies and store them in Group<T >::Items
by passing a specific parameter scalar
like list()
in python
or it should work this way.
Here is the class hierarchy in code:
template <typename T>
class Group
{
private:
T* Items; int length;
public:
Group() : length(0)
{
Items = new(std::nothrow) T[0];
}
~Group()
{
delete[] Items; Items = nullptr;
}
void push(T ValuetoPush)
{
T* BufferAddress = new(std::nothrow) T[length + 1]; // allcatoin new heep's place for data
for(int i=0; i<length; i++) BufferAddress[i] = Items[i]; // copy data to the new place from the current.
/* add the new item to the new place, then make it the current place.*/
BufferAddress[length++] = ValuetoPush; Items = BufferAddress;
}
Group& operator*(int scalar)
{
Group* result = new Group;
for(int i=0; i<scalar; i++)
{
for(int j=0; j<length; j++) result->push(Items[j]);
}
return *result;
}
};
the problem
Now the key point is that when applying Group& Group<Group<int>>::operator*(int)
to a nested (Group<Group<int>>&
) object (it is similar to the main idea of arrays or lists 2d), when I run the executable, it terminates with a segmentation fault
error sometimes it returns a primitive Group<Group<int>>&
object which stores garbage in the first positions which is not related to my program and the command What is actually uncommon is that all operations performed after that (if the program passes without error) are correct, which makes matters worse, obviously this means there are memory allocation errors and of course I guess Group<Group<int>>::Items
stores wrong data positions after entering Group& Group<Group<int>>::operator*(int)
.
I’ve debugged the code several times trying to figure out what made the memory or Group<Group<int>>::Items
behave this way.
Here is the faulty code:
int main()
{
Group<int> A;
A.push(1); A.push(2); A.push(3);
Group<Group<int>> B;
B.push(A * 3);
B = B * 3; // now B = [[- <Lablablala>, <Lablablala>, - <Lablablala>, <Lablablala>, ...],
// [1, 2, 3, 1, 2, 3, 1, 2, 3],
// [1, 2, 3, 1, 2, 3, 1, 2, 3]] or it raise an error
}
I’ve debugged the code several times trying to figure out what made the memory or Group<Group<int>>::Items
behave this way.
so I want to do this:
B = B * 3;
std::cout << "<B>:[[" << B[0][0] << ',' << B[0][1] << ',' << B[0][2] << "]," << std::endl;
<< " [" << B[1][0] << ',' << B[1][1] << ',' << B[1][2] << "]," << std::endl;
<< " [" << B[2][0] << ',' << B[2][1] << ',' << B[2][2] << "]]" << std::endl;
<B>:[[1, 2, 3, 1, 2, 3, 1, 2, 3],
[1, 2, 3, 1, 2, 3, 1, 2, 3],
[1, 2, 3, 1, 2, 3, 1, 2, 3]]
Hopefully this makes sense to understand what I mean.
Mohmmed Omer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1