Results 1 to 10 of 10

Thread: Dymanic Array

  1. #1

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Dymanic Array

    hei buddies, have more than a month did login this forum . "coz juz busy with a project and now I juz stuck in somewhere else and need your expert to get me out from the dark

    what is the good what to write a dynamic array in C++/VC++? I have to store the following item in the array:

    PHP Code:
    typedef struct tagLANDMARK
    {
        
    TCHAR    Place[33];
        
    TCHAR    type[33];
        
    long       Ptr;
        
    RECT      rt;
    LANDMARK
    I use UNICODE here is because the app is running on WinCE and it should not have any significant in coding

    regards,
    Chris.C
    Last edited by Chris; Oct 25th, 2001 at 07:51 PM.

  2. #2
    Megatron
    Guest
    I think the easiest way to create a dymanic array would be to use the ready-made vector class. Otherwise you'd have to create your own linked-list or binary-tree class.
    Code:
    vector<int> myVar;
    // Add objects to the vector
    myVar.push_back(43);
    myVar.push_back(466);
    myVar.resize(10);
    myVar[8] = 43;

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    An array provides instant random access unlike linked lists and trees

    A dynamical array is allocated on the heap, and access is provided with a pointer.

    allocation:

    type* array = new type[elements];

    random access:

    array[element];

    destruction:

    delete[] array;
    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.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Megatron: if he is codeing for WinCE, using vector might not be a good idea...
    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.

  5. #5
    Megatron
    Guest
    kedaman: While that is a dymanic array, it cannot be resized without losing all the data that was originally in it. (unless you write your own function to loop through the elements and add them to the new array)

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    well thats how you do it usually

    template <class T>
    T* resize(T*& x,int size){T* temp=(T*)memcpy(new T[size],x,_msize(x));delete[]x;return x=temp;}
    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.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    kedaman, you sure know how to make code totally illegible. But it is cool nevertheless - I never thought there's such a cool way to take advantage of the fact that memcpy returns the address of the buffer.
    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.

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    readability vs elegance

    I prefer elegance, because it's cool, and in some cases like this, also profitable in performance
    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.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    yeah, but splitting up the lines wouldn't hurt performance without decreasing performance. But personally I like code that I can read but not newbies
    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.

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

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