Results 1 to 11 of 11

Thread: buffer[100] and *buffer

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    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"?
    Baaaaaaaaah

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  4. #4

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    So is there any way to allocate the memory for some char declared as "*buffer"?
    Baaaaaaaaah

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  7. #7
    Megatron
    Guest
    For Win32, you could use the LocalAlloc or GlobalAlloc functions.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  9. #9

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    So its mean, it is good idea to declare it as an array if you want to use limited size of memory?
    Baaaaaaaaah

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Well...technically yes, but it shouldn't do. When you declare an array:
    Code:
    char pcBuf[100];
    ...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

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width