회원 로그인
정보기억 정보기억에 체크할 경우 다음접속시 아이디와 패스워드를 입력하지 않으셔도 됩니다.
그러나, 개인PC가 아닐 경우 타인이 로그인할 수 있습니다.
PC를 여러사람이 사용하는 공공장소에서는 체크하지 마세요.
소셜네트워크 서비스를 통해서 로그인하시면 별도의 로그인 절차없이 회원서비스를 이용하실 수 있습니다.


최근 게시물

1.노션에서 작성 중

1.노션에서 작성 중

개편하기 전까지 노션에서 작성 중

2024.04.04//read more

2.ChatGPT

2.ChatGPT

OpenAI로 대규모 언어 모델대화형...

2023.03.16//read more

3.노코딩 게임 엔진 - 빌..

3.노코딩 게임 엔진 - 빌..

빌드 지원안드로이드iOS윈도우즈특이사...

2023.03.14//read more

4.(완료) 미접속 회원 정..

4.(완료) 미접속 회원 정..

[완료] 36명의 회원을 정리하였습니...

2023.02.16//read more

5.매뉴얼 플러스 - 전자제..



안정적인 DNS 서비스 DNSEver
DNS Powered by DNSEver.com


동적할당

푸딩뱃살 | 2015.11.05 11:23 | 조회 1806
동적할당

{..}(스코프 영력) 함수 영역이 아닌 메모리 영력 -> 스택 메모리

#include <iostream> // cin, cout
#include <string> // string
#include <stdio.h> // rand()
#include <time.h> // time()
#include <Windows.h> // Sleep()

using namespace std;

class Bullet{
public:
    float speed;
    int damage;
};


void main() {
    // int a;
    // int* a = int *a (표기법의 차이일뿐 똑같음)

        // 동적 할당, 주소를 저장할 수 있는 int* a 생성(4byte) + new int 힙 메모리(4byte) = 8byte 할당
    int* a = new int;
    *a = 10;  // *a = 10; (포인트 연산자이므로 int* a, int *a와 다름)
    cout << "동적 할당한 int 변수에 값은 " << *a << endl;
    cout << "sizeof(a) : " << sizeof(a) << endl;  // byte 출력
    cout << "sizeof(*a) : " << sizeof(*a) << endl;
    delete a;  // 힙 메모리 꼭 삭제, 안그러면 메모리 누수

        // double* b는 포인터 생성 (4byte) + new double은 실수형 힙 메모리(8byte)= 12byte
    double* b = new double;
    *b = 10.12;
    cout << "동적 할당한 double 변수에 값은 " << *b << endl;
    cout << "sizeof(b) : " << sizeof(b) << endl;  // byte 출력
    cout << "sizeof(*b) : " << sizeof(*b) << endl;
    delete b;

    cout << endl;

    // 
    Bullet* bullet = new Bullet;
    bullet->speed = 10.2;  // (*bullet).speed 쓰기 불편하여 bullet->speed 사용
    bullet->damage = 20;
    cout << "동적 할당한 bullet의 speed 변수에 값은 " << bullet->speed << endl;
    cout << "동적 할당한 bullet의 damage 변수에 값은 " << bullet->damage << endl;
    cout << "sizeof(bullet) : " << sizeof(bullet) << endl;
    cout << "sizeof(*bullet) : " << sizeof(*bullet) << endl;
    delete bullet;

    int size = 4;
    int* arr = new int[size];  // 포인터 arr 주소로 int[4](16byte)의 시작 주소에 접근
    int arr2[5];  // 수정할 수 없는 상수(5)로 이루어진 정적배열, arr2[5]은 *(arr2+5)와 같음

    cout << "sizeof(arr) : " << sizeof(arr) << endl;
    cout << "sizeof(arr2) : " << sizeof(arr2) << endl;

    for (int i = 0; i < size; i++){
        arr[i] = i;
    }

    for (int i = 0; i < size; i++){
        cout << "arr[" << i << "] : " << arr[i] << endl;
    }

    /*
    for (int i = 0; i < size; i++){
        delete &arr[i];
    }
    */
    delete[] arr;  // delete[] 해제할 것은 arr[] 배열, aar2[] 배열은 정적이므로 알아서 해제

    size = 3;
    Bullet* bullets = new Bullet[size];
    Bullet bullets2[3];

    cout << "sizeof(bullets) : " << sizeof(arr) << endl;
    cout << "sizeof(bullets2) : " << sizeof(arr2) << endl;

    for (int i = 0; i < size; i++){
        bullets[i].speed = (i + 1) * 10;  // bullets[i].speed와 *(bullets+i)->speed은 같다.
        bullets[i].damage = (i + 1) * 100;
    }

    for (int i = 0; i < size; i++){
        cout << "bullets[" << i << "].speed " << bullets[i].speed << endl;
        cout << "bullets[" << i << "].damage " << bullets[i].damage << endl;
    }
    delete[] bullets;
}

#include <iostream> // cin, cout
#include <string> // string
#include <stdio.h> // rand()
#include <time.h> // time()
#include <Windows.h> // Sleep()

using namespace std;

class Bullet2{
public:
    float speed;
    int damage;

    void SetBullet(int i) {
        speed = (i + 1) * 10;
        damage = (i + 1) * 100;
    }

    void PrintBulletInfo(int i) {
        cout << i << "번째 bullet speed " << speed << endl;
        cout << i << "번째 bullet damage " << damage << endl;
    }
};

void main() {
    int size = 3;
    Bullet2* bullets = new Bullet2[size];
    Bullet2 bullets2[3];

    cout << "sizeof(bulelts) : " << sizeof(bullets) << endl;
    cout << "sizeof(bulelts2) : " << sizeof(bullets2) << endl;

    for (int i = 0; i < size; i++){
        bullets[i].SetBullet(i);
    }
    
    for (int i = 0; i < size; i++){
        bullets[i].PrintBulletInfo(i);
    }
    delete[] bullets;
}
285개(7/15페이지)
프로그래밍
번호 제목 글쓴이 조회 날짜
165 [C#] 네임스페이스 (namespace) 푸딩뱃살 3406 2015.11.14 17:34
164 [C#] 인터페이스 (Interface) 첨부파일 푸딩뱃살 2197 2015.11.13 18:17
163 [C/C++] Static(정적) 멤버 변수, 메소드 푸딩뱃살 2475 2015.11.13 10:32
162 [C/C++] 추상 클래스 - 오버라이드 / 업/다운캐스팅(형변환) / virtual(가상함수) 푸딩뱃살 4727 2015.11.12 02:00
161 [C/C++] 메소드 오버라이드 푸딩뱃살 1994 2015.11.12 01:51
160 [C#] 상속 (with Unity) 푸딩뱃살 5455 2015.11.10 16:25
159 [C/C++] 클래스 상속 푸딩뱃살 2247 2015.11.10 14:08
158 [C/C++] 객체 활용 푸딩뱃살 2472 2015.11.09 21:28
157 [C#] C# 객체 클래스 푸딩뱃살 3569 2015.11.08 15:51
156 [C/C++] 생성자 / 소멸자 / 오버로드 푸딩뱃살 2205 2015.11.07 01:23
155 [C/C++] 클래스 선언/정의, 객체 생성 푸딩뱃살 3569 2015.11.06 14:05
154 [C/C++] 로또 프로그램 푸딩뱃살 2286 2015.11.06 12:00
153 [C/C++] 2차원 동적 객체 배열 활용 푸딩뱃살 3652 2015.11.06 00:47
152 [C/C++] 2차원 배열 푸딩뱃살 2404 2015.11.06 00:30
151 [C/C++] 2차원 포인터 푸딩뱃살 2053 2015.11.06 00:26
>> [C/C++] 동적할당 푸딩뱃살 1807 2015.11.05 11:23
149 [C/C++] 당신의 프로그래밍에 디버깅 더하기 : Visual C++ 디버깅 기초에서 고급까지 첨부파일 푸딩뱃살 1454 2015.11.05 11:20
148 [C/C++] 포인터와 배열의 이해 푸딩뱃살 2016 2015.11.04 23:54
147 [C/C++] 포인터 푸딩뱃살 2195 2015.11.04 15:14
146 [C/C++] 일반 함수와 메소드간의 차이 푸딩뱃살 2433 2015.11.03 23:38