This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.
| Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
|---|---|---|
| Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |
| Other format: | [Raw text] | |
Dear gcc-developers, The attachment is a sample source code that shows an error of gcc-optimization(O2, O3 and Os). gcc version 4.4.7 20120313 Best regards, chenzhi
//============================================================================
// Name : hello.cpp
// Author : chenzhi
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C, Ansi-style
//============================================================================
#include <stdio.h>
template<typename _Type> class array_t
{
size_t m_item_count;
_Type* m_item_array[15];
public:
array_t(void){
m_item_count = 0;
}
~array_t(void){
}
size_t append_item(_Type *_ptr){
m_item_array[m_item_count] = _ptr;
return ++m_item_count;
}
size_t get_count(void) const {
return m_item_count;
}
const _Type& get_item(size_t _index) const {
return *m_item_array[_index];
}
_Type* find_item(_Type *_ptr) const {
for(size_t i = 0; i < m_item_count; ++i){
_Type *_item = m_item_array[i];
if(*_item == *_ptr)
return _item;
}
return 0;
}
};
struct IDString
{
unsigned char m_id;
unsigned char m_len;
char m_str[6];
friend bool operator ==(const IDString &_r1, const IDString &_r2){
return (_r1.m_id == _r2.m_id) ? true : false;
}
};
class IDString_Array : public array_t<IDString>
{
IDString *find_idstring(size_t _id){
return find_item((IDString*)&_id);
}
public:
IDString_Array(IDString *_id_strings, size_t _count){
for(size_t i = 0; i < _count; ++i){
append_item(&_id_strings[i]);
}
}
~IDString_Array(void){
}
const char *get_default_string(void){
IDString *_ids = find_idstring(2);
return (_ids) ? _ids->m_str : "Cannot find!!!";
}
void print_all_elements(void){
size_t n = get_count();
for(size_t i = 0; i < n; ++i){
const IDString &_ids = get_item(i);
printf("id=%u\tlen=%u\tstring=\"%s\"\n", _ids.m_id, _ids.m_len, _ids.m_str);
}
}
};
static IDString g_idstrings[] = {
{1, 4, "id=1"},{2, 4, "id=2"},{3, 4, "id=3"},{4, 4, "id=4"},
{5, 4, "id=5"},{6, 4, "id=6"},{7, 4, "id=7"},{8, 4, "id=8"}
};
int main(void)
{
IDString_Array idstring_array(g_idstrings, sizeof(g_idstrings)/sizeof(g_idstrings[0]));
idstring_array.print_all_elements();
const char *_str = idstring_array.get_default_string();
int len = printf("%s\n", _str);
return len;
}
Attachment:
Makefile
Description: Binary data
| Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
|---|---|---|
| Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |