c,c++ 기본/next step(15)
-
상속에 대한 얘기
안녕하세요 대학생 개발자입니다. 이번글에서는 상속에 관한 얘기를 좀 해보고 싶습니다. 사실 상속을 얘기하려면 생성자부터 모조리 다시 얘기를 해야됩니다.ㅠㅠ(물론 예전처럼 하지는 않을겁니다) 상속이란건 말그대로 그 객체의 모든 정보를 내가 이어 받는거죠. 그정보를 이어받아서 내가 추가를 하든 아니면 필요없는건 제외를 하든 그렇게 사용하라는겁니다. 뭐... 상속에서 public protected private상속 일때 접근을 어떻게하는가 이런건... 뭐 건너뛰겠습니다 그런걸 논할 글은 아니라고 생각합니다. 우선 상속을 할때 부모의 생성자가 존재한다면 그 밑의 클래스 즉 하위클래스들은 전부다 생성자가 존재해야됩니다. 이유는 당연하죠 하위 클래스의 생성자에서 상위클래스의 생성자를 호출해야되기 때문이죠. 그리고 ..
2021.11.24 -
class에 대한 얘기(2)
안녕하세요 대학생 개발자입니다. 이번글에서는 저번글에 이어서 클래스에 대한 정리를 좀더 해보려고합니다. 저번글에서는 기본 생성자와 소멸자에 대한 얘기를 해봤습니다. 그럼 다음으로 기본 복사생성자에 대한 얘기를 해보겠습니다. 우리가 코딩을할때 분명 복사생성자를 만든 기억이 없음에도 이런 복사생성자 호출이 가능해집니다. 그렇다는 말은 컴파일러가 복사생성자를 만들어 준다는 의미겠죠. 네. 여기까지가 c++을 훌륭히 배워오신 분들이 가지고 있는 지식입니다. 하지만 조금만 더 깊게 들어가보겠습니다. 어셈블리를 통해서 기본 복사생성자라는 것을 자세히 보면 그냥 memcpy호출과 동일합니다. 네... 이게 기본 복사생성자의 실체입니다. 그냥 복사를 드르르르륵 하고 있는 상황인겁니다. 아... 그럼 memcpy를 한번..
2021.11.24 -
template와 class에 대한 얘기(1)
안녕하세요 대학생 개발자입니다. 이번글에서는 template에 대해서 정리를 해보려고합니다. 우선 템플릿에 대해서 얘기하기 전에 우리가 c언어에서 부터 지금까지 모든 타입이라는 개념을 어떻게 해왔는지를 간단히 정리해보도록하겠습니다. 똑같은 기능을 변수 타입만 바꼈을때 똑같이 쓰는방법... c언어에서는 당연 다른 함수를 만들어야됬습니다. int add(int a, int b); float add(float a, float b); 이런 오버로딩의 개념은 c언어에서는 지원하지 않으니까요... c언어에서는 안타깝게도 이런방식을 써왔었습니다. int addInt(int a, int b); float addFloat(float a, float b); 그리고 c++에서는 이를 조금 보안해서 오버로딩이라는 개념을 지원..
2021.11.23 -
new와 delete에 대한 얘기
안녕하세요 대학생 개발자입니다. 이번에 다뤄볼 내용은 동적할당에 대한 얘기입니다. 할당에는 두가지 방법이 있을겁니다. 정적할당과 동적할당 이렇게 말이죠. 일반적으로 우리가 처음 C++을 배울때 main함수를 사용하게 될텐데요. 메인함수와 같은 함수 즉 지역내에 선언되는 변수들은 모두 정적할당을 통해서 얻어지는 메모리입니다. 컴파일러가 컴파일 타임에 어느정도를 할당해야될지 정해서 올려버리는 값들이죠. 마찬가지로 전역변수도 같습니다. 컴파일타임에 존재하는 변수이기 때문에 정적으로 공간이 할당되어 버리죠... 하지만 분명히 우리가 예상하지 않았던 크기의 공간이 필요하게 될 수도있습니다. 물론 스택으로 일부 커버를 할 수 있긴 합니다. 하지만... 100바이트면 충분할줄 알았던 것이 1000바이트가 되면요..?..
2021.11.20 -
메모리와 c++에 관한 얘기
안녕하세요 대학생개발자입니다. 이번 글에서는 메모리에 대한 얘기를 하려고합니다. 아참 예전에 메모리가 있지않았나요? 라고 생각하실 분들을 위해서 말씀드리지만 이 메모리는 예전의 메모리에 비해 많이 심도가 깊어졌습니다... 그럼 우선 시작하겠습니다. 우선 저번 글에서 좀 적었으면 싶었는데 너무 피곤한 탓에... 빨리 정리를 해서 못 정리한 내용에 대해서 간략하게 정리를 좀 하고 넘어가도록 하겠습니다. 저번 글에서는 file io에대해서 얘기를 해봤는데요 그럼 결국 다른 프로세스에 접근하는것도 file io이지 않겠는가 입니다. 다른 프로세스의 메모리에 접근하는것과 파일에 접근하는것 두개가 다른게 전혀없다는거죠 결국 메모리에 올려서 값을 쓰고 그걸 다시 돌려놓고를 할거기 때문이죠.. windows에서는 Wr..
2021.11.14 -
DLL,LIB, FILEIO에 대한얘기
안녕하세요 대학생개발자입니다. 이번 내용에서는 dll과 lib에 대한 차이점을 아주 간단하게 정리하고 FILE IO에 대해서 얘기를 좀 해보려고합니다. 음... 우선 우리가 dll이나 lib를 쓰는 이유가 뭘까에 대한 고민을 우선 해봐야겠는데요... 당연히 dll과 lib가 없으면 무조건적으로 안된다는 아닙니다. 왜냐면 소스를 그대로 주는 것과 dll, lib를 주는것과는 다를바가 없거든요... 어차피 내가 주려고 하는 라이브러리를 준다는 부분에서는 동일한거니까요 하지만 source code의 경우에는 역시... 고대로 전달하게 되면 내 모든것을 공개하는것과 동일합니다... 남이 내가 어떻게 코드를 짰는지 노출의 위험이 있는 것부터 시작해서 내가 만들어놓은 코드를 변경할 여지도 있고 이런 위험이 있습니다..
2021.11.13 -
게임서버 이론1
안녕하세요 대학생개발자입니다. 이번 글에서는 여러가지가 짬뽕이 될 것같은데요... 사실 이걸 c,c++로 넣어야될지 아니면 게임서버쪽으로 봐야될지 고민을 하다가 그냥... 이것만을 위해서 새로운 카테고리를 파기는 좀 그래서 그냥 c,c++로 넣어버렸습니다. 일단 시작하겠습니다. 음... 어떻게 시작해야될지를 모르겠습니다. 사실 이부분을 필기한지가 꽤나 시간이 지나서 무슨 말을 했었던건지도 기억이안나고... 필기도 너무 빠르게 해서 그런지 핵심내용이 많이 사라져있네요... 일단 서버의 경우에는... 대체적으로 하드웨어를 분산시키는 기법을 많이 사용하죠. 말그대로 분산서버인데요. 아마 구글링을 해보신 분들은 다 아시겠지만 뭐 채팅서버, 로그인서버, 로비서버, 게임서버, 월드서버, NPC서버 등등 다양한 서버..
2021.11.09 -
시간과 랜덤에 관한 얘기
안녕하세요 대학생 개발자입니다. 이번에는 시간과 랜덤에 대해서 정리를 좀 해보려고합니다. 음 무슨 이름이 드래곤볼에 나올거같은 방이름인데요... 우선 우리가 아는 시간 컴퓨터는 어떻게 알고있을까요? 사실 시간이라는 개념은 컴퓨터는 알수가 없죠 왜냐면 사람이 기준을 정했으니까요... 우선 1초라는게 뭔지 알아봐야 될 것 같은데요 실제 1초의 기준입니다. "초는 세슘-133 원자의 바닥상태에 있는 두 초미세 준위간의 전이에 대응하는 복사선(방사)의 9,192,631,770 주기의 지속시간이다." 그럼 컴퓨터가 진짜 1초를 세기위해서 세슘-133원자의 바닥상태에 있는 어쩌고 저행동을 할까요?? 그럴리가없죠... 절대로... 컴퓨터가 시간을 재는 방법은 cpu가 한번 진동하는 틱이라고하죠? 클럭이요.. 그래서 ..
2021.11.08 -
문자열과 해시에 대한 얘기
안녕하세요 대학생 개발자입니다. 이번 글에서는 표준 c에서 제공하는 문자열과 위험한 부분 그리고 우리는 어떻게 써야되는지에 대한 내용과 해시에 대한 내용을 간단하게 정리하고 넘어가고자 합니다. 우선 windows의 visual studio환 경에서 문자열을 쓰다보면 scanf함수가 작동이 안되는 경우를 확인할 수 있습니다. 이렇게 말이죠... 이건 사실 다른 컴파일러에서는 에러사항은 아닙니다. 하지만 windows의 vs가 특별히 안전한 버전인 _s를 붙인 버전을 제공하고 있기 때문인데요... 사실... 문자열의 비교를 위한 strcmp, 그리고 문자열의 길이를 알아내는 strlen과 같은 함수들은 _s 버전이 존재하지 않습니다 그이유는 뭘까요? 우선... _s가 존재하는 기준에 대해서 알아봐야되겠는데요..
2021.11.07 -
메모리에 대한 얘기
안녕하세요 대학생개발자입니다. 그럼 계속 이어서 하도록하겠습니다. 저번 글에서는 alignas키워드에 대한 내용을 좀 다뤘는데요. 이번 내용에서는 메모리에 대해서 좀 알아보려고합니다. 우선 메모리에 대해 하기전에 추가로 내용을 더 정리하고 가면요 heap의 경우에는 alignas키워드를 사용해도 맞지 않습니다(malloc의 경우에는 안맞습니다). 하지만 _aligned_malloc() 이라는 함수를 제공하고 2번째 인자로 어디에 맞출것인지에 대한 인자를 받습니다. 그럼 _aligned_malloc은 어떻게 만드는걸까요? 내가 내가 어떤 구조체가 필요한데 이걸 64바이트에 맞춰버리고싶습니다. 그럼 실제로 할당하는 양은 64바이트에 + 64이하 바이트들을 전부다 1로 세팅한 값을 더해버립니다. 즉 63을 더..
2021.11.07 -
바이트 패딩과 캐시에 대한 얘기
안녕하세요 대학생 개발자입니다 오늘은 바이트 패딩이 왜 그 타입의 사이즈의 경계에 맞게 나오게 되는지 이유를 좀 알아보려고합니다. 네 먼저 저번 내용을 간단히 요약해본다면 어떠한 구조체가 있을때 char의 경우에는 1바이트의 경계에 세우면 반으로 잘리지 않기 때문에 사실상 어디에 있어도 상관이 없습니다. short, int, double등과 같이 1바이트가 아닌 변수들은 각자의 사이즈의 경계에 서야 된다는것도 알았습니다. 그리고 구조체 자체의 패딩또한 가장 큰 변수에 맞춰서 패딩이 된다는 것도 알았습니다. 그럼 여기서 한단계만 더 들어가보겠습니다 왜 이렇게 맞춰야하는가입니다. 이유는 단순한데요 캐시라인이 64바이트이기 때문입니다. 물론 어느 아키텍쳐냐에 따라 달라지겠지만 요새 cpu들은 대부분 64byt..
2021.11.06 -
전처리기, 바이트패딩룰에대한 얘기
안녕하세요 대학생개발자입니다. 저번글에서는 함수에 대한 내용을 다뤄봤습니다. C언어의 내용을 순서대로 나열해보자면 이쯤에서 포인터를 다루는게 일반적이겠으나... 포인터변수에 대한 내용은 지금 다룰 내용은 아니고 그보다 더 원초적이고 기본이 될 만한 지식을 다지는 것이 더 중요합니다. 그럼 시작하도록 하겠습니다. 우선 전처리기에 대한 내용이 아주아주아주 살짝 나오게 될것입니다. 그리고 typedef와 const와도 좀 비교가 될 것이구요. 우선 우리가 자주 쓰는 전처리기 명령어 #include 와같은 이런 #이 붙은 명령어들이 전처리기 명령어인데요. 저도 사실 전처리기에 대해서는 잘 모르기 때문에 간단하게 정리하려고 합니다. 우선 말 그대로 전 처리기죠 컴파일 이전에 정해진 명령어들을 수행하는 녀석입니다...
2021.11.01 -
함수에 대한 얘기
안녕하세요 대학생 개발자입니다. 이어서 C언어 기본에 대해서 얘기를 하려고 합니다. 점프 테이블을 끝으로 마무리를 했던 것 같은데요... 아쉽게도 오늘도 점프 테이블이 등장할 겁니다. 하지만 이번에는 스위치에 대한 점프 테이블은 아니고요. 함수에 대한 점프 테이블인데요. 물론 이 점프 테이블의 경우에는 디버그 빌드에서만 나오는 거긴 하지만... 링킹 타임을 획기적으로 줄이는 방법이기 때문에 이렇게 했답니다. 그럼 우리는 call Foo라는 코드를 전부 점프 테이블로 가게 만들고 점프 테이블의 값만 수정해주면 끝나기 때문이죠. 네 뭐... 점프 테이블은 이런 식으로 생겨먹었습니다. 이상한 다른 함수들이 많은데 제가 따로 뭐 하고 있는 데다가 그냥 점프 테이블 보려고 잠시 디버그로 바꾼 거라서 저런 겁니다...
2021.11.01 -
연산자와 조건문에 대한 얘기
안녕하세요 대학생 개발자입니다. 이번 글에서는 연산자와 조건문에 대한 얘기를 간단히 해보려고합니다. ----- cpu는 1,0 으로만 읽을 수 있는데 음수와 뺄셈을 어떻게 하는지..? 우선 이전 글에서 변수에 대한 언급을 했었습니다. 변수는 공간일 뿐이라고 말씀드렸었던 부분이 있는데요. 그저 공간밖에 존재하지 않는 상황에서 연산은 어떻게하며 뺄샘과 같은 처리는 어떻게 하는지 의문이실겁니다. 시작 전에 말씀드리지만 메모리에는 음수라는 개념이 존재하지 않습니다. 그저 1,0 만 존재하는 공간에 불과합니다. 이를 음수라고 정의하는건 사람이 한것이죠. 우선 시작하겠습니다. + 연산은 2진수에서도 10진수와 동일합니다 1111 + 1 = 10000 이죠 1010 + 101 = 1111 이죠 하지만 뺄샘같은 경우는..
2021.10.29 -
변수와 타입, 키워드에관한 얘기
안녕하세요 대학생 개발자입니다. 이번 글에서는 c언어의 기본을 공부할 때 간략하게 듣긴 했지만 정확한 원리에 대한 생각을 해보지 않았을 부분에 대해서 언급을 하려고 합니다. 게임 서버를 개발하실 정도의 실력이 가지고 계신 분들이라면 다 아실법한 내용들이 위주로 나오게 됩니다. 그러니까 아시는 분들은 어느 정도 걸러서 들으셔도 좋습니다. 게임서버를 개발하며 가장 중요한건 최적화와 안정성입니다. 알고리즘에 대한 최적화 부분도 상당부분 중요합니다. 하지만 당연히도 알고리즘만으로 최적화 할 수 있는 부분은 한계가 있습니다. 우선 우리는 메모리라는 녀석이 어떻게 생겨먹은 녀석이고 어떻게 사용할때 효율적인지를 알아보기 위해서 메모리에 대해 알아보고 갈겁니다. 그러다 보니 자연스럽게 언어와 관계없이 메모리 관점에서의..
2021.10.28