• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

타입 불일치 오류

21.11.30 15:39 작성 조회수 137

0

#include <iostream>
#include <cassert>
#include <initializer_list>

class IntArray {
private:
    unsigned int m_length{ 0 };
    int* m_data = nullptr;

public:
    IntArray(unsigned length)
        :m_length(length) {
        m_data = new int[length];
    }
    
    // ERROR: C4267 - 'argument': conversion from 'size_t' to 'unsigned int', possible loss of data
    IntArray(const std::initializer_list<int>& list)
        :IntArray(list.size()) {
        unsigned int count{ 0 };
        for (auto& element : list) {
            m_data[count++] = element;
        }
    }

    ~IntArray() {
        delete[] this->m_data;
    }
};

int main() {
    IntArray int_array{ 0,1,2,3,4 };
}

 

안녕하세요.

MSVS debug x64 에서 빌드하면 이런 오류가 나옵니다.

 

----

C4267 : 'argument' - conversion from 'size_t' to 'unsigned int', possible loss of data

----

 

dubug x86 에서는 오류가 안나오네요.

이유가 궁금합니다. 감사합니다.

답변 1

답변을 작성해보세요.

0

상욱님의 프로필

상욱

2021.12.01

안녕하세요

Warning이 뜨는 이유는 라인 마지막에 있는 list.size() 에서

std::size의 반환 값이 size_t 형이라 그렇습니다.

size_t는 x64일때 unsigned __int64(8byte), x86에서는 unsigned int(4byte)로 typedef되어있습니다.

그래서 x64에서 빌드하면 8byte 값이 4byte로 conversion되면서 소실될 수 있다고 Warning이 나오게 됩니다. 

이후 강의에 size_t가 자세히 언급됩니다.