Resizing pointers

Hello!
i'm using this code for resizing pointers (i don't like to use realloc)
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
int CAccReader::ResizeAttribs(int nNewSize)
{
	if(nNewSize<0) return 0;
	if(m_nSizeAttrib == nNewSize) return 1;

	int nMinCache = min(nNewSize, m_nSizeAttrib);

	// ** Store the cache if posible
	CString** strAttrib;
	CString** strFormat;
	bool* bIsMultiData;
	int* nSizeData;
	int** nSizeSubData;
	CString**** strData;
	if(nMinCache>0)
	{
		strAttrib = new CString*[nMinCache];
		strFormat = new CString*[nMinCache];
		bIsMultiData = new bool[nMinCache];
		nSizeData = new int[nMinCache];
		nSizeSubData = new int*[nMinCache];
		strData = new CString***[nMinCache];
		for(int i = 0; i < nMinCache; i++)
		{
			strAttrib[i] = m_strAttrib[i];
			strFormat[i] = m_strFormat[i];
			bIsMultiData[i] = m_bIsMultiData[i];
			nSizeData[i] = m_nSizeData[i];
			nSizeSubData[i] = m_nSizeSubData[i];
			strData[i] = m_strData[i];
		}
	}

	// ** Delete current
	if(m_nSizeAttrib>0)
	{
		delete m_strAttrib;
		delete m_strFormat;
		delete m_bIsMultiData;
		delete m_nSizeData;
		delete m_nSizeSubData;
		delete m_strData;
	}

	// Create new if possible
	if(nNewSize > 0)
	{
		m_strAttrib = new CString*[nMinCache];
		m_strFormat = new CString*[nMinCache];
		m_bIsMultiData = new bool[nMinCache];
		m_nSizeData = new int[nMinCache];
		m_nSizeSubData = new int*[nMinCache];
		m_strData = new CString***[nMinCache];
		// ** Copy cache if is stored
		if(nMinCache>0)
		{
			for(int i = 0; i < nMinCache; i++)
			{
				m_strAttrib[i] = strAttrib[i];
				m_strFormat[i] = strFormat[i];
				m_bIsMultiData[i] = bIsMultiData[i];
				m_nSizeData[i] = nSizeData[i];
				m_nSizeSubData[i] = nSizeSubData[i];
				m_strData[i] = strData[i];
			}
			delete strAttrib;
			delete strFormat;
			delete bIsMultiData;
			delete nSizeData;
			delete nSizeSubData;
			delete strData;
		}
	}
	m_nSizeAttrib = nNewSize;
	return 1;
}


The class has this structure:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class CAccReader
{
public:
	CAccReader(void);
	~CAccReader(void);
	int m_nSizeAttrib;
	CString** m_strAttrib;
	CString** m_strFormat;
	bool* m_bIsMultiData;
	int* m_nSizeData;
	int** m_nSizeSubData;
	CString**** m_strData;
	int AddAttrib(LPCTSTR strAttrib, LPCTSTR strFormat, bool bIsMultiData);
	int ResizeAttribs(int nNewSize);
};


I resize for this way because I use the CString and other classes made for me.
This is the correct way for resizing pointers? Exist some method more faster?
Or this code is right?
Thanks! :)
Last edited on
new -> delete

new[] -> delete[]

lines 15 to 32 are useless. (copy first, then delete)
The copy could be done with memcpy.

Use std::vector instead
Last edited on
ne555 wrote:
Use std::vector instead


^ This ^

Also...

CString**** m_strData;

4-deep pointer? You must be doing something horribly wrong. Why do you need that many pointers?
"CString****"
this is because I need to save data, and sub data, and sub-sub data
For each "data", maybe has many lists, and for each list has many elements.
jmm... but.. std::vector can save other "std::vector"?
Topic archived. No new replies allowed.