Thread: LPSTR Size

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    11

    LPSTR Size

    Morning.

    LPSTR is just a *char isn't it...

    If this is the case, does a string variable (i.e. lpszString) created with LPSTR have the same restrictions on size as a single char?

    i.e., if I stored an integer value in lpszString, would it only go up to 255?

    It's my first venture into C++ with a complex app (company policy - deep end!) that needs some fixes.

    Cheers

    DS


  2. #2
    Registered User The Junglist's Avatar
    Join Date
    Nov 2002
    Posts
    42
    An LPSTR is just a char* thus it's only a pointer, so it's size is only sizeof(char*) bytes.

    >> i.e., if I stored an integer value in lpszString, would it only go up to 255?

    I'm unsure of why you'd want to store an integer in a LPSTR.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: LPSTR Size

    Originally posted by Dirty Sanchez
    Morning.

    LPSTR is just a *char isn't it...

    If this is the case, does a string variable (i.e. lpszString) created with LPSTR have the same restrictions on size as a single char?

    i.e., if I stored an integer value in lpszString, would it only go up to 255?

    It's my first venture into C++ with a complex app (company policy - deep end!) that needs some fixes.

    Cheers

    DS

    I believe it is.
    For your second question, that depends on how you store it. char* is just a pointer to a char, so you can't store anything in it unless you point it to something or allocate memory for it.

    You could store an integer value in it as an address (on the low level, everything is 1:s and 0:s so it doesn't matter which datatype it is).

    A pointer is 4 bytes large, which is the same size as an integer, so you won't lose any data in the process.

    I believe LISP uses this way to store small numbers directly in the pointer in their CONS cells (linked list nodes) to save memory, instead of letting the pointer point to some extra memory.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    11
    The reason for using LPSTR with integer values is that the code I am working on uses TextOut to write out the values.

    There is also a function to insert commas into the numeric values for thousands, millions etc.

    Magos -

    xGlobalAlloc is used to allocate memory for the variable.

    I think I understand what you mean. This would be the reason for using LPSTR rather than just using char.

    This is the code I am working from:

    Code:
    /****************************************************************/
    /* FormatAndPrint - print vpData at nXpos, nYpos in hDCDisplay */
    /****************************************************************/
    void  FormatAndPrint(HDC   hDCDisplay,
                         void  _far *vpData, // Numeric value
                         int   nXpos,
                         int   nYpos,
                         BOOL  bNumbers)
    {
       LPSTR    lpszNewString = NULL;
       HANDLE   hNewString    = 0;
    
       if (hNewString = xGlobalAlloc(GMEM_MOVEABLE | GMEM_DISCARDABLE | GMEM_ZEROINIT, 255))
       {
          if (lpszNewString = GlobalLock(hNewString))
          {
             if (bNumbers)
             {
                InsertCommas(&lpszNewString, vpData);
                TextOut(hDCDisplay, nXpos, nYpos, lpszNewString, lstrlen(lpszNewString));
             }
             else
                TextOut(hDCDisplay, nXpos, nYpos, (LPSTR)vpData, lstrlen(vpData));
    
             GlobalUnlock(hNewString);
          }
          xGlobalFree(hNewString);
       }
    }
    
    /********************************************************************/
    /* InsertCommas - formats a numeric string (with commas) and passes */
    /*                it back via the FormatString variable             */
    /*                                                                  */
    /*        input - lppszFormatString - the string which will contain */
    /*                                    the modified number           */
    /*                plNumber - the number we wish to format           */
    /*                                                                  */
    /********************************************************************/
    void  InsertCommas(LPSTR _far *lppszFormatString, LONG   _far *plNumber)
    {
       LONG  lCopyNumber =  labs(*plNumber);
    
       LPSTR   lpszWorkString = NULL;
    
       HANDLE   hWorkString    = 0;
    
       if (hWorkString = xGlobalAlloc(GMEM_MOVEABLE | GMEM_DISCARDABLE, 255))
       {
          if (lpszWorkString = GlobalLock(hWorkString))
          {
             int   nCommas    = 0,
                   nLastComma = 0,
                   nStrLen    = 0,
                   nCommaCnt  = 0,
                   nCounter   = 0;
    
             /* copy it, flip it and get the stats */
             wsprintf(lpszWorkString, "%ld", lCopyNumber);
             _fstrrev(lpszWorkString);
             nStrLen = lstrlen(lpszWorkString);
    
             nCommaCnt = nStrLen / 3;
             nLastComma = nCommaCnt * 3;
    
             for (nCounter = 0; nCounter < nStrLen; nCounter++)
             {
                /* comma position ? */
                if ((nCounter / 3) * 3 == nCounter && nCounter != 0)
                {
                   *(*lppszFormatString + nCounter + nCommas) = ',';
                   nCommas++;
                }
    
                *(*lppszFormatString + nCounter + nCommas) = *(lpszWorkString + nCounter);
             }
    
             /*   adjust for negatives   */
             if (*plNumber < 0)
                lstrcat(*lppszFormatString, "-\0");
    
             _fstrrev(*lppszFormatString);
    
             GlobalUnlock(hWorkString);
          }
          xGlobalFree(hWorkString);
       }
       else
          lstrcpy(*lppszFormatString, "!Error!");
    }
    (FYI: These are the actual variable names used in this code - handy eh!)

    My concern with this is that we are losing some accuracy with basically frigging around with the data, and I was unsure as to whether the entire integer value would be passed into TextOut.

    Cheers (or sorry?)

    DS

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heapsort
    By xENGINEERx in forum C Programming
    Replies: 2
    Last Post: 03-30-2008, 07:17 PM
  2. Pointer Size Relative To Integers?
    By SMurf in forum C Programming
    Replies: 1
    Last Post: 04-18-2006, 06:58 AM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM