Thread: when to allocate memory

  1. #1
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001

    Smile when to allocate memory

    Hi everybody, can any one help me with this i much appreciate it.

    I have at times this problem of not being sure if i have to allocate
    memory space for a certain var(usually strings or blocks of memory and at times had become a game of guessing) so i will put these in list below
    please if you can tell me in which cases i need to allocate memory either with mallock or calloc.and inwhich i dont need.

    case # 1 =Lets say we have a char string nonconstant that we dont know the lenght of it like a directory environmental block or a
    structure member

    case #2 = a fixed const char string like"hello everybody"

    now which of the variables listed below need to allocate memory to point and be able to hold the data for the cases above

    BYTE    *ptobyte
    PTSTR    *ptstr
    char     *pchar
    BUFFER   pbuffer
    and couple of more types that i forgot and u can tell if u know of.

    Now i need to know which one of the above variables need to allocate mem to hold the data for the 2 cases i mentioned earlier?

    there are cases that i forget to allocate mem and the program works fine may be the compiler is doing it for me.

    thank you in advance
    Last edited by SAMSAM; 01-22-2003 at 11:24 AM.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    When you declare a pointer, that is what you are doing. You are declaring a 32bit storage location which will be used to hold a value which will point to something else.

    So, your first three cases are allocating a 32 bit pointer. They are not allocating the space that the pointer points too.

    I don't know what BUFFER is declared to be, it is not a standard type, or included in my VC++ documentation, but assuming that is a simple type, then you are allocating the memory for a "BUFFER".

    Case 1 or 2 is immaterial really, the pointer variable is just a named place in memory that, at this time, points nowhere.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Thx but look at these two examples in couple of books i read

    the programm is about clipboard;

    static PTSTR      ptext;
    HGLOBAL  hglobal   // global handle for clipbord
    PTSTR       pglobat;
    pglobal = globallock(hglobal);
    ptext = NULL;
    ptext = malloc(globalsize(hglobal));
    strcpy(ptext, pglobal);

    This programm to get path of dir in list box just a few line
    char  * pVarBeg, * pVarEnd, * pVarName, *pVarBlock ;
    pVarBlock =GetEnvironmentStrings()  ;  // Get pointer to env block
         while (*pVarBlock)
     if(*pVarBlock != '='){     
          pVarBeg = pVarBlock ;      // Beginning of variable  name
         while (*pVarBlock++  != '=')            // Scan until '='
          pVarEnd = pVarBlock - 1 ;          // Points to '=' sign
         iLength = pVarEnd - pVarBeg ;     // Length of variable name
                        // Allocate memory for the variable name and terminating
                        // zero. Copy the variable name and append a zero.
                   pVarName = calloc (iLength + 1, sizeof (CHAR)) ;
                   CopyMemory (pVarName, pVarBeg, iLength * sizeof(char)) ;
                   pVarName[iLength] = '\0' ;
                        // Put the variable name in the list box and free memory.
                   SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) pVarName) ;
                   free (pVarName) ;
              while (*pVarBlock++ != '\0');           // Scan until terminating zero
         FreeEnvironmentStrings (pVarBlock) ;

    In both cases they allocated mem for those pointers

    can u please elaborate?

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    In the first example, memory is allocated for hglobal using GlobalAlloc() elsewhere in the program. GlobalLock() then locks the position of that memory block so that a physical memory address can be given for use in more simple situations. That address is stored in pglobal. Next, an adequate amount of memory is allocated locally and statically for ptext using malloc(), and the contents of hglobal are copied to ptext.
    So, to recap, in the first example, memory is allocated with GlobalAlloc() and malloc().

    In the second example, it would appear that GetEnvironmentStrings() allocates memory itself.

    A word of warning; you shouldn't be using malloc() anymore. The new operator has superseeded it.

    char *ptr1,ptr2;
    ptr1=(char *)malloc(256);
    ptr2=new char[256];
    delete [] ptr2;
    EDIT: formatting
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  2. Memory Leak Help
    By (TNT) in forum Windows Programming
    Replies: 3
    Last Post: 06-19-2006, 11:22 AM
  3. Allocate a big dynamic memory location
    By vnrabbit in forum C Programming
    Replies: 11
    Last Post: 10-09-2002, 08:02 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. Error when freeing memory
    By foniks munkee in forum C Programming
    Replies: 9
    Last Post: 03-01-2002, 08:28 PM