Results 1 to 13 of 13

Thread: redim arrays?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Location
    Ottawa
    Posts
    155
    how do you redim an array in c++?

    how do you change its size?

    i mean, if you have:

    int names[10];

    can you change the '10' later on in your code to whatever you want?

    please answer

  2. #2
    Lively Member
    Join Date
    Sep 2000
    Location
    Singapore
    Posts
    78
    You can't redim an array like in VB. If you wish to dynamically allocate a array, you will need to use pointers and the malloc() function. Not too sure if malloc() is still used in C++ though.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Location
    Ottawa
    Posts
    155

    well...

    well...

    so there is FOR sure no way around this at all?

    also...
    why can't you do this

    cin>>howmany;

    int names[howmany];

    that is basically what i need. i need the user to specify instead of me doing that.

    any way around this at all?

  4. #4
    Lively Member
    Join Date
    Sep 2000
    Location
    Singapore
    Posts
    78
    No, you can't do allocation of arrays in any form without using pointers.

    Before starting, do you have knowledge of pointers in the first place? Or else you will have sometime understanding the codes.

  5. #5
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Try this:

    Code:
    int howmany;
    cin>>howmany; 
    
    int *names= new int[howmany]; 
    //then
    names[0] = 23423;
    names[1] = ..;
    ..
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  6. #6
    Lively Member
    Join Date
    Sep 2000
    Location
    Singapore
    Posts
    78
    Do remember to use the "delete" keyword to remove the array from the heap after finish using it


    delete [] names;


    If I'm not wrong with the syntax......

  7. #7
    Lively Member rekcus's Avatar
    Join Date
    Jan 1999
    Location
    Kuala Lumpur
    Posts
    122

    Talking

    Hey guys, ever thought of using a linked-list? Do you think it is possible?
    penyou!

    "The code bytes.."

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Linked lists are useful, but if you're only occasionally reallocating then their overhead is too high. However, if inserting into the middle of the list is frequently needed, then they're great. Check out the STL list template, or the slist template if you only need forward traversal of the list.
    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
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You don't usually need to do that, since it handles reallocation internally:
    Code:
    vector<int> viMyArray;
    
    viMyArray.push_back(5);
    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

  10. #10
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Maybe some on could spend the time and make a dll, header file, or class to do redim, ubound, etc easily.
    <All eyes turn to look at parksie>
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    *menacing*
    You talkin' ta me?
    *growls*

    UBound is nasty to do in C++ since you don't know which dimension you're using. You need to divide the dimension size by the size of it's parent (I think...this may be the wrong way around!)

    LBound is easy:
    Code:
    inline long LBound(void *pArray) { return 0; }

    Hehehehehe

    ReDim, well, normally you'd just delete and reallocate. Preserve is a little more complicated, but dead simple. Unfortunately they're pretty much specific to the situation...templates anyone?

    *looks at Technocrat*

    Double Dare time
    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

  12. #12
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    ooooooooohhhhhhhhhh, rumble in the C++ jungle.

    I would try it if I had time. In fact I might when I get done with my current project. It would be nice to have those types of abilities that were built into arrays in VB. Redim, ubound, lbound were so handy.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  13. #13
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Oh and dont make me get my pillow case of oranges!

    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


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