Results 1 to 12 of 12

Thread: dynamic array in cpp

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2010
    Posts
    408

    dynamic array in cpp

    hello,

    my problem is that i have a code in matlab and i want to rewrite it in cpp

    in the matlab code i have dynamic arrays
    with that i mean that too many times rows or columns of the table are deleted or a new one is inserted



    but in cpp we know that the dimensions of an array are constant

    what do you suggest me to use??
    vector or lists or do it with array??
    Last edited by vagelis; Dec 4th, 2010 at 03:14 PM.

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: dynamic array in cpp

    I think, you could use Linked List

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: dynamic array in cpp

    An array is simply a block of memory. The dimensions of an array is directly related to the size of this block. If you want to resize your array, you need to allocate a larger memory block.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: dynamic array in cpp

    Quote Originally Posted by Atheist View Post
    An array is simply a block of memory. The dimensions of an array is directly related to the size of this block. If you want to resize your array, you need to allocate a larger memory block.
    But in VB6, we could dynamically increment the array size by using the ReDim keyword...! So, how does VB dynamically allocate memory in this manner ?

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: dynamic array in cpp

    Quote Originally Posted by akhileshbc View Post
    But in VB6, we could dynamically increment the array size by using the ReDim keyword...! So, how does VB dynamically allocate memory in this manner ?
    VB6 will reallocate the memory block with a new size. If the Preserve keyword is used, the contents from the original memory block will be copied to the new memory block.

    Code:
    'VB
    Dim i(10) As Integer
    'C
    int *i = (int*)malloc(10 * sizeof(int));
    
    'VB
    ReDim i(20)
    'C
    free(i);
    i = (int*)malloc(20 * sizeof(int));
    
    'VB
    ReDim Preserve i(40)
    'C
    i = (int*)realloc(i, 40 * sizeof(int))
    (Yes, I am aware that the VB array declarations will be 1 element larger than the C ones).
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Re: dynamic array in cpp

    Quote Originally Posted by akhileshbc View Post
    But in VB6, we could dynamically increment the array size by using the ReDim keyword...! So, how does VB dynamically allocate memory in this manner ?
    how you do dynamic allocation in vb.net, does it use the same ReDim?

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: dynamic array in cpp

    Quote Originally Posted by Aash View Post
    how you do dynamic allocation in vb.net, does it use the same ReDim?
    While this really is the wrong forum section for this kind of question; yes, ReDim is used to redimension arrays in VB.NET. That said, you normally wouldn't use arrays if you know that the size will change, you would use a collection such as a List(Of T).
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Re: dynamic array in cpp

    Quote Originally Posted by Atheist View Post
    While this really is the wrong forum section for this kind of question; yes, ReDim is used to redimension arrays in VB.NET. That said, you normally wouldn't use arrays if you know that the size will change, you would use a collection such as a List(Of T).
    Thanks

  9. #9
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: dynamic array in cpp

    Thanks Atheist..

    But I'm having another doubt...

    When a array is being declared it allocates or reserves that space (consecutive memory locations)... But when we try to reallocate or extend the reserved space, what happens if there's no vacant space ?

    For example, reserved for array from memory(assumption) location 2000 to 2010.
    But from 2015 onwards, there's other data stored. So, if we try to enlarge the array space to 20 more, wouldn't it overlap ?

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  10. #10
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: dynamic array in cpp

    realloc() will extend the memory block if possible. If it isn't possible (That is, if the new memory block would overlap other reserved space) it will move the memory block to another address.
    If the allocation can not be made at all, meaning that there is no more space, NULL is returned.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  11. #11
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: dynamic array in cpp

    Quote Originally Posted by Atheist View Post
    realloc() will extend the memory block if possible. If it isn't possible (That is, if the new memory block would overlap other reserved space) it will move the memory block to another address.
    If the allocation can not be made at all, meaning that there is no more space, NULL is returned.
    Thanks.. Got it...

    Sorry for hijacking this thread...

    Quote Originally Posted by Aash View Post
    how you do dynamic allocation in vb.net, does it use the same ReDim?
    An example would be:
    vb.net Code:
    1. Dim myUsers as new list(of string) '~~~ you could list of integers, long, etc..
    2.  
    3. myusers.add("Atheist")
    4. myusers.add("Aash")
    5. myusers.add("ABC")
    6. 'etc..
    7.  
    8. if myusers.contains("Aash") then messagebox.show("Hey, you are there !")
    9.  
    10. messagebox.show("Total number of items = " & myusers.count)
    11.  
    12. '~~~ there are several other useful methods like toarray() which will convert the items to an array, binarysearch(), etc....
    PS: I have handwritten this code because my VS2010 is showing some "unhandled exception", when I try to open it..

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  12. #12
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Re: dynamic array in cpp

    Quote Originally Posted by akhileshbc View Post
    Thanks.. Got it...

    Sorry for hijacking this thread...


    An example would be:
    vb.net Code:
    1. Dim myUsers as new list(of string) '~~~ you could list of integers, long, etc..
    2.  
    3. myusers.add("Atheist")
    4. myusers.add("Aash")
    5. myusers.add("ABC")
    6. 'etc..
    7.  
    8. if myusers.contains("Aash") then messagebox.show("Hey, you are there !")
    9.  
    10. messagebox.show("Total number of items = " & myusers.count)
    11.  
    12. '~~~ there are several other useful methods like toarray() which will convert the items to an array, binarysearch(), etc....
    PS: I have handwritten this code because my VS2010 is showing some "unhandled exception", when I try to open it..
    Thanks Akhilesh

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