Странное поведение функции с переменным количеством аргументов
02.03.2013, 15:09. Показов 876. Ответов 1
Честно говоря даже не знаю где проблема, надеюсь вы поможете разобраться. Есть функция LinkStr для соединения нескольких строк:
Кликните здесь для просмотра всего текста
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| // LinkStr for more than two strings
template <typename type1>
auto_arr<type1> LinkStr(int argc, const type1 *argv,...) {
type1 **in_add=(type1**) &argv;
type1 **temp=in_add+1;
std::cout<<*temp<<std::endl;
int res_len(0); sequence<int> lengths;
for(int i(0); i<argc; i++) {
int curr_len=GetStrLen(in_add[i]);
lengths.addLast(curr_len);
res_len+=curr_len;
}
// create new string
auto_arr<type1> res_str=new type1[res_len+1];
// copy all string to new
res_str[res_len]=0; int curr_len(0);
for(int i(0), curr_full_len(0); i<argc; i++) {
curr_len=lengths.popFirst();
for(int j(0); j<curr_len; j++) res_str[curr_full_len+j]=in_add[i][j];
curr_full_len+=curr_len;
}
return res_str;
} |
|
Вот в таком контексте её тестирую:
Кликните здесь для просмотра всего текста
| C++ | 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
| #include "..\cml\cml.h"
#include <windows.h>
#pragma comment(lib, "cml.lib")
char *gloabal_var=0;
int main() {
std::cout<<"test that it's works!"<<std::endl;
cml::auto_arr<char> str_1, str_2;
str_1=cml::IntToCh(213); str_2=cml::IntToCh(222);
std::cout<<"str_1="<<str_1<<std::endl;
std::cout<<"str_2="<<str_2<<std::endl;
gloabal_var=str_2;
char *str_3, *str_4;
str_3="213"; str_4="222";
std::cout<<cml::LinkStr<char>(2,
str_1,
str_2)
<<std::endl;
std::cout<<cml::LinkStr<char>(2, str_3, str_4)<<std::endl;
MessageBoxA(NULL, cml::LinkStr<char>(2, str_1, str_2), "with auto_arr<char>", MB_OK);
MessageBoxA(NULL, cml::LinkStr<char>(2, str_3, str_4), "without auto_arr", MB_OK);
std::cin.get();
return 0;
} |
|
Вот скрины после теста:
как видно на скринах вторая строка заменяется на какой-то бред, но для константных строк такого не происходит насколько видно в дебагере и с помощью инструкций:
| C++ | 1
2
| std::cout<<"str_1="<<str_1<<std::endl;
std::cout<<"str_2="<<str_2<<std::endl; |
|
на вход LinkStr передаются нормальные адреса строки, а дальше из стека извлекается совсем не тот адрес который должен (я его присвоил global_var для сравнения):
вот здесь на всякий случай класс auto_arr, может быть в нём проблема:
Кликните здесь для просмотра всего текста
| C++ | 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
| template <typename ptr_type>
class auto_arr {
struct ptr_case {
ptr_type *ptr;
UINT use_counter;
ptr_case(ptr_type *in_ptr) :ptr(in_ptr), use_counter(1) {}
~ptr_case() { delete [] ptr; }
};
public:
auto_arr(ptr_type *in_ptr=0);
~auto_arr();
auto_arr(const auto_arr &in_obj);
auto_arr & operator =(const auto_arr &in_obj);
auto_arr & operator =(ptr_type *in_ptr);
ptr_type & operator [](int index) const;
ptr_type * operator ->() const;
ptr_type & operator *() const;
operator ptr_type*() const;
ptr_type * release();
auto_arr & operator+=(int index);
auto_arr & operator-=(int index);
auto_arr operator++(int);
auto_arr operator--(int);
auto_arr & operator++();
auto_arr & operator--();
bool operator ==(const auto_arr &in_obj) const;
bool operator !=(const auto_arr &in_obj) const;
bool operator ==(const ptr_type *in_ptr) const;
bool operator !=(const ptr_type *in_ptr) const;
template <typename _ptr_type> friend
bool operator==(const _ptr_type *in_ptr, const auto_arr<_ptr_type> &in_obj);
template <typename _ptr_type> friend
bool operator!=(const ptr_type *in_ptr, const auto_arr<_ptr_type> &in_obj);
private:
ptr_case *pointer;
ptr_type *curr_add;
};
template <typename ptr_type>
auto_arr<ptr_type>::auto_arr(ptr_type *in_ptr) {
pointer=new ptr_case(in_ptr);
curr_add=in_ptr;
}
template <typename ptr_type>
auto_arr<ptr_type>::~auto_arr() {
pointer->use_counter--;
if(pointer->use_counter==0) delete pointer;
}
template <typename ptr_type>
auto_arr<ptr_type>::auto_arr(const auto_arr &in_obj) {
pointer=in_obj.pointer; pointer->use_counter++;
curr_add=in_obj.curr_add;
}
template <typename ptr_type>
auto_arr<ptr_type> & auto_arr<ptr_type>::operator =(const auto_arr &in_obj) {
if(pointer->use_counter==1) delete pointer;
pointer=in_obj.pointer; pointer->use_counter++;
this->curr_add=in_obj.curr_add;
return *this;
}
template <typename ptr_type>
auto_arr<ptr_type> & auto_arr<ptr_type>::operator =(ptr_type *in_ptr) {
if(pointer->use_counter==1) delete pointer;
pointer=new ptr_case(in_ptr);
curr_add=in_ptr;
return *this;
}
template <typename ptr_type>
ptr_type & auto_arr<ptr_type>::operator [](int index) const {
return curr_add[index];
}
template <typename ptr_type>
ptr_type * auto_arr<ptr_type>::operator ->() const {
return curr_add;
}
template <typename ptr_type>
ptr_type & auto_arr<ptr_type>::operator *() const {
return *curr_add;
}
template <typename ptr_type>
auto_arr<ptr_type>::operator ptr_type*() const {
return curr_add;
}
template <typename ptr_type>
ptr_type* auto_arr<ptr_type>::release() {
ptr_type *tmp=pointer->ptr;
pointer->ptr=0; curr_add=0;
if(pointer->use_counter==1) delete pointer;
else pointer->use_counter--;
pointer=new ptr_case(0);
return tmp;
}
template <typename ptr_type>
auto_arr<ptr_type> & auto_arr<ptr_type>::operator+=(int index) {
curr_add+=index; return *this;
}
template <typename ptr_type>
auto_arr<ptr_type> & auto_arr<ptr_type>::operator-=(int index) {
curr_add-=index; return *this;
}
template <typename ptr_type>
auto_arr<ptr_type> auto_arr<ptr_type>::operator++(int) {
auto_arr<ptr_type> prev_obj(*this);
curr_add++; return prev_obj;
}
template <typename ptr_type>
auto_arr<ptr_type> auto_arr<ptr_type>::operator--(int) {
auto_arr<ptr_type> prev_obj(*this);
curr_add--; return prev_obj;
}
template <typename ptr_type>
auto_arr<ptr_type> & auto_arr<ptr_type>::operator++() {
curr_add++; return *this;
}
template <typename ptr_type>
auto_arr<ptr_type> & auto_arr<ptr_type>::operator--() {
curr_add--; return *this;
}
template <typename ptr_type>
bool auto_arr<ptr_type>::operator ==(const auto_arr &in_obj) const {
return this->curr_add==in_obj.curr_add;
}
template <typename ptr_type>
bool auto_arr<ptr_type>::operator !=(const auto_arr &in_obj) const {
return this->curr_add!=in_obj.curr_add;
}
template <typename ptr_type>
bool auto_arr<ptr_type>::operator ==(const ptr_type *in_ptr) const {
return this->curr_add==in_ptr;
}
template <typename ptr_type>
bool auto_arr<ptr_type>::operator !=(const ptr_type *in_ptr) const {
return this->curr_add!=in_ptr;
}
template <typename _ptr_type>
bool operator==(const _ptr_type *in_ptr, const auto_arr<_ptr_type> &in_obj) {
return in_ptr==in_obj.curr_add;
}
template <typename _ptr_type>
bool operator!=(const _ptr_type *in_ptr, const auto_arr<_ptr_type> &in_obj) {
return in_ptr!=in_obj.curr_add;
} |
|
А вот для такого кода:
Кликните здесь для просмотра всего текста
| C++ | 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
| LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
switch (message) {
case WM_PAINT:
{
#define STR_LEN 9
HDC hDC = BeginPaint(hWnd, &ps);
ABC *char_widths=new ABC[26+1];
wchar_t *out_str=L"some text";
HFONT my_font=CreateFont(50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
SelectObject(hDC,(HGDIOBJ)my_font);
GetCharABCWidths(hDC, L'a', L'z', char_widths);
int length(0);
for(int i(0); i<STR_LEN; i++) {
ABC *currABC=char_widths+static_cast<int>(out_str[i]-L'a');
length+=(currABC->abcA + currABC->abcB + currABC->abcC);
cml::auto_arr<wchar_t> abcAstr, abcBstr, abcCstr;
abcAstr=cml::IntToWch(213, 10, true);
abcBstr=cml::IntToWch(16, 10, true);
abcCstr=cml::IntToWch(36, 10, true);
MessageBox(hWnd,
cml::LinkStr(9,
L"A = ", abcAstr, L";\n",
L"B = ", abcBstr, L";\n",
L"C = ", abcCstr, L";"
),
cml::IntToWch(i), MB_OK);
}
MessageBox(hWnd, cml::IntToWch(length), L"full len=", MB_OK);
TabbedTextOut(hDC, 10, 10, out_str, 9, 0, 0, 0);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
} |
|
на котором проблема изначально проявилась (до этого я тестировал только на константных строках) выдаёт что-то ещё более непонятное:

вот ещё функция IntToCh на всякий случай:
Кликните здесь для просмотра всего текста
| C++ | 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
| #define MIN_SIGNED_LINT_VAL 0x8000000000000000
#define DOUBLE_BIAS 0x3FF
#define EXP_MASK 0x7FF0000000000000
#define P_INF 0x7ff0000000000000
#define N_INF 0xfff0000000000000
auto_arr<char> IntToCh(LINT in_val, int base=10, bool show_plus=false);
auto_arr<char> IntToCh(LINT in_val, int base, bool show_plus) {
if((base<=0)||(base>16)) throw exception(EX_LOC, EX_INCORRECT_ARGUMENT);
if(in_val==MIN_SIGNED_LINT_VAL) throw exception(EX_LOC,EX_TYPE_OVERFLOWING);
if(in_val==0) return CloneStr("0");
stack<int> digits; bool is_neg(false);
if(in_val<0) is_neg=true, in_val*=(-1);
while(in_val) {
digits.push(in_val%base);
in_val/=base;
}
bool add_len=is_neg||show_plus; int len=digits.size()+add_len;
char *new_str=new char[len+1];
for(int i(add_len); i<len; i++) {
int curr_digit(digits.pop());
if(curr_digit<10) new_str[i]=char(int('0')+curr_digit);
else new_str[i]=char(int('a')+curr_digit-10);
}
if(add_len) new_str[0]=(is_neg?'-':'+');
new_str[len]=0; return auto_arr<char>(new_str);
} |
|
Надеюсь кто-то это всё же прочитает
0
|