프로그래밍/C++2015. 3. 23. 10:13

갑자기 C++ 소스를 봐야 하는데, 워낙 양이 많아서 전체적으로 틀을 보고 싶은데, class diagram이 있었음 했다.

그런데 그리자니... 노동력이 많이 들어가고... 그래서 찾아보았다.

우선 autodia라는 툴이 있는데, 설치가 조금 까다롭다.(적어도 perl을 안 써본 사람한테는)

그리고 무엇보다, 동작을 제대로 안한다.(이건 내 경우에 한해서)

template이라는 툴까지 잘 깔고 문제 없는 듯 한데, 정작 language를 인식 못한다.(혹시 db를 어디 깔아야 되는건가????)



그래서 이리 저리 찾다가 완벽하지는 않은데, 나름 유용한 툴이 있다.

cpp2dia(http://cpp2dia.sourceforge.net/)라고 

소스 받고 설치 후에 c++소스가 있는 곳에 가서 다음과 같이 실행하면 된다.



tclsh $설치경로/cpp2dia.tclssh 



그러면 output.dia라는 파일이 나오고, 이를 dia-normal 같은 어플리케이션을 통해서 보면된다.(jpeg, png로도 export가능하다)


올해 안에 python으로 비슷한 걸 한번 만들어봐야겠다.

'프로그래밍 > C++' 카테고리의 다른 글

생성자 호출 방법  (0) 2015.03.19
[에러] jump to case label -fpermissive  (0) 2014.02.19
[template]템플릿 예제  (0) 2013.08.04
null pointer vs stray pointer  (0) 2013.05.26
[c++] malloc/free 대신 new/delete을 쓰는 이유?  (0) 2013.05.18
Posted by code cat
프로그래밍/C++2015. 3. 19. 18:38

출처: C++ 에스프레소

생성자 호출 방법 중 실수할 수 있는 경우가 있다.

Car c1;                         //default constructor 호출
Car c2();                       // c2()라는 함수 원형선언, constructor 호출 아님!
Car c3(100, 3, "white");      // constructor 호출
Car c4 = Car(0, 1, "blue");   // 먼저 임시 객체를 만들고 이것을 c4에 복사

2번째의 경우 아무생각없이 하다가 쓸 수 있는 방법인데, 잘못된 방법이다.

'프로그래밍 > C++' 카테고리의 다른 글

[c++] UML autogenerator  (2) 2015.03.23
[에러] jump to case label -fpermissive  (0) 2014.02.19
[template]템플릿 예제  (0) 2013.08.04
null pointer vs stray pointer  (0) 2013.05.26
[c++] malloc/free 대신 new/delete을 쓰는 이유?  (0) 2013.05.18
Posted by code cat
프로그래밍/C++2014. 2. 19. 08:51

jump to case label -fpermissive


라고 나오는 에러는 흔히 switch-case문에서 case안에서 변수 선언 & 초기화를 할 때 나올 수 있는 문제이다.  이는 언어에서 정한 standard를 벗어난 경우이며, -fpermissive를 써서 호환가능하게 해 줄 수는 있지만, 쓰지 말고, 코드를 fix하는 방향으로 가도록 하자.


'프로그래밍 > C++' 카테고리의 다른 글

[c++] UML autogenerator  (2) 2015.03.23
생성자 호출 방법  (0) 2015.03.19
[template]템플릿 예제  (0) 2013.08.04
null pointer vs stray pointer  (0) 2013.05.26
[c++] malloc/free 대신 new/delete을 쓰는 이유?  (0) 2013.05.18
Posted by code cat
프로그래밍/C++2013. 8. 4. 19:31
예제: Teach yourself C++ in 21 days
Posted by code cat
프로그래밍/C++2013. 5. 26. 11:31
What is the difference between a null pointer and a stray pointer? 

Answer: When you delete a pointer, you tell the compiler to free the memory, but the pointer itself continues to exist. It is now a stray pointer. When you then write myPtr = 0; you change it from being a stray pointer to being a null pointer. Normally, if you delete a pointer and then delete it again, your program is undefined. That is, anything might happen—if you are lucky, the program will crash. If you delete a null pointer, nothing happens; it is safe. Using a stray or a null pointer (for example, writing myPtr = 5;) is illegal, and it might crash. If the pointer is null, it will crash, another benefit of null over stray. Predictable crashes are preferred because they are easier to debug
Posted by code cat
프로그래밍/C++2013. 5. 18. 19:42
int *p1;
struct employee{
  char name[20];
  int age;
  float sal;
} *p2;

p1 = new int;  //allocates 4 bytes
p2 = new employee; //allocates 32? bytes

int *p3;
p3 = new int[30];  //allocates memory for storing 30 inters

delete p1;
delete p2;
delete [] p3;

에 비해, malloc/free를 쓴다면....

p1 = (int*) malloc (sizeof(int));
p2 = (struct employee*) malloc (sizeof(struct employee));
p3 = (int*) malloc (sizeof(int) * 30);

malloc은 void 타입을 리턴하기 때문에, typecasting도 해 줘야 하고,  new의 경우, array dimension을 지원한다.  물론 variable dimension(dynamic size)도 지원한다.
훨씬 낫지 않은가?

그렇다면 new / free, malloc / delete 이런 조합도 가능할가? 안된다.

한가지 더...
new/delete는 오브젝트들을 생성/파괴 할 수 있는 반면, malloc/free는 메모리를 할당/해제 한다.  이는 중요한 부분으로 클래스 생성/파괴에서 new/delete을 쓰는 이유이다.
Posted by code cat
프로그래밍/C++2013. 5. 11. 14:53

c에서 c++함수 호출 시에 역시 extern "c"를 사용하게 되는데, 우선 예를 보자.

 

// gcd.h

#ifdef __cplusplus

extern "C"

#endif

int gcd(int v1, int v2);

 

//gcd.cpp

#include <boost/math/common_factor.hpp>

#include "gcd.h"

extern "C" {

int gcd(int v1, int v2){

return boost::math:gcd(v1, v2);

}

}

 

//sample.c

#include <stdio.h>

#include "gcd.h"

int main()

{

printf("%d와 %d의 최대공약수는 %d(이)다.\n", 14, 35, gcd(14,35));

return 0;

}

 

(위의 예문은 "BINARY HACKS 해커가 전수하는 테크닉 100선, 진명조 옮김" page 74 ~ page 75에서 발췌한 내용이다.)

뭐 다 좋은데, C++에서 저렇게 extern "C"를 함수에 추가해 줄 수 있는 경우가 과연 많을까?  본인 경험상 라이브러리 함수로 받아 수정을 못하는 경우가 태반인데, 과연 저렇게 수정해서 쓸 수 있는 경우가 많을까 고민이다.

 

뭔가 잘못 알고 있는 건 아닐까? 누가 알면 알려줬으면 좋겠다.

Posted by code cat
프로그래밍/C++2013. 5. 11. 12:09

C++에서 C라이브러리 혹은 프로그램 링크시에 우리는 보통 다음과 같이 쓴다

 

extern "C" 함수리턴타입 함수이름(매개변수원형);

 

그런데 이걸 C에서 쓰듯 함수 콜 직전에 불러준다거나 하면(그리 추천하는 방법은 아니다)

 

'expected unqualified-id before string constant' 

 

라는 GR같은 에러가 난다.

이건 C++에서는 클래스 정의나 함수구현 부분에서 "C"코드를 허락하지 않기 때문이다.

 

Posted by code cat
프로그래밍/C++2013. 2. 2. 20:32

예를 들어 c에서 라이브러리로 제공되는 함수가 있다고 하면 c++에서 그냥 c 함수 부르듯이 하면 알 수 없는 link error를 뿜는다.  

어?   분명히 제대로 적었는데... 하고 헤매일텐데, 이것은 syntax error나 라이브러리 링크가 제대로 안되서 그런 것이 아니라, symbol 이름 안 맞아서 그렇다.  이럴 땐, c++ 에서

extern "C" 함수선언

이렇게 해주면 된다.

Posted by code cat
프로그래밍/C++2013. 1. 1. 02:20

class DefaultDevice : public Device {

public:

DefaultDevice() :

ui(new DefaultUI) {

}

...


1. base class 생성자 호출


2. 생성자가 실행되기 전에 초기화 실행

- 상수(const) 멤버 변수 와 같은 경우 변경이 불가능하나,  이런 방식을 통해 변경 시킬 수 있다.

- 클래스 멤버인 reference 를 초기화 시키는 경우


'프로그래밍 > C++' 카테고리의 다른 글

[c++] expected unqualified-id before string constant  (0) 2013.05.11
C++에서 C 함수 사용하기  (0) 2013.02.02
cout , endl 의 원리  (0) 2012.04.28
Android Framework 분석을 위한 C++ 4일차  (0) 2012.04.26
[C++] Smart Pointer  (0) 2012.04.25
Posted by code cat