C, C++ 메모리 동적 할당 및 해제에 대해서 확실히 해두고 싶습니다 ~ 도와주세요 ~

cjynim의 이미지

//
// 정수형 메모리 동적 할당을 통한 2차원 배열 구현 ..
//
//
// 변수 선언
//
int** arr1, i, j, count=0;
//
// 메모리 할당
//
arr1 = (int**)malloc(sizeof(int*) * 10);
for(i=0; i<10; i++)
arr1[i] = (int*)malloc(sizeof(int) * 10);
//
// 2차원 배열 초기화 ..
//
for(i=0; i<10; i++)
for(j=0; j<10; j++)
arr1[i][j] = count++;
//
// 2차원 배열 출력 ..
//
for(i=0; i<10; i++)
for(j=0; j<10; j++)
printf("arr[%d][%d] = %d\n", i, j, arr1[i][j]);
//
// 메모리 해제 ..
//
for(i=0; i<10; i++)
free(arr1[i]);
free(arr1);
//
// new 연산자를 통한 정수 2차원 배열 동적 할당 ..
//
cout<<"\n\nnew 연산자를 통한 정수 2차원 배열 동적 할당 .."< //
// 변수 선언
//
int** arr2;
//
// 2차원 배열 동적 할당
//
arr2 = new int*[BUF];
for(int i=0; i arr2[i] = new int[BUF];
//
// 2차원 배열 초기화
//
count=0;
for(int i=0; i for(int j=0; j arr2[i][j] = count++;
//
// 2차원 배열 출력
//
for(int i=0; i for(int j=0; j cout<<"arr2["< //
// 메모리 해제
//
for(int i=0; i delete[] arr2[i];
delete[] arr2;

위의 두가지 소스는 제가 메모리 동적 할당을 공부하다가 만들어 본것인데요 ... 일단 메모리 동적 할당과

해제가 옳바르게 이루어 지고 있는지가 궁금합니다 ... 특히 아래 C++문법으로 만든 메모리 동적 할당

소스에서 마지막에 delete[]이러한 방법을 사용해서 해제를 했는데요 delete이렇게만 해도 해제는 되는

것 같습니다. 그런데 delete와 delete[]의 차이점을 알고 싶어서 Google에서 검색을 해보니 인용을 하자

면 "우선 delete 와 delete[]의 차이를 한마디로 말하라면 delete 는 단일 객체에 대한 소멸자의 호출과

메모리 환원을 하며 delete[] 는 배열객체에 대한 각각의 소멸자의 호출과 메모리 환원을 한다는 것입니

다."라고 되어 있거든요 그래서 delete[]이 방식으로 해제를 해 주었는데 제가 공부한 내용이 틀림이 없

는지 궁금합니다... 특히 C, C++같은 경우는 메모리 동적 할당하는 부분과 해제하는 부분에서 메모리 누수

현상을 통한 프로그램 오류가 잦다고 하니 여기서 확실히 해두고 싶네요 ~

그럼 오늘 하루도 공부 열심히 하세요 ~

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.