c,c++ 기본(19)
-
c_4
안녕하세요 대학생개발자입니다. 이번글에서는 반복문에 대해서 간단히 알아보겠습니다. 반복문이란 말 그대로 코드가 반복되는 구간을 의미하는거겠죠... 그리고 거의 모든 반복에는 조건이 있습니다. 우선 반복문에 어떤것들이 있는지부터 봐야할 것 같습니다. 첫번째로 for문이 있습니다. 두번째로 while문이 있습니다. 세번째로 do while문이 있습니다. 먼저 조건문에 공통적으로 들어가는 구성에 대한 얘기를 먼저 해보겠습니다. 1. 초기식 2. 조건식 3. 증감식 조건식에는 위의 세가지 구성이 들어가는데요. 초기식의 경우에는 말그대로 조건식에서 사용될 값을 초기화 해주는 부분입니다. 초기화되지 않은 값을 사용하게되면 visual studio에서는 컴파일 에러를 뱉어주지만 그외 다른 컴파일러에서는 에러를 뱉어주..
2022.05.10 -
c_3
안녕하세요 대학생 개발자입니다. 이번 글에서는 이제 모든 코드에 로직이라는것을 결정하는 조건문이나 반복문에 대해서 알아볼거에요. 우선 조건문이라는게 무엇일까요? 말 그대로 어떤 조건에서 실행하고 어떤 조건이 아니면 실행하지 않겠다는 의미로 해석이 되는데요. 우선 조건문은 크게 두가지로 쓰이는데요 첫번째는 if문이고 두번째는 switch문입니다. if문의 경우에는 true와 false로 떨어지는 하나의 명제를 넣어서 그 명제가 참이라면 실행하고 참이 아니라면 실행하지 않는 구문이에요. 물론 if문이라고 해서 꼭 if만 있는건 아닙니다. if 조건에 해당하지 않는 경우에만 실행하고 싶은 부분이 분명 있기 때문에 else라는 키워드도 같이 제공하고 있습니다. if(조건){ 실행블럭 } else{ 실행블럭 } ..
2022.05.10 -
c_2
안녕하세요 대학생개발자입니다. 이번글은 저번글에 이어서 C언어에대한 기본 두번째 글입니다. 사실 C언어라는게 막상 접해보면 그렇게 어렵지 않은데 많은분들이 어려운줄 알고계셔요 그래서 어떻게하면 쉽게 설명할 수 있을까 고민하며 적고있습니다. 이번글에서는 자료형에 대한 얘기와 함께 데이터가 실제 어떻게 저장되는지에 대한 얘기를 좀 해볼겁니다. 자료형은 간단히 말하면 어떤 방식으로 이 데이터를 저장할거냐 혹은 표현할거냐 입니다. 저 방식이라는 것에는 다양한게 존재할수있는데요. 첫번째로 데이터를 크기에 따라 나누는 방법이 있습니다. 두번째로 데이터의 종류에 따라 나누는 방법이 있습니다. 먼저 데이터를 크기에 따라 나누는 방법인데요 여러분이 계산기를 사려고 한다고 생각해봅시다. 난 3자리 이상의 연산을 할일이 없..
2022.05.08 -
c_1
안녕하세요 대학생 개발자입니다. 오랜만에 돌아왔습니다. 그리고 진짜 기본이라는 새로운 카테고리를 가지고 돌아왔습니다. 이 카테고리는 c언어를 처음하시는 분들을 위한 카테고리입니다. 만약 나는 이런거 볼 레벨은 아닌데...? 하시는분들과 서버 개발일지를 보러 오셨다면 과감히 좋아요를 누르고 뒤로가기를 눌러주시면 감사하겠습니다. 먼저 변수에 대한 얘기를 해보려고합니다. c언어의 책에 뭐라고 써져있는지 모르겠지만 변수는 사실 정해진 크기의 값을 담을 수 있는 공간입니다. 이렇게 말로만 말씀을 드리면 이해가 안되실겁니다... 지금 제입장에서는 너무나도 당연히 받아들여지는 부분이 입문한지 얼마안되신 분들께는 어려운 내용이니 말이죠. 우선 변수를 선언하는 방법은 다음과 같습니다. 타입 이름 = 값;(int a = ..
2022.05.04 -
상속에 대한 얘기
안녕하세요 대학생 개발자입니다. 이번글에서는 상속에 관한 얘기를 좀 해보고 싶습니다. 사실 상속을 얘기하려면 생성자부터 모조리 다시 얘기를 해야됩니다.ㅠㅠ(물론 예전처럼 하지는 않을겁니다) 상속이란건 말그대로 그 객체의 모든 정보를 내가 이어 받는거죠. 그정보를 이어받아서 내가 추가를 하든 아니면 필요없는건 제외를 하든 그렇게 사용하라는겁니다. 뭐... 상속에서 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