Results 1 to 20 of 20

Thread: dynamic array

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2000
    Posts
    34

    dynamic array

    Can somebody help me understand how to correctly create and manage a dynamic array? This is what I've got so far:

    Code:
    CItem* lpItems = NULL;
    unsigned int nItemCount = 0;
    
    // When I want to resize the array I would do this???
    if (lpItems)
      delete[] lpItems;
    
    nItemCount++;
    lpItems = new CItem[nItemCount]
    
    // And to cleanup the memory I would do this???
    delete[] lpItems;
    But, since I am new to C/C++, I am not sure if this is correct. Also, is this the correct way to declare a pointer to an array?

    Code:
    CItem* lpItems[];  // Seems like this would be an array of pointers and not a pointer to an array.
    CItem[]* lpItems; // Or maybe this???
    Any and all help is apprecitated. I can deal with pointers, and arrays don't bother me too much, but when you combine the two it gives me headaches. It makes me really appreciate how easy VB makes it look

    Code:
    ' All in two lines of code :)
    ReDim Preserve arrayName(newArraySize) As CItem
    
    Erase arrayName

  2. #2

    Thread Starter
    Member
    Join Date
    Oct 2000
    Posts
    34
    Well, looks like I had it right on my first guess. At least I assume it is correct since I am not getting any compiler or run-time errors. My only question is how do I preserve the contents of the array before resizing it?? I'm guessing I could do this:

    1. Create a temporary array that is the same size as the original.
    2. Loop through the original array and copy each item over to the temp array.
    3. Delete the original array.
    4. Loop through the temp array and copy each item back over to the original array.
    5. Delete the temp array.

    Does this sound right? Also, I know there's bound to be a standard library class to handle this type of situation as I'm sure it is used frequently by many programmers. What is the C/C++ equivalent to the VB Collection?? Or at least, is there a function that lets you resize an array without having to delete it first (and thus losing its contents)??? Thanks

  3. #3
    Zaei
    Guest
    If you want to create your own class, its faster for resizing to:
    1: Create a new array of whatever size you want.
    2: Copy all values from Old Array into the new array.
    3: Delete old array.
    4: Reset old array pointer to new array pointer.

    The STL vector is the same thing as what you want. #include <vector> (no ".h"). For example:
    Code:
    #include <vector>
    #include <iostream.h>
    void main()
    {
      std::vector<int> SomeVector;
      SomeVector.resize(50000000000); // i dunno  =)
      SomeVector.resize(0);
      SomeVector.push_back(10); // inserts a 10 as the last item
      cout << SomeVector[0] << endl; //a 10
      SomeVector.push_back(55);
      cout << SomeVector[1] << endl; // a 55;
      SomeVector.resize(10);
      for(int i = 2; i < SomeVector.size(); i++)
      {
        SomeVector[i] = i;
      }
      
      for(i = 0; i < SomeVector.size(); i++)
        cout << SomeVector[i];
      return;
    }
    This should work (except for the ...resize(50000...) line =).
    Z.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If you're using the STL you should use <iostream> not <iostream.h>

    Also, you can use the list classes which are more efficient if you need to add/remove items in the middle of the list (you only have to allocate/deallocate one item at a time rather than everything after that point).
    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

  5. #5
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    355
    are these really the ways ppl use to implement dynamic arrays?
    it just seems really stupid that there's no way to avoid copying the entire array to a new one!
    buzzwords are the language of fools

  6. #6
    amac
    Guest
    To Kenny:

    How else would you expect them to do it?


    To parksie:

    Is there any difference between iostream and iostream.h?

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Well there are memory reallocation functions. Not sure how you use them though.

    <iostream.h> is Microsoft's version of the headers, <iostream> is the standard version. The obvious difference youll notice is that with the standard headers you have to write std::cout instead of just cout unless you use the using directive to put yourself in the standard namespace - using namespace std;

    Better to use the standard header, then you know what's going on with the namespaces. I don't really know what the M$ headers do with the namespaces.
    Harry.

    "From one thing, know ten thousand things."

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by KENNNY
    are these really the ways ppl use to implement dynamic arrays?
    it just seems really stupid that there's no way to avoid copying the entire array to a new one!
    Well, VB does this

    Vectors and Lists are probably the two most useful parts of the STL (apart from string )
    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
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    I've come across maps before too, they seem useful.
    Harry.

    "From one thing, know ten thousand things."

  10. #10
    amac
    Guest
    > Well there are memory reallocation functions. Not sure how you > use them though.

    are you talking about malloc()?

    well the new and delete keywords are the C++ way of doing it

    > <iostream.h> is Microsoft's version of the headers <iostream> > is the standard version. The obvious difference youll notice is
    > that with the standard headers you have to write std::cout
    > instead of just cout unless you use the using directive to put
    > yourself in the standard namespace - using namespace std;

    Are you sure iostream.h is a M$ thing? I think iostream.h is just a c++ implementation... I am familiar with the concepts of namespaces.. and that is one of the differences between C and C++... C uses separate namespaces whereas C++ uses the same one for everything...

    just like when you create an struct in C.. whenever you use it you have to specify the namespace... but C++ you dont.

  11. #11
    amac
    Guest
    Originally posted by parksie
    Well, VB does this

    Vectors and Lists are probably the two most useful parts of the STL (apart from string )
    VB doesnt... if you are referring to the preserve keyword? well one thing about high-level languages is that it does a lot of processing you dont see... or need to worry about...

    how exactly would you resize allocated a section of memory?

    I am familiar with STL... again... prewritten classes that do a lot of stuff in the background that you dont seee... or worry about...

  12. #12
    amac
    Guest
    I made a mistake... iostream.h and iostream are exactly the same thing... iostream.h is not a M$ header.... just to prove my point...

    This is the first 10 or so lines of iostream.h...

    oxygen: cat iostream.h | head
    /* This is part of libio/iostream, providing -*- C++ -*- input/output.
    Copyright (C) 1993 Free Software Foundation

    This file is part of the GNU IO Library. This library is free
    software; you can redistribute it and/or modify it under the
    terms of the GNU General Public License as published by the
    Free Software Foundation; either version 2, or (at your option)
    any later version.

    seems to me that iostream.h on here... was created by GNU... and UNIX organization... i dont think they would have anything to do with M$

    Contents of iostream...

    oxygen: cat iostream
    // -*- C++ -*- forwarding header.
    // This file is part of the GNU ANSI C++ Library.

    #ifndef __IOSTREAM__
    #define __IOSTREAM__
    #include <iostream.h>
    #endif

    correct me if i am wrong but... iostream... seems to me to include iostream.h...





  13. #13
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    355
    VB uses the SafeArray APIs for arrays..
    I dunno how these work internally, do they do mass-copying or is there a cunning way?

    like..

    when it's redim preserved, use malloc or heapalloc to allocate some more mem at the end of the array?
    buzzwords are the language of fools

  14. #14
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    If you're using Visual C++ then <iostream.h> is the Microsoft version of the header. As far as I am aware, on any compiler <iostream.h> is probably specific to the compiler. Well unless the compiler was written before <iostream> became the standard.

    <iostream> has to be compatible with the ANSI standard version of C++. Just because the contents of <iostream.h> are released under the GPL doesn't mean they're nothing to do with Microsoft. You presume too much.

    Many of the standard headers actually just include the Microsoft headers in VC++, because the MS headers comply with the ANSI standards. The point is that the code is compatible with any compiler.

    <iostream> and <iostream.h> behave differently, in VC++6 at least. Where did you get that header code from? For instance, this code won't compile with <iostream> but will compile with <iostream.h>:
    Code:
    #include <iostream.h>  // change to <iostream> and it won't compile
    
    int main()
    {
        cout << "Hello world" << endl;
        return 0;
    }
    are you talking about malloc()?

    well the new and delete keywords are the C++ way of doing it
    No I wasn't talking about malloc(), I was talking about realloc(). new and delete are for allocating and deallocating memory, not reallocating it.

    In C++ by default things are in the global namespace, but you can place things in different namespaces and some headers place things in their own namespaces.
    Harry.

    "From one thing, know ten thousand things."

  15. #15
    amac
    Guest
    Originally posted by KENNNY
    VB uses the SafeArray APIs for arrays..
    If you were unaware but the Win32 API is written in C++, so even if VB uses the SafeArray API call... its still comes down to allocating a new set of memory... then transfering if need be to the new section of memory and deleting the old one....

    Originally posted by KENNNY
    when it's redim preserved, use malloc or heapalloc to allocate some more mem at the end of the array?
    it could use those functions, but its not as easy as just adding some more memory onto a block... malloc.. creates a block of memory of a certain size... so if an array is already occupying a section... then the new block cannot occupy that same section... therefore... when you use preserve... its goin to allocate a new memory block of the desired size... copy the contents of the old one into the new... and then free the old memory block...

  16. #16
    amac
    Guest
    You cant assume that everyone is using Visual C++ though...

    maybe the iostream.h is a microsoft thing... but iostream.h on non microsoft compiler environments wont be...

    do you know for sure that microsofts iostream is the standard??? I have heard too many lies come from microsoft to believe that right away...

    what about Microsofts implementation of Java... Visual J++... they got sued for that one... because it took one of Java's features and got rid of it... the portablility of it... well J++ will only run in windows environments...

    i will believe GNU before i ever believe Microsoft...


    realloc()...

    if you had an essay... and you had to re-write it... maybe you would have to re-write the hole thing...

    realloc() will use the same block of memory if the space is available at that position... but if there isnt then its goin to move... besides realloc() probably does copy the contents of the original block to the new... its function is the "resize" a block of allocated memory... either way you will need a temporary position to hold the original data...

  17. #17
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    355
    Originally posted by amac


    If you were unaware but the Win32 API is written in C++, so even if VB uses the SafeArray API call...
    course i know API is written in and for C/C++, but how is that relevant? I'm saying the VB runtime uses the SafeArray APIS.



    2. Are there any memory allocation functions where you control where the memory is allocated?
    buzzwords are the language of fools

  18. #18
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Well what do you think the SafeArray functions do internally then?

    And no, you can't say EXACTLY where the memory is allocated. In C++ you can use "placement new" to wrap a variable round a specific memory location. For example, if you have a hardware timer that has an 4-byte value at address 0x12345678 (DON'T DO THIS IT WON'T WORK!!!), you can do something like:
    Code:
    #include <new.h>      // Must #include this to use "placement new"
    
    void someCode() {
        void* place = (void*)0x12345678;
    
        long *val = new(place) int;
        // The pointers val and place will be equal
    }
    It's not recommended to do that, though.
    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

  19. #19
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    355
    I don't know how the SafeArray APIS work! I was just saying VB uses them..
    buzzwords are the language of fools

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

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