포인터와 배열의 차이점 문제 같은데요...

archiroad의 이미지

현재 진행중인 프로젝트에서 최적화라고 해야 할까요..
사용하는 플랫폼이 ARM7을 core로 가지고 있다 보니, 메모리가 무척 부족합니다.
더군다나 os가 돌고 있고, 어플리케이션을 작성해야 하는 상황입니다.
사실 문제의 해결책은 찾았는데, 부족한 스택 영역에 조금이라도 부담을 덜 주고 싶어 질문 드립니다.

/*!
  @brief Show a prompt
 
  @param prompt       The characters of the font, as an array of UCS-2 characters.
  @param prompt_len   The number of characters in the prompt.
  @param prompt_first The first column to use for the prompt.
  @param prompt_last  The last column to use for the prompt (inclusive).
  @param left_to_right TRUE to align to first column, FALSE to align to last.
  @param scroll_type  How to scroll the prompt.
*/
DisplayResult DisplayShowPrompt (const ucs2char *prompt, uint8 prompt_len, uint8 prompt_first, uint8 prompt_last, bool left_to_right, DisplayScrollType scroll_type);

위 함수는 제가 던저주는 문자열을 받아서 화면에 뿌려 주는 역할을 합니다. 현 플랫폼에서는 96x16싸이즈의 OLED 입니다..
이 함수에 문자열을 아래와 같이 주었었습니다.
static const char string1[] = "Start";
DisplayShowPrompt((ucs2char*)string1, sizeof(string1)-1,.......);

ucs2char는 아스키 코드에 따라 미리 저장된 bdf 타입의 폰트에서 bmp값을 추출하기 위해 만들었습니다.
이런식으로 문자열을 출력하다 보니, 필요한 문장에 따라 많은 배열을 선언해야 할거 같아서(플래쉬 제한도 있습니다. 8Mbit), 간단한 함수를 만들기로 하였습니다.
void DisplayText(const char *s)
{  
    DisplayShowPrompt ((ucs2char*)s, sizeof(s)-1,.......................);  
}
아래와 같이 콜했습니다.
DisplayText("Start");

결과는 동작하지 않았습니다.
아래는 제가 해결한 방법입니다.
void DisplayText(const char *s)
{
    uint16 i,n;
    n = strlen(s);
    static char text[16];
    for(i=0;i<n;i++)
    {
        text[i]=*s++;
    }
    DisplayShowPrompt ((ucs2char*)text, sizeof(s)-1,.......................);  
}

위 방법은 문제는 해결 되었지만 배열 선언으로 인해 추가적으로 스택 영역에 메모리를 더 차지하더군요..
좋은 방법 있을까요?

gcc3.3 이용합니다.

kslee80의 이미지

배열에 대해서 sizeof() 는 그 배열의 크기를 되돌려 주지만,
포인터에 대해서 sizeof() 는 단지 그 포인터형의 크기를 되돌려줄 뿐입니다.

해결한 방법도 실제 첫번째 아규먼트로 넘어가는 문자열의 길이를 제대로 전달하지 않고 있습니다.
s 의 실제 길이와 관련없이 항상 15를 넘길 뿐이죠.

가장 정확한 해결법은 strlen() 과 같은 문자열의 실제 길이를 구하는 함수를 사용하여 문자열의 실제 길이를 구한 후,
그 값을 두번째 아규먼트로 넘겨주는 것이겠죠.

archiroad의 이미지

감사합니다
바로 해결 되었습니다.
왜 sizeof를 strlen으로 수정하지 않았을까요;;;
바로 윗줄에서는 n = strlen(s);로 길이를 재었는데 ㅡ.ㅠ
정말 감사합니다.

초보인생아키

익명사용자의 이미지

그런데 어떻게 구현하든 DisplayShowPrompt()를 호출한다면,
필요한 스택은 항상 DisplayShowPrompt()를 호출하는데 필요한 스택 크기에
해당 함수에서 필요한 스택 크기가 추가로 더 필요하게 되겠지요.

좋은 방법은 저렇게 파라미터를 많이 넘기지 않는 함수를 만드는 것이지요.
새로 만드는 함수내에서 원래있던 함수를 호출하면 더욱 오버헤드가 많아질 뿐입니다.
물론 사용하는 입장에서는 신경안써서 좋을 수는 있지만...
구현하신 방법이 원하는 답은 아닙니다. ^^

archiroad의 이미지

DisplayShowPrompt()는 라이브러리로 제공되는거라서 제가 수정할 수 없습니다.
제 설명이 짧았던거 같네요.

초보인생아키

댓글 달기

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