Class and dinamyc memory

In this simple exercise, for the "test" of type boolean, I used the variable k = sizeof (data) / sizeof (data [0]), the problem lies in the fact that the dynamic memory k is always equal to 1 . Why?

Thanks on advance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  #include <iostream>
#include <math.h>
using namespace std;
 
 
int size = 1;
 
// const int size = 10;
 
class parity
  {
    private:
            int  count;
            int  k;
            int* data = new (nothrow) int[size];
            // int data[ size ];
      
    public: 
            explicit parity();// constructor
            void put();       // this function count the number of elements on the array data[]
            bool test();      // this function control if the number of elements is even
            void printer();   // output to screen
   };     
    
    
inline parity::parity()
  {
    count = 0;
    k = 0;
   };
 
inline void parity::put()  // 
  {
    int item = 1;
    while( item != 0 )
      {
        cout << " input new element " << endl;
        cin >> item;
        
        if ( item != 0 )
          {          
            data[count] = item;
            count++;
          }
       }
    k = sizeof( data ) / sizeof( data[ 0 ] );
  };
 
    
    
inline bool parity::test()
  {
    return ( k % 2 != 0 ? 0 : 1 );
   };
 
    
    
inline void parity::printer()
  {
    for ( int i = 0; i < count; i++)
      { 
        cout << " data[ " << i << " ] = " << data [ i ] << " k = " << k << endl;
       }
   };
 
    
    
int main() {
  
  class parity a;
 
  a.put();
      switch ( a.test() )
      {
        case 0:
             cout << " number item is even " << endl;
             break;
        
        case 1:
             cout << " number item is odd " << endl;
             break;
        
        default:
             cout << " zero item " << endl;
      }
  
  a.printer();
  
  return 0;
}

Last edited on
sizeof(a_pointer) gives you the size of a pointer... not the size of the data the pointer points to. This is different from sizeof(an_array) which gives you the size of an array.

Dynamically allocated memory ('data' in your example) is accessed through a pointer. So when you say sizeof(data) you are getting sizeof(int*) which is useless for what you're trying to do.


This is one of the reasons why sizeof() is a bad approach for finding array dims. Not only does it not work for dynamic arrays but it also does not give you a compiler error when used incorrectly like the above... leading to runtime bugs that are harder to find.



Possible solutions to your problem:

1 (recommended): Use a vector rather than managing the memory yourself. In addition to eliminating memory leaks (which you currently have because you never delete[] your array), it also has a proper copy ctor and assignment operator so you don't have to write your own (which, for your class you would or else your program will not work correctly if you try to copy parity objects). AND it has a built-in size() method so you don't have to do sizeof() tricks.

or

2: Keep a separate 'size' variable which keeps track of the size of allocated memory. Use that rather than using sizeof()
Well, since I took the time to write this I'll post it anyway.

k is not "dynamic memory". k is a pointer.

A pointer is a variable that holds a memory address (which is a number). On some machines it happens that the size of a pointer equals the size of an integer.

Your trick works with arrays, where sizeof array gives how many total bytes the array contains, and by dividing by how many bytes an element of the array requires sizeof array[0], you get the length of the array. But that trick doesn't work with pointers.
@disch

In addition to eliminating memory leaks (which you currently have because you never delete[] your array)


can explain why in general, we have memory leaks, during the execution of the program?


@catfìsh
apologizes. dynamic memory was referring to the vector data. I expressed myself badly
can explain why in general, we have memory leaks, during the execution of the program?


new allocates heap memory. That memory stays allocated until you explicitly tell the machine you're done with it by delete-ing it. If you do not delete it... the memory stays allocated for at least the lifetime of the entire program.

If you do this repeatedly, your program will suck up more and more and more memory over time. Therefore it's good practice to delete memory when you're done with it... or better yet... use smart pointers and containers to manage the memory for you so that it is automatically deleted when you're done with it.
Thanks you disch and cath for the explanations.
Topic archived. No new replies allowed.