new 연산자로 메모리 할당 받을 때 size 가 0 이면 어떻게 되나요?

trymp의 이미지

C++ new 연산자로 메모리 할당을 받을때 size 가 '0' 인 경우

별다른 오류가 없을까요?

 char *ptr = new char[0]

일단 위와 같을 때 exception 없이 메모리 주소를 주긴 하는데요.

write 하지 않는다면 다른쪽에 side effect 나 다른 오류를 발생하지는 않겠죠?

노파심에 질문 드립니다.

shint의 이미지

https://msdn.microsoft.com/ko-KR/library/windows/apps/kewsb8ba(v=vs.120).aspx

[::] new [placement] new-type-name [new-initializer]

[::] new [placement] ( type-name ) [new-initializer]

실패할 경우 new는 0을 반환하거나 예외를 throw합니다. 자세한 내용은 new 및 delete 연산자를 참조하십시오. 사용자 지정 예외 처리 루틴을 작성하고 함수 이름을 인수로 사용하여 _set_new_handler 런타임 라이브러리 함수를 호출하면 이 기본 동작을 변경할 수 있습니다.
관리되는 힙에서 개체를 만드는 방법에 대한 자세한 내용은 gcnew를 참조하십시오.
new가 C++ 클래스 개체에 메모리를 할당하는 데 사용되면 메모리가 할당된 후 개체의 생성자가 호출됩니다.
new 연산자로 할당한 메모리를 할당 해제하려면 delete 연산자를 사용하십시오.

_msize()로 윈도우에서는 크기를 구할 수 있습니다.
리눅스에서는 구하지 못하네요. ㅇ_ㅇ;;

{
    char *ptr = new char[0];
    printf("%d\n", _msize(ptr));    //1
    printf("%d\n", sizeof(*ptr));   //1
    delete ptr;
}
 
{
    char *ptr = new char[1];
    printf("%d\n", _msize(ptr));    //1
    printf("%d\n", sizeof(*ptr));   //1
    delete [] ptr;
}
 
{
    char *ptr = new char[10];
    printf("%d\n", _msize(ptr));    //10  바이트 
    printf("%d\n", sizeof(*ptr));   //1  바이트 
    printf("%d\n", _msize(ptr));    //10 바이트 
    delete [] ptr;
}
 
{
    int *ptr = new int[10];
    printf("%d\n", _msize(ptr));    //40 바이트 
    printf("%d\n", sizeof(*ptr));   //4  바이트 
    printf("%d\n", _msize(ptr));    //40 바이트 
    delete [] ptr;
}
 
{
    char *ptr = (char*) malloc (10);
    printf("%d\n", _msize(ptr));    //10 바이트 
    printf("%d\n", sizeof(*ptr));   //1  바이트 
    printf("%d\n", _msize(ptr));    //10 바이트 
    free(ptr);
}
 
{
    int *ptr = (int*) malloc (10);
    printf("%d\n", _msize(ptr));    //10 바이트 
    printf("%d\n", sizeof(*ptr));   //4  바이트 
    printf("%d\n", _msize(ptr));    //10 바이트 
    free(ptr);
}

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.