Problema con variables sin inicializar
Estás cometiendo un error muy común y es hacer uso de una variable sin haber sido iniciada previamente a un valor.
void productos(int x, int y) {
x = n;
y = m;
std::cout << x * y;
}
Como puedes ver aquí estás asignando a x
y a y
los valores de n
y m
, sobreescribiendo los valores que has pasado al constructor, pero éstos no tienen valores inicialmente, por lo que su contenido inicial es indeterminado.
private:
int m;
int n;
int mil;
Solución haciendo uso correcto de la clase
Si reordeno el código para que haga lo que deseas éste sería el resultado:
#include <iostream>
class productos {
public:
productos(int x, int y) {
n = x;
m = y;
std::cout << "En el constructor: " << x * y << std::endl;
}
int producto1() {
return n * m;
}
private:
int m;
int n;
int mil;
};
int main() {
/* Creo una instancia de productos pasando al constructor (2, 2500) */
productos producto (2, 2500);
/* Uso el método producto1 para calcular el resultado de la multiplicación */
std::cout << "El resultado del producto es: " << producto.producto1() << std::endl;
return 0;
}
El resultado de la ejecución es:
En el constructor: 5000
El resultado del producto es: 5000
Análisis de errores con valgrind
Resultados de valgrind de tu código:
==30015== Memcheck, a memory error detector
==30015== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==30015== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==30015== Command: ./pruebas_stack
==30015==
==30015== Conditional jump or move depends on uninitialised value(s)
==30015== at 0x4EBFCDE: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==30015== by 0x4EC02BC: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==30015== by 0x4ECC06D: std::ostream& std::ostream::_M_insert<long>(long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==30015== by 0x4008DB: productos::productos(int, int) (in ./pruebas_stack)
==30015== by 0x40084A: main (in ./pruebas_stack)
==30015==
==30015== Use of uninitialised value of size 8
==30015== at 0x4EBFBC3: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==30015== by 0x4EBFD05: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==30015== by 0x4EC02BC: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==30015== by 0x4ECC06D: std::ostream& std::ostream::_M_insert<long>(long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==30015== by 0x4008DB: productos::productos(int, int) (in ./pruebas_stack)
==30015== by 0x40084A: main (in ./pruebas_stack)
==30015==
==30015== Conditional jump or move depends on uninitialised value(s)
==30015== at 0x4EBFBCF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==30015== by 0x4EBFD05: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==30015== by 0x4EC02BC: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==30015== by 0x4ECC06D: std::ostream& std::ostream::_M_insert<long>(long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==30015== by 0x4008DB: productos::productos(int, int) (in ./pruebas_stack)
==30015== by 0x40084A: main (in ./pruebas_stack)
==30015==
==30015== Conditional jump or move depends on uninitialised value(s)
==30015== at 0x4EBFD33: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==30015== by 0x4EC02BC: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==30015== by 0x4ECC06D: std::ostream& std::ostream::_M_insert<long>(long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19)
==30015== by 0x4008DB: productos::productos(int, int) (in ./pruebas_stack)
==30015== by 0x40084A: main (in ./pruebas_stack)
==30015==
-251679360
==30015==
==30015== HEAP SUMMARY:
==30015== in use at exit: 0 bytes in 0 blocks
==30015== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==30015==
==30015== All heap blocks were freed -- no leaks are possible
==30015==
==30015== For counts of detected and suppressed errors, rerun with: -v
==30015== Use --track-origins=yes to see where uninitialised values come from
==30015== ERROR SUMMARY: 20 errors from 4 contexts (suppressed: 0 from 0)
Donde resalto el significado de:
- Use of uninitialised value of size 8: uso de un valor sin inicalizar de tamaño 8.
- Conditional jump or move depends on uninitialised value(s): salto condicional depende de un valor sin inicializar.
Resultados de valgrind tras corregir el problema:
==29964== Memcheck, a memory error detector
==29964== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==29964== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==29964== Command: ./pruebas_stack
==29964==
5000
==29964==
==29964== HEAP SUMMARY:
==29964== in use at exit: 0 bytes in 0 blocks
==29964== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==29964==
==29964== All heap blocks were freed -- no leaks are possible
==29964==
==29964== For counts of detected and suppressed errors, rerun with: -v
==29964== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)