-
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
Code:
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
-
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.
-
Thx but look at these two examples in couple of books i read
the programm is about clipboard;
Code:
,,,,,,,,,,,,,,,,,,,,,
static PTSTR ptext;
HGLOBAL hglobal // global handle for clipbord
PTSTR pglobat;
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
openclipboard(hwnd);
pglobal = globallock(hglobal);
if(ptext)
{
free(ptext);
ptext = NULL;
}
ptext = malloc(globalsize(hglobal));
strcpy(ptext, pglobal);
OR
This programm to get path of dir in list box just a few line
Code:
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?
-
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.
ie:
Code:
char *ptr1,ptr2;
ptr1=(char *)malloc(256);
ptr2=new char[256];
free(ptr1);
delete [] ptr2;
EDIT: formatting