strings, arrays, pointers and dynamic memory allocation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • swarsa@msn.com

    #1

    strings, arrays, pointers and dynamic memory allocation

    Hi All,

    I realize this is not a Palm OS development forum, however, even though
    my question is about a Palm C program I'm writing, I believe the topics
    are relevant here. This is because I believe the problem centers
    around my handling of strings, arrays, pointers and dynamic memory
    allocation. Here is the problem I'm trying to solve:

    I want to fill a list box with a list of Project Names from a database
    (in Palm this is more similar to a file). The Palm OS function I must
    call in order to get the Project Names to show up in the list box is
    described below:

    *************** *************** *************** *************** ******
    LstSetListChoic es Function
    Purpose
    Set the items of a list to the array of text string pointers passed to
    this function. This functions erases the old list items.

    Declared In
    List.h

    Prototype
    void LstSetListChoic es (
    ListType *listP,
    char **itemsText,
    Int16 numItems
    )


    Parameters
    - listP
    Pointer to a list object.
    - itemsText
    Pointer to an array of text strings.
    - numItems
    Number of choices in the list.
    Returns nothing.
    *************** *************** *************** *************** ******

    So, what I want to do is create the array of strings from the database.
    Obviously, I don't know how many Project Names I will get from the
    database or what size they'll be - so everything needs to be handled
    dynamically.

    So I define a global variable in the file like this:

    static char **itemList;

    I also have a structure defined that represents the record of a project
    from the database as shown here:

    typedef struct {
    const char *projectName;
    const char *projectDesc;
    } ProjectDBRecord ;

    I also have a "packed" version of that structure defined that is how
    the actual record of a project is stored in the database as shown here:

    typedef struct {
    char rec[1];
    } PackedProjectDB Record;

    And I have a function that gets called when the project list needs to
    be filled as shown here:

    static void FillProjectList (ListType *lst)
    {
    UInt16 index;
    MemHandle h=0;
    UInt16 numRecs = DmNumRecordsInC ategory(db, dmAllCategories );
    char defaultVal[] = "New...";
    itemList = (char **) MemPtrNew((size of(char *) * numRecs) + 1);
    itemList[0] = (char *) MemPtrNew(sizeo f(defaultVal));
    StrCopy(itemLis t[0], defaultVal);

    for(index = 0; index < numRecs; index++)
    {
    // get the handle to the record and set busy bit
    h = DmQueryRecord(d b, index);
    if (h) { // could fail due to insufficient memory!
    PackedProjectDB Record *project = MemHandleLock(h );
    ProjectDBRecord rec;
    UnpackProject(& rec, project);
    printf("Here is the project name: %s", rec.projectName );
    itemList[index + 1] = (char *)
    MemPtrNew(StrLe n(rec.projectNa me));
    StrCopy(itemLis t[index + 1], rec.projectName );
    MemPtrUnlock(pr oject);
    }
    }

    LstSetListChoic es(lst, itemList, numRecs + 1);
    LstDrawList(lst );
    }

    As you can see, the 'itemList' is initialized in this method.

    When this function runs in POSE (Palm OS Emulator), it is telling me
    that my program is writing to "a memory location (0x0003D062), which is
    in the MemoryManager data structures..... Such an access usually means
    that an application allocated a buffer (possibly with MemPtrNew) that
    wasn't large enough for its purpose. When the application then tries to
    write data to the buffer, it writes off the end of the buffer,
    accessing the start of the buffer following it in memory." I see this
    error displayed once for each of the following lines of code execute
    when they execute:

    itemList[0] = (char *) MemPtrNew(sizeo f(defaultVal));
    StrCopy(itemLis t[0], defaultVal);

    Then finally the program dies with the following error message:

    "During a regular checkup, Palm OS Emulator determined that the dynamic
    heap chunk with the header address 0x0003D062 got corrupted. The size
    of the chunk (%chunk_size) was larger than the currently accepted
    maximum (%chunk_max)."

    I'm sure I must be doing something obviously wrong here. Can anyone
    give me a clue or point me in the right direction? If needed, I can
    post the whole program. I just didn't want to "muddy up" the issue
    with all the other event handing code. However, it might be helpful to
    see the methods that pack and unpack a project, so I have posted them
    here:

    static void PackProject(Pro jectDBRecord *project, MemHandle
    projectDBHandle )
    {
    UInt16 length = 0;
    char *s;
    UInt16 offset = 0;

    length = StrLen(project->projectName) +
    StrLen(project->projectDesc) + 2;// 2 for string terminators
    // resize the MemHandle and check for errors (0 means no error)
    if(MemHandleRes ize(projectDBHa ndle, length) == 0)
    {
    // copy the fields
    s = MemHandleLock(p rojectDBHandle) ;
    DmWrite(s, offset, (char *) project->projectName,
    StrLen(project->projectName) + 1);
    offset += StrLen(project->projectName) + 1;
    DmWrite(s, offset, (char *) project->projectDesc,
    StrLen(project->projectDesc) + 1);
    MemHandleUnlock (projectDBHandl e);
    }
    }

    static void UnpackProject(P rojectDBRecord *project, const
    PackedProjectDB Record *packedProject)
    {
    const char *s = packedProject->rec;
    project->projectName = s;
    s += StrLen(s) + 1;
    project->projectDesc = s;
    s += StrLen(s) + 1;
    }


    Thanks,
    Steve Warsa

  • Jens.Toerring@physik.fu-berlin.de

    #2
    Re: strings, arrays, pointers and dynamic memory allocation

    swarsa@msn.com wrote:[color=blue]
    > I realize this is not a Palm OS development forum, however, even though
    > my question is about a Palm C program I'm writing, I believe the topics
    > are relevant here. This is because I believe the problem centers
    > around my handling of strings, arrays, pointers and dynamic memory
    > allocation.[/color]

    There is at least one likely problem - but if this is really a problem
    I can't tell because all the functions you use are non-standard functions,
    so all people here can do is making guesses what they do and what kind of
    arguments they expect, going by what some standard functions do that look
    similar. But to get a reasonable answer you have to ask in a group that
    deals with the specifics of the Palm OS - it's really off-topic here.
    [color=blue]
    > static void FillProjectList (ListType *lst)
    > {
    > UInt16 index;
    > MemHandle h=0;
    > UInt16 numRecs = DmNumRecordsInC ategory(db, dmAllCategories );
    > char defaultVal[] = "New...";
    > itemList = (char **) MemPtrNew((size of(char *) * numRecs) + 1);
    > itemList[0] = (char *) MemPtrNew(sizeo f(defaultVal));
    > StrCopy(itemLis t[0], defaultVal);[/color]
    [color=blue]
    > for(index = 0; index < numRecs; index++)
    > {
    > // get the handle to the record and set busy bit
    > h = DmQueryRecord(d b, index);
    > if (h) { // could fail due to insufficient memory!
    > PackedProjectDB Record *project = MemHandleLock(h );
    > ProjectDBRecord rec;
    > UnpackProject(& rec, project);
    > printf("Here is the project name: %s", rec.projectName );
    > itemList[index + 1] = (char *)
    > MemPtrNew(StrLe n(rec.projectNa me));[/color]

    If this MemPtrNew function works similar to malloc() and StrLen() is
    like strlen() then you need to allocate one more char - strings in C
    have a '\0' character at the end and that's not counted by strlen()
    (please note that this is different from sizeof() when applied to a
    string literal, as you did above with the string "New...").
    [color=blue]
    > StrCopy(itemLis t[index + 1], rec.projectName );[/color]

    If it is the case that MemPtrNew() behaves like malloc(), StrLen()
    like strlen() and StrCpy() like strcpy() then it would explain why you
    get problems with memory corruption since you are always writing one
    char past the end of the memory you allocated. Another thing that could
    go wrong under these conditions is that MemPtrNew() returns a value that
    tells you that you run out of memory, you must check for the possibility.

    On the other hand, all this is pure guesswork and it would be much better
    to ask in a group concerned with the idiosyncracies of Palm OS.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
    \______________ ____________ http://www.toerring.de

    Comment

    • swarsa@msn.com

      #3
      Re: strings, arrays, pointers and dynamic memory allocation

      Hi Jens,

      Thanks for the reply. I have posted the documentation for the 3
      functions you mention below. The reason I asked in this forum is that
      it is much more active and the question seems to center more on "core"
      c rather than the specific use of the Palm OS API. The functions used
      are (from what I understand) roughly equivalent to the standard C
      functions of the same name. Anyway, I think you are probably correct
      regarding not adding enough space for the null terminator on the
      string.

      Thanks again,
      Steve Warsa


      *************** *************** *************** *************** *************** *************** **
      StrLen Function ^TOP^
      Purpose
      Compute the length of a string.

      Declared In
      StringMgr.h
      Prototype
      UInt16 StrLen (
      const Char *src
      )

      Parameters
      → src
      Pointer to a string.
      Returns
      Returns the length of the string in bytes.

      Comments
      Use this function instead of the standard strlen routine.

      This function returns the length of the string in bytes. On systems
      that support multi-byte characters, the number returned does not always
      equal the number of characters.

      StrCopy Function ^TOP^
      Purpose
      Copy one string to another.

      Declared In
      StringMgr.h
      Prototype
      Char *StrCopy (
      Char *dst,
      const Char *src
      )

      Parameters
      → dst
      Pointer to the destination string.
      → src
      Pointer to the source string.
      Returns
      Returns a pointer to the destination string.

      Comments
      Use this function instead of the standard strcpy routine.

      This function does not work properly with overlapping strings.
      MemPtrNew Function ^TOP^
      Purpose
      Allocate a new memory chunk from the dynamic heap.

      Declared In
      MemoryMgr.h
      Prototype
      MemPtr MemPtrNew (
      uint32_t size
      )

      Parameters
      → size
      The desired size of the chunk.
      Returns
      Returns a pointer to a newly allocated chunk if successful, or NULL if
      the Memory Manager was unable to allocate a memory chunk of the
      requested size.

      Comments
      This function allocates a non-movable chunk in the dynamic heap and
      returns a pointer to that chunk. Applications can use this call to
      allocate dynamic memory. User processes should use this call as a
      primary dynamic memory allocator.
      *************** *************** *************** *************** *************** *************** *

      Comment

      • swarsa@msn.com

        #4
        Re: strings, arrays, pointers and dynamic memory allocation

        Sorry, but I have made changes to the FillProjectList function and I
        still receive the error. Here is the modified source for it below:

        static void FillProjectList (ListType *lst)
        {
        UInt16 index;
        MemHandle h=0;
        UInt16 numRecs = DmNumRecordsInC ategory(db, dmAllCategories );
        char defaultVal[] = "New...";
        itemList = (char **) MemPtrNew((size of(char *) * numRecs) + 1);
        itemList[0] = (char *) MemPtrNew(StrLe n(defaultVal) + 1);
        StrCopy(itemLis t[0], defaultVal);

        for(index = 0; index < numRecs; index++)
        {
        // get the handle to the record and set busy bit
        h = DmQueryRecord(d b, index);
        if (h) { // could fail due to insufficient memory!
        PackedProjectDB Record *project = MemHandleLock(h );
        ProjectDBRecord rec;
        UnpackProject(& rec, project);

        // do something with the record's contents here
        itemList[index + 1] = (char *)
        MemPtrNew(StrLe n(rec.projectNa me) + 1);
        StrCopy(itemLis t[index + 1], rec.projectName );
        MemPtrUnlock(pr oject);
        }
        }

        LstSetListChoic es(lst, itemList, numRecs + 1);
        LstDrawList(lst );
        }

        Thanks,
        Steve Warsa

        Comment

        • John L

          #5
          Re: strings, arrays, pointers and dynamic memory allocation


          <swarsa@msn.com > wrote in message news:1103897815 .983949.32360@z 14g2000cwz.goog legroups.com...[color=blue]
          > Sorry, but I have made changes to the FillProjectList function and I
          > still receive the error. Here is the modified source for it below:
          >
          > static void FillProjectList (ListType *lst)
          > {
          > UInt16 index;
          > MemHandle h=0;
          > UInt16 numRecs = DmNumRecordsInC ategory(db, dmAllCategories );
          > char defaultVal[] = "New...";
          > itemList = (char **) MemPtrNew((size of(char *) * numRecs) + 1);[/color]

          Are you adding one in the wrong place?
          Should that be: ... * (numRecs + 1)); ?

          --
          John.


          Comment

          • Jens.Toerring@physik.fu-berlin.de

            #6
            Re: strings, arrays, pointers and dynamic memory allocation

            swarsa@msn.com wrote:[color=blue]
            > Sorry, but I have made changes to the FillProjectList function and I
            > still receive the error. Here is the modified source for it below:[/color]

            Well, there's another rather suspicious looking thing. Let's start with
            your typedef (all your code indented to make it more readable)
            [color=blue]
            > typedef struct {
            > char rec[ 1 ];
            > } PackedProjectDB Record;[/color]

            Arrrays with a single element tend to point to trouble... And what do
            we find:
            [color=blue]
            > static void UnpackProject( ProjectDBRecord *project,
            > const PackedProjectDB Record *packedProject )
            > {
            > const char *s = packedProject->rec;
            > project->projectName = s;
            > s += StrLen( s ) + 1;
            > project->projectDesc = s;
            > s += StrLen( s ) + 1;
            > }[/color]

            You're using memory that can't be fitting into that type of structure -
            the only string you can legally store in a structure of your type
            PackedProjectDB Record is a single, empty string. I don't know if that
            has anything to do with your troubles, but it would make sense to think
            again what you're doing here - it's not legal C.

            On the other hand the lines you specifically pointed out
            [color=blue]
            > char defaultVal[ ] = "New...";
            > itemList = ( char ** ) MemPtrNew( ( sizeof( char * ) * numRecs ) + 1 );
            > itemList[ 0 ] = (char *) MemPtrNew( StrLen( defaultVal ) + 1 );
            > StrCopy( itemList[ 0 ], defaultVal );[/color]

            look reasonable to me, at least under the assumption that memPtrNew()
            is equivalent to malloc() and StrCpy() is like strcpy() (but you still
            have to check if MemPtrNew() does not return a value that tells you
            that you didn't got memory!). In the special case that the first
            element of 'itemList' is a string literal you could even use:
            [color=blue]
            > char defaultVal[ ] = "New...";
            > itemList = ( char ** ) MemPtrNew( ( sizeof( char * ) * numRecs ) + 1 );
            > itemList[ 0 ] = defaultVal;[/color]

            or just
            [color=blue]
            > itemList = ( char ** ) MemPtrNew( ( sizeof( char * ) * numRecs ) + 1 );
            > itemList[ 0 ] = "New...";[/color]

            thus avoiding one allocation.
            Regards, Jens
            --
            \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
            \______________ ____________ http://www.toerring.de

            Comment

            Working...