Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

CW_Kovok

Alter Hase

  • »CW_Kovok« ist der Autor dieses Themas

Beiträge: 836

Wohnort: nähe Bonn

Beruf: Schüler

  • Private Nachricht senden

1

28.12.2005, 23:40

Merkwürdiges verhalten einer Templatevariable

moin,

bitte nicht über den code beschweren, der ist in 10 min entstanden... allerdings bin ich etwas verwundert über die augabe:

TArray.h:

C-/C++-Quelltext

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
namespace CW_Core
{
    template<class T> class TArray;
}

template<class T> class CW_Core::TArray
{

public:

    TArray(int nQuantity); //

    TArray(const TArray& rkObject); //

    ~TArray(); //


    TArray<T>& operator=(const TArray<T>& rkObject);
    T&       operator[](int i);
    const T&      operator[](int i) const;

    T*       GetArray();
    const T*      GetArray() const;


    void        Append(const T& rtElement);
    void        SetElement(int i, const T& rtElement);
    void        Remove(int i);
    void        RemoveAll();
    void        SetMaxQuantity(int nNewMaxQuantity);

    int      GetMaxQuantity() const;

private:

    int m_nMaxQuantity;
    T*  m_atArray;
};

template<class T> CW_Core::TArray<T>::TArray(int nQuantity = 1)
{
    if(nQuantity<1)
        nQuantity = 1;

    m_nMaxQuantity  = nQuantity;
    m_atArray       = 0;

    SetMaxQuantity(m_nMaxQuantity);
}

template<class T> CW_Core::TArray<T>::TArray(const TArray& tObject)
{
    m_atArray = 0;
    *this = tObject;
}

template<class T> CW_Core::TArray<T>::~TArray()
{
    free(m_atArray);
}

template<class T> inline CW_Core::TArray<T>& CW_Core::TArray<T>::operator=(const TArray<T>& rkObject)
{
    m_nMaxQuantity  = rkObject.m_nMaxQuantity;

    if(m_atArray)
        free(m_atArray);

    if(m_nMaxQuantity > 0)
    {
        m_atArray = (T*)malloc(sizeof(T)*m_nMaxQuantity);

        for (int i = 0; i < m_nMaxQuantity; i++)
            m_atArray[i] = rkObject.m_atArray[i];
    }

    else
    {
        m_atArray = 0;
    }

    return *this;
}

template<class T> inline T& CW_Core::TArray<T>::operator[](int i)
{
    if(i>m_nMaxQuantity-1)
        i = m_nMaxQuantity-1;

    return m_atArray[i];
}

template<class T> inline const T& CW_Core::TArray<T>::operator[](int i) const
{
    if(i>m_nMaxQuantity-1)
        i = m_nMaxQuantity-1;

    return m_atArray[i];
}

template<class T> inline T* CW_Core::TArray<T>::GetArray()
{
    return m_atArray;
}

template<class T> inline const T* CW_Core::TArray<T>::GetArray() const
{
    return m_atArray;
}

template<class T> inline void CW_Core::TArray<T>::Append(const T& rtElement)
{
    SetMaxQuantity(m_nMaxQuantity+1);
    m_atArray[m_nMaxQuantity-1] = rtElement;
}

template<class T> inline void CW_Core::TArray<T>::SetElement(int i, const T& rtElement)
{   
    if(i>=m_nMaxQuantity)
        Append(rtElement);

    else
    {
        SetMaxQuantity(m_nMaxQuantity+1);

        for(int j=m_nMaxQuantity-1; j>i; j--)
            m_atArray[j] = m_atArray[j-1];

        m_atArray[i] = rtElement;
    }
}

template<class T> inline void CW_Core::TArray<T>::Remove(int i)
{
    for(int j=m_nMaxQuantity-1; j>i; j--)
    {
        m_atArray[j-1] = m_atArray[j];
    }

    if(i<m_nMaxQuantity)
        SetMaxQuantity(m_nMaxQuantity-1);
}

template<class T> inline void CW_Core::TArray<T>::RemoveAll()
{
    SetMaxQuantity(0);
}

template<class T> inline void CW_Core::TArray<T>::SetMaxQuantity(int nNewMaxQuantity)
{
    if(nNewMaxQuantity)
    {
        if(!m_atArray)
            m_atArray = (T*)malloc(sizeof(T)*nNewMaxQuantity);

        else
        {
            m_atArray = (T*)realloc(m_atArray, sizeof(T)*nNewMaxQuantity);
            m_nMaxQuantity = nNewMaxQuantity;
        }
    }

    else
    {
        free(m_atArray);
        m_atArray       = 0;
        m_nMaxQuantity  = 0;
    }
}

template<class T> inline int CW_Core::TArray<T>::GetMaxQuantity() const
{
    return m_nMaxQuantity;
}


main.cpp:

C-/C++-Quelltext

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
#include <iostream>
#include <cstdio>
#include <strstream>
#include "CW_TArray.h"

using namespace std;

int main()
{
    CW_Core::TArray<int> a(5);

    for(int i=0; i<a.GetMaxQuantity(); i++)
        a[i]=i;

    a.SetMaxQuantity(3);
    a.Append(6);
    a.Append(7);
    a.Append(55);
    a.SetElement(1, 4);

    FILE* s = fopen("kai.ntz", "wb");

    fwrite((void*)&a, sizeof(CW_Core::TArray<int>)*a.GetMaxQuantity(), 1, s);

    fclose(s);

    s = fopen("kai.ntz", "rb");

    cout<<a.GetMaxQuantity()<<endl;

    CW_Core::TArray<int> b(7);
    
    cout<<a.GetMaxQuantity()<<endl;

    fread((void*)&b, sizeof(CW_Core::TArray<int>)*a.GetMaxQuantity(), 1, s);

    cout<<a.GetMaxQuantity()<<endl;

    for(i=0; i<b.GetMaxQuantity(); i++)
        cout<<b[i]<<endl;

    fclose(s);

    return 0;
}


Die Ausgabe ist jedoch:

Zitat


7
7
3284056 <-- hier der Fehler WIESO???? :crying:
0
4
1
2
6
7
55


Kann mir jemand sagen, warum a da seine größe verliert???

CW_Kovok
Was es alles gibt, das ich nich brauche - Aristoteles

Anonymous

unregistriert

2

28.12.2005, 23:47

Frage am Rande: Was sagt der Debugger? (F5 zum Starten, F10 und F11 zum Navigieren)

CW_Kovok

Alter Hase

  • »CW_Kovok« ist der Autor dieses Themas

Beiträge: 836

Wohnort: nähe Bonn

Beruf: Schüler

  • Private Nachricht senden

3

28.12.2005, 23:55

genau dass!!! Die variable verliert einfach ihren wert, wobei sie ihn bei

C-/C++-Quelltext

1
fread((void*)&b, sizeof(CW_Core::TArray<int>)*a.GetMaxQuantity(), 1, s);
plötzlich wiederfindet, kA wieso
Was es alles gibt, das ich nich brauche - Aristoteles

Anonymous

unregistriert

4

29.12.2005, 00:09

CW_Kovok
Ganz ehrlich? Keine Ahnung :D Ich kann das mit dem fread/fwrite und malloc und realloc einfach nicht sehen :D

Falls Du aber auf real C++ zurückgreifen willst und ein realloc suchst: Marke Patrick hat eins:

C-/C++-Quelltext

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
// ///////////////////////////////////////////////// 

// GLOBAL 

// 

// Funktion zum Vergrößern eines Speicherbereiches

// z. B. ein Array.

// 

// [in]  array   : Das 'Quellarray'. 

// [in]  old_size: Alte größe. 

// [in]  new_size: Neue größe. 

// 

// [ret] Das neue Array. 

// ///////////////////////////////////////////////// 

template<typename T> inline T *resize (T *array, unsigned long old_size, unsigned long new_size)
{
        // Speicher reservieren für das neue Array.

    T *temp = new T[new_size];
    
        // Den Inhalt von 'array' in 'temp' kopieren.

    ::memcpy (temp, array, old_size);
    
        // Altes Array löschen.

    delete [] array;

        // Neues zurück geben.

    return (temp);
}


Wie schon gesagt, hat nichts mit Deinem Problem speziell zu tun, aber mir brennt der alte C-Kram richtig in den Augen :angel:

CW_Kovok

Alter Hase

  • »CW_Kovok« ist der Autor dieses Themas

Beiträge: 836

Wohnort: nähe Bonn

Beruf: Schüler

  • Private Nachricht senden

5

29.12.2005, 00:21

soweit ich weiß, benutzt new intern nur malloc bzw. deren eigentlich funktion GlobalAlloc(oder so), daher egal. Außerdem ist bei new nicht gesichert, dass der speicher nur vergrößert wird, und man muss immer erstmal alles vom alten in den neuen speicherbereich kopieren
Was es alles gibt, das ich nich brauche - Aristoteles

Anonymous

unregistriert

6

29.12.2005, 10:13

CW_Kovok
Ich glaube Du solltest Dir mal "Effektiv C++ Programmieren" anschaffen ;)

Vorteile von New:
1. Constructor wird aufgerufen
2. überladbar
3. malloc wird intern sogut wie gar nicht mehr aufgerufen, nur in älteren Versionen, es wird direkt HeapAlloc aufgerufen.
4. ist kein veralteter C-Müll bei dem man Casten muss was sehr auf die Performance geht.

Natürlich geht dann der Speicher flöten. Überleg mal folgende Situation:
Du suchst ein Blatt Papier zum drauf kirtzeln, findest aber kein Leeres. Also was denkste? "Joah ich geh da mit malloc dran und fertig", tja denkste: Der alte Scheiß bleibt ja da und du hast keine neuen Speicherbereich und musst diesen erst überschreiben. Sieht sehr schön aus *würg*

Für sowas schreibt man sich ein resize (like me) das dein realloc ersetzt.

Außerdem ist C/C++ Mischmasch ekelerregend.

CW_Kovok

Alter Hase

  • »CW_Kovok« ist der Autor dieses Themas

Beiträge: 836

Wohnort: nähe Bonn

Beruf: Schüler

  • Private Nachricht senden

7

29.12.2005, 10:22

dass ist ja alles schön und gut, nur warum verliert meine Variable jetzt ihren wert?
Was es alles gibt, das ich nich brauche - Aristoteles

CW_Kovok

Alter Hase

  • »CW_Kovok« ist der Autor dieses Themas

Beiträge: 836

Wohnort: nähe Bonn

Beruf: Schüler

  • Private Nachricht senden

8

29.12.2005, 11:52

ok habe das problem gelöst, auch wenn ich jetzt filestreams benutze...

so besser?

C-/C++-Quelltext

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
namespace CW_Core
{
    template<class T> class TArray;
}

template<class T> class CW_Core::TArray
{

public:

    TArray(int nQuantity); //

    TArray(const TArray& rkObject); //

    ~TArray(); //


    TArray<T>&  operator=(const TArray<T>& rkObject);
    T&          operator[](int i);
    const T&    operator[](int i) const;

    T*          GetArray();
    const T*    GetArray() const;


    void        Append(const T& rtElement);
    void        SetElement(int i, const T& rtElement);
    void        Remove(int i);
    void        RemoveAll();
    void        SetMaxQuantity(int nNewMaxQuantity);

    int         GetMaxQuantity() const;

private:

    int m_nMaxQuantity;
    T*  m_atArray;
};

template<class T> CW_Core::TArray<T>::TArray(int nQuantity = 1)
{
    if(nQuantity<1)
        nQuantity = 1;

    m_nMaxQuantity  = nQuantity;
    m_atArray       = 0;

    SetMaxQuantity(m_nMaxQuantity);
}

template<class T> CW_Core::TArray<T>::TArray(const TArray& tObject)
{
    m_atArray = 0;
    *this = tObject;
}

template<class T> CW_Core::TArray<T>::~TArray()
{
    delete[] m_atArray;
}

template<class T> inline CW_Core::TArray<T>& CW_Core::TArray<T>::operator=(const TArray<T>& rkObject)
{
    m_nMaxQuantity  = rkObject.m_nMaxQuantity;

    if(m_atArray)
        delete[] m_atArray;

    if(m_nMaxQuantity > 0)
    {
        m_atArray = new T[m_nMaxQuantity];

        for (int i = 0; i < m_nMaxQuantity; i++)
            m_atArray[i] = rkObject.m_atArray[i];
    }

    else
    {
        m_atArray = 0;
    }

    return *this;
}

template<class T> inline T& CW_Core::TArray<T>::operator[](int i)
{
    if(i>m_nMaxQuantity-1)
        i = m_nMaxQuantity-1;

    return m_atArray[i];
}

template<class T> inline const T& CW_Core::TArray<T>::operator[](int i) const
{
    if(i>m_nMaxQuantity-1)
        i = m_nMaxQuantity-1;

    return m_atArray[i];
}

template<class T> inline T* CW_Core::TArray<T>::GetArray()
{
    return m_atArray;
}

template<class T> inline const T* CW_Core::TArray<T>::GetArray() const
{
    return m_atArray;
}

template<class T> inline void CW_Core::TArray<T>::Append(const T& rtElement)
{
    SetMaxQuantity(m_nMaxQuantity+1);
    m_atArray[m_nMaxQuantity-1] = rtElement;
}

template<class T> inline void CW_Core::TArray<T>::SetElement(int i, const T& rtElement)
{   
    if(i>=m_nMaxQuantity)
        Append(rtElement);

    else
    {
        SetMaxQuantity(m_nMaxQuantity+1);

        for(int j=m_nMaxQuantity-1; j>i; j--)
            m_atArray[j] = m_atArray[j-1];

        m_atArray[i] = rtElement;
    }
}

template<class T> inline void CW_Core::TArray<T>::Remove(int i)
{
    for(int j=m_nMaxQuantity-1; j>i; j--)
    {
        m_atArray[j-1] = m_atArray[j];
    }

    if(i<m_nMaxQuantity)
        SetMaxQuantity(m_nMaxQuantity-1);
}

template<class T> inline void CW_Core::TArray<T>::RemoveAll()
{
    SetMaxQuantity(0);
}

template<class T> inline void CW_Core::TArray<T>::SetMaxQuantity(int nNewMaxQuantity)
{
    if(nNewMaxQuantity)
    {
        if(!m_atArray)
            m_atArray = new T[nNewMaxQuantity];

        else
        {
            T* tmp = new T[m_nMaxQuantity];

            memcpy((void*)tmp, (const void*)m_atArray, sizeof(T)*m_nMaxQuantity);

            delete[] m_atArray;

            m_atArray = new T[nNewMaxQuantity];

            memcpy((void*)m_atArray, (const void*)tmp, sizeof(T)*m_nMaxQuantity);

            m_nMaxQuantity = nNewMaxQuantity;

            delete[] tmp;
        }
    }

    else
    {
        delete[] m_atArray;
        m_atArray       = 0;
        m_nMaxQuantity  = 0;
    }
}

template<class T> inline int CW_Core::TArray<T>::GetMaxQuantity() const
{
    return this->m_nMaxQuantity;
}
Was es alles gibt, das ich nich brauche - Aristoteles

Werbeanzeige