c++ operator = working weird (output ok inside the function, but wrong after return..)

c++ operator = working weird (output ok inside the function, but wrong after return..)

I have a program that I've been modifying to extract some data.

I have a program that I've been modifying to extract some data.

Recently I did another data extraction and used a work-around for lack of time.

Now, I am trying to find what the problem was, because I have some spare time.

Below, there is a function Act() which is a member function of class Mat, and it forms a new matrix with altered data values. The Mat has a pointer to a buffer containing actual data. I found that after the Act() function which returns a Matrix, the 'operator =' function is called. (there was no 'operator =' function so I added it. maybe default '= operator' was begin used. With or without my added 'operator =' function, it does not produce the final right output) Here is the portion of the '=' operator which is returning the data(type Mat).(it is a c++ code but I just used printf because it works anyway)

template<typename T>
Mat<T> Mat<T>::operator = (const Mat<T>& rhs)
{
  Mat<T> result(rhs.row_, rhs.col_);
  int num_bytes = sizeof(T) * rhs.row_ * rhs.col_;
  printf("ope = , res val = %x, src val = %x, row = %d, col = %d\n", result.val_, rhs.val_, rhs.row_, rhs.col_);
  for(int i=0;i<10;i++){
    printf("%04x ",((half_float::half *)rhs.val_)[i].data_);
    }
  printf("\n");
  memcpy(result.val_, rhs.val_, num_bytes);
  for(int i=0;i<10;i++){
    printf("%04x ",((half_float::half *)result.val_)[i].data_);
    }
  printf("\n");
  return result;
}

ope = , res val = 12199b0, src val = c07680, row = 128, col = 128

3be0 bbfc bbf5 3bf8 3af0 bbf6 bbef b29f bbaf 3bfd

3be0 bbfc bbf5 3bf8 3af0 bbf6 bbef b29f bbaf 3bfd

This '3be0 bbfc bbf5 ...' is the right pattern.

The calling part looks like this.

  printf("calling activation..\n");
  d0_out = d0_out.Act(NN_CELL::AT_TANH);
  std::cout << "MLP Layer 0 executed." << std::endl;
  for(int i=0;i<10;i++){
    printf("%04x ",((half_float::half *)d0_out.val_)[i].data_);
    }
  printf("\n");

But after the assignment, seen from the main function, the data is weird.

MLP Layer 0 executed.

40de c2e2 c1eb 425e 3d4a c21b c187 b2b8 bfce 4358

As you can see, the data is wrong.

The only way I can produce right output is using a new object and assigning the result of Act() function to it.

NN_CELL::Mat<T> d0_out1 = d0_out.Act(NN_CELL::AT_TANH); // tmp work-around  

Then, d0_out1 (new object) contains the right data(3be0 bbfc bbf5 ..), instead of original

d0_out = d0_out.Act(NN_CELL::AT_TANH);   

What is wrong with the original code?

c++

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Loops in C++ | For, While, and Do While Loops in C++

In this Video We are going to see how to use Loops in C++. We will see How to use For, While, and Do While Loops in C++.

Using isdigit() in C/C++

In this article, we'll take a look at using the isdigit() function in C/C++. This is a very simple way to check if any value is a digit or not. Let's look

C++ Tutorial for Beginners 19 - C++ Structures | Struct declaration | C++ Data Structures

Welcome to this course on C++ Tutorial for Beginners. In this video we will see How to use C++ Structures.

Object Oriented Programming in C++ | C++ OOPs Concepts | Learn Object Oriented C++

C++ is general purpose, compiled, object-oriented programming language and its concepts served as the basis for several other languages such as Java, Python, Ruby, Perl etc.

A Complete Guide to fread() in C/C++

In this article, we’ll take a look at using fread() in C/C++.