-
buffer[100] and *buffer
I just want to know what is the real difference between those two char declarations other than:
-buffer[100] is any array in which you can store maximum of around 100 characters.
-buffer[100] is declared so that it does not take more memory than *buffer because you can only put limited number of characters.
I may be wrong but what is the difference between those two "chars"?
-
char buffer[100] is an array of 100 characters, returns a pointer to the first element but allocates 100 bytes. buffer[x] will return the x+1'th char.
char* buffer is a pointer to a char, not an array. buffer[x] will return the x'th dereferenced offset from buffer while buffer[0] will return the char at buffer.
-
The only real difference is that for char[100] memory is allocated but not or char* (just the four bytes to hold the pointer)
-
So is there any way to allocate the memory for some char declared as "*buffer"?
-
a char isn't declared as char* buffer, but a pointer.
char* is used as pointer to dynamical arrays, which are allocated with new char[x] where x can be variable, that is evaluated at runtime. To deallocate dynamical arrays you need to use delete[] on the pointer.
-
yeah
malloc or new
wait - did you say char**?
Do you want a) a pointer to a C-string, b) an array of C-strings or c) a two-dimensional array of char?
Code:
int i; // loop
// case a
char ** ppCa = NULL;
ppCa = new char*;
*ppCa = new char[length of string];
delete[] *ppCa; // maybe delete *ppCa[];
delete ppCa;
// case b
char** ppCb = NULL;
ppCa = new char*[number of strings];
for(i=0;i<numstrs;i++)
{
ppCa[i] = new char[length of strings];
}
// case c
// like b
new char*[4] equals to
(char**)malloc(4 * sizeof(char*));
-
For Win32, you could use the LocalAlloc or GlobalAlloc functions.
-
For maximum flexibility, I normally use HeapAlloc:
Code:
#define xmalloc(sz) HeapAlloc(GetProcessHeap(), 0, sz)
#define xfree(ptr) HeapFree(GetProcessHeap(), 0, ptr)
#define xrealloc(ptr, sz) HeapReAlloc(GetProcessHeap(), 0, ptr, sz)
Those are usually slower than calls to malloc, but if you're keeping your program size down and/or you want to make your own allocator, they're pretty good.
For blocks of memory larger than 4mb, use the VirtualAlloc functions.
PS: GlobalAlloc and LocalAlloc are deprecated, I think.
-
So its mean, it is good idea to declare it as an array if you want to use limited size of memory?
-
Well...technically yes, but it shouldn't do. When you declare an array:...that takes up 100 bytes of memory. When you allocate it:
Code:
char *pcBuf = new char[100];
...it normally takes up more than 100, because of dynamic allocation overhead (for example, storage of the data the compiler needs to know how much to free when you call delete[]).
-
Local/GlobalAlloc is for Win16 compability. You can also use VirtualAlloc if access speed is not an issue, but you need much space. This is a little complicatd though. (explained in "Windows Programming for Experts" by Jeffrey Richter, Microsoft Press, besides other books and web sites and msdn)