malloc에 관해서 스택, 힙...

dulra82의 이미지

void main() {
char* testChar;
printf("%p", testChar);
// 여기서 어떤 주소값이 출력되겠죠? (testChar는 지역변수이기에 여기에 출력되는 주소값은 스택영역의 주소값인가요???
testChar = (char*)malloc(sizeof(char) * 256);
printf("%p", testChar);
/* 메모리 할당을 했기에 다른 주소값이 출력됩니다. (malloc을 하면 힙영역에 공간을 잡아 준다고 얘기를 합니다.
그러면 여기서 출력되는 주소값은 힙영역의 주소값인가요? 그러면 testChar 변수가 malloc 이전에는 스택영역에
있다가 malloc 이후에는 힙영역을 가리키는 건가요? */
free(testChar);
/* 여기서 free의 정확한 의미가 무엇인가요? testChar 주소에 다른 무엇인가가 바뀌지 않으면 다시 접근해서
문자열 출력이 가능합니다. 그래서 testChar = NULL; 을 넣어야한다 이것은 알겠는데 ...
그러면 free의 정확한 의미는 무엇인지... */
}

알다가도 조금씩 헤깔리네요ㅠ 아무생각없이 사용은 할 수 있겠는데 정확하게 모르니까 갑갑하네요

dalant019의 이미지

free되는 영역을 자유(free)롭게 풀어 주는 겁니다. 그럼 이 녀석은 자유롭게 그리운 부모 품(운영체제)으로 돌아가게 됩니다. 그럼 이 녀석은 다시 부모 슬하에서 안전하게 누군가의 다음 요청에 사용 될 수 있게 됩니다. 설명을 조금 쉽게하려고 부모니 자식이니 말을 썼는데 더 복잡하게 해드린 건 아닌지--;;

okdongil의 이미지

1. testChar는 지역변수이기에 여기에 출력되는 주소값은 스택영역의 주소값인가요???
-> 쓰레기 값입니다. 해당 변수의 stack영역의 주소값을 알려면, &testCahr를 찍으시면 됩니다.
그래서 pointer를 stack에서 선언하면 반드시 NULL로 초기화 하는게 rule입니다.

2. malloc의 return 값은 heap영역에 alloc한 address입니다.

3. 윗 분이 잘 설명해 주셔서...

댓글 달기

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