sysmem_ptrsize()


    Oct 27 2006 | 6:10 pm
    I'm struggling with some code, and found the following problem:
    "pchlen*sizeof(char)" returns a different number than "sysmem_ptrsize(pch)".
    Is this because pch is not a "t_ptr"? If so, am I writing bad code by using
    regular pointers with the sysmem_ptr functions? I'm confused.
    cheers, -thijs
    char * pch;
    char * tmp;
    char * mrk;
    long pchlen;
    long i;
    char *newline = "x0Dx0A"; // windows specific linebreak CR LF
    pch = strtok (*x->fdata, newline);
    pchlen = strlen(pch);
    post("strlen %ld", pchlen*sizeof(char));
    post("ptrsize %ld", sysmem_ptrsize(pch));
    if(mrk = sysmem_newptr(sysmem_ptrsize(pch)))
    {
    sysmem_copyptr(pch, mrk, sysmem_ptrsize(pch));
    }else{
    error("mem problem");
    }
    tmp = strtok(mrk, ","); // kick off the first element
    while(tmp = strtok(NULL, ","))
    {
    post ("column %s",tmp);
    }

    • Oct 27 2006 | 11:27 pm
      On Oct 27, 2006, at 11:10 AM, Thijs Koerselman wrote:
      > I'm struggling with some code, and found the following problem:
      > "pchlen*sizeof(char)" returns a different number than
      > "sysmem_ptrsize(pch)". Is this because pch is not a "t_ptr"? If so,
      > am I writing bad code by using regular pointers with the sysmem_ptr
      > functions? I'm confused.
      While you shouldn't use sysmem_ptrsize() with pointers that you
      haven't explicitly allocated yourself with sysmem_newptr(), there's
      no reason to assume that strlen(p) == sysmem_ptrsize(p) even if you
      have. Take for instance the example:
      char *p = sysmem_newptr(16);
      sprintf(p,"boo");
      In this case, strlen(p) should report 3, sysmem_ptrsize(p) should
      report 16.
      When copying strings you should minimally use strlen()+1 for the
      string copy to account for the null terminated C string. Search
      online for more information regarding memory and strings in C.
      -Joshua
    • Oct 28 2006 | 4:38 pm
      Hi Joshua, thanks for for the detailed explanation. I get it now.
      best, -thijs
      On 10/28/06, Joshua Kit Clayton wrote:
      >
      >
      > On Oct 27, 2006, at 11:10 AM, Thijs Koerselman wrote:
      >
      > > I'm struggling with some code, and found the following problem:
      > > "pchlen*sizeof(char)" returns a different number than
      > > "sysmem_ptrsize(pch)". Is this because pch is not a "t_ptr"? If so,
      > > am I writing bad code by using regular pointers with the sysmem_ptr
      > > functions? I'm confused.
      >
      > While you shouldn't use sysmem_ptrsize() with pointers that you
      > haven't explicitly allocated yourself with sysmem_newptr(), there's
      > no reason to assume that strlen(p) == sysmem_ptrsize(p) even if you
      > have. Take for instance the example:
      >
      > char *p = sysmem_newptr(16);
      >
      > sprintf(p,"boo");
      >
      > In this case, strlen(p) should report 3, sysmem_ptrsize(p) should
      > report 16.
      >
      > When copying strings you should minimally use strlen()+1 for the
      > string copy to account for the null terminated C string. Search
      > online for more information regarding memory and strings in C.
      >
      > -Joshua
      >
      >