79542178

Date: 2025-03-28 18:34:28
Score: 0.5
Natty:
Report link

Well, following the advice of everyone who answered my question, I think I've managed to solve the problem.

1-Creating pointers in the constructor and destroying them in the structure's destructor was definitely a very bad idea. It was causing most of the problem.

2-Casting using a type that is not of a fixed size also turned out to be a serious mistake.

3-I also remove the MyMemcpy function and am no longer trying to copy the entire structure (just its members).

4-Add a byte of size to the array to fit the end-of-string character '\0'

5-Use sizeoff only with fixed-size types

6- Kenny's page was a great help --> (recommended) https://godbolt.org/

7-Well, all the comments were really helpful. Thank you all so much, guys.

And now I'm ready to start serializing.

The fixed code below:


//-----------------------------------------------------------------------------
// 
//-----------------------------------------------------------------------------
#include <iostream>
#include <cstring>
#include <cstdint>
//-----------------------------------------------------------------------------
// CL /EHsc /std:c++20 Test.cpp
//-----------------------------------------------------------------------------
struct Test
{
    uint8_t *A = nullptr;
    uint8_t *B = nullptr; 
};

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
int main(int argc, char **argv)
{
    Test *test1 = new Test;
    Test *test2 = new Test;
    Test *test3 = new Test;
    Test *test4 = new Test; 

    test1->A = new uint8_t[ 2 ];
    test1->B = new uint8_t[ 2 ];

    test2->A = new uint8_t[ 2 ];
    test2->B = new uint8_t[ 2 ];   

    test3->A = new uint8_t[ 2 ];
    test3->B = new uint8_t[ 2 ];

    test4->A = new uint8_t[ 2 ];
    test4->B = new uint8_t[ 2 ];


       
    if( memcpy( (void*)test2->A, (const void *) test1->A, sizeof(uint8_t) )  == nullptr)
    {
        std::cout<<"memcpy() -> ERROR = "<<std::endl;
        exit(EXIT_FAILURE);
    }
    
     if( memmove( (void*)test2->A, (const void *) test1->A, sizeof(uint8_t) )  == nullptr)
    {
        std::cout<<" memmove() -> ERROR = "<<std::endl;
        exit(EXIT_FAILURE);
    } 
    
    if( memmove( (void*)test4->A, (const void *) test1->A, sizeof(uint8_t))  == nullptr)
    {
        std::cout<<"memmove() -> ERROR = "<<std::endl;
        exit(EXIT_FAILURE);
    }   


    printf("%p  %p\n", test1->A ,   test1->B);
    printf("%p  %p\n", test2->A ,   test2->B);
    printf("%p  %p\n", test3->A ,   test3->B);  
    printf("%p  %p\n", test4->A ,   test4->B);


    delete test1->A;
    delete test1->B;

    delete test2->A;
    delete test2->B;   

    delete test3->A;
    delete test3->B;

    delete test4->A;
    delete  test4->B; 

    delete test1;
    delete test2;
    delete test3;
    delete test4;    
 
    exit(EXIT_SUCCESS);
    return 0;
}



Program returned: 0
Program stdout
0x26590330  0x26590350
0x26590370  0x26590390
0x265903b0  0x265903d0
0x265903f0  0x26590410

Note: I tagged the post with "C." It was later edited by a moderator, who changed the tag to C++... He'll know what he's doing. So don't make a mountain out of a grain of sand.


Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: IVANEZEDITIONS