|
-
Dec 4th, 2010, 02:10 PM
#1
Thread Starter
Hyperactive Member
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.
-
Dec 5th, 2010, 12:15 AM
#2
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,...
-
Dec 7th, 2010, 10:46 AM
#3
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.
-
Dec 17th, 2010, 11:04 AM
#4
Re: dynamic array in cpp
 Originally Posted by Atheist
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,...
-
Dec 17th, 2010, 11:31 AM
#5
Re: dynamic array in cpp
 Originally Posted by akhileshbc
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).
-
Dec 17th, 2010, 11:35 AM
#6
Hyperactive Member
Re: dynamic array in cpp
 Originally Posted by akhileshbc
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?
-
Dec 17th, 2010, 11:37 AM
#7
Re: dynamic array in cpp
 Originally Posted by Aash
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).
-
Dec 17th, 2010, 11:46 AM
#8
Hyperactive Member
Re: dynamic array in cpp
 Originally Posted by Atheist
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
-
Dec 17th, 2010, 11:58 AM
#9
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,...
-
Dec 17th, 2010, 12:31 PM
#10
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.
-
Dec 17th, 2010, 09:28 PM
#11
Re: dynamic array in cpp
 Originally Posted by Atheist
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...
 Originally Posted by Aash
how you do dynamic allocation in vb.net, does it use the same ReDim?
An example would be:
vb.net Code:
Dim myUsers as new list(of string) '~~~ you could list of integers, long, etc..
myusers.add("Atheist")
myusers.add("Aash")
myusers.add("ABC")
'etc..
if myusers.contains("Aash") then messagebox.show("Hey, you are there !")
messagebox.show("Total number of items = " & myusers.count)
'~~~ 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,...
-
Dec 18th, 2010, 11:39 AM
#12
Hyperactive Member
Re: dynamic array in cpp
 Originally Posted by akhileshbc
Thanks..  Got it...
Sorry for hijacking this thread...
An example would be:
vb.net Code:
Dim myUsers as new list(of string) '~~~ you could list of integers, long, etc..
myusers.add("Atheist")
myusers.add("Aash")
myusers.add("ABC")
'etc..
if myusers.contains("Aash") then messagebox.show("Hey, you are there !")
messagebox.show("Total number of items = " & myusers.count)
'~~~ 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|