|
-
Sep 25th, 2001, 07:01 PM
#1
Thread Starter
PowerPoster
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"?
-
Sep 26th, 2001, 03:54 AM
#2
transcendental analytic
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.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Sep 26th, 2001, 07:26 AM
#3
The only real difference is that for char[100] memory is allocated but not or char* (just the four bytes to hold the pointer)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 26th, 2001, 09:26 AM
#4
Thread Starter
PowerPoster
So is there any way to allocate the memory for some char declared as "*buffer"?
-
Sep 26th, 2001, 09:41 AM
#5
transcendental analytic
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.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Sep 26th, 2001, 09:47 AM
#6
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*));
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 26th, 2001, 11:25 AM
#7
For Win32, you could use the LocalAlloc or GlobalAlloc functions.
-
Sep 26th, 2001, 12:17 PM
#8
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 26th, 2001, 03:45 PM
#9
Thread Starter
PowerPoster
So its mean, it is good idea to declare it as an array if you want to use limited size of memory?
-
Sep 26th, 2001, 04:51 PM
#10
Monday Morning Lunatic
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[]).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 27th, 2001, 09:37 AM
#11
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)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|