|
-
Oct 18th, 2001, 08:16 PM
#1
Thread Starter
Lively Member
VERY URGENT!!! Dynamic Arrays (Adding elements)
Ok, I have no idea how to do this, so I'm hoping someone here does. Would it be possible to add an element to an array after it has been declared and initialized with some number of elements? For example:
void main(){
int x[4];
x[0]=1;
x[1]=2;
x[2]=3;
x[3]=4;
?????????????
}
I have something like this, now how would I be able to add another element to this array? Please help, this is very urgent! Thanks.
-
Oct 18th, 2001, 08:56 PM
#2
Hyperactive Member
possible
People accomplished these by creating a dynamic array class.
This is how it 'adds'.
It add by creating and allocating another big array and copies the original array over and deletes the original array. And points it to the new array.
-
Oct 18th, 2001, 10:08 PM
#3
Thread Starter
Lively Member
Using my example, what code would you use to increase the number of elements in x from 4 to 5 after everything has been initialized?
-
Oct 18th, 2001, 11:17 PM
#4
Hyperactive Member
Code:
//I am not creating and using class
#include<iostream.h>
#include<memory.h>
bool add(int *index);
int *ip;
int main(void)
{
int index=4;
ip= new int[index];
if (ip)
{
if (add(&index)==false)
return 1;
}
else
{
cerr<<"fails";
return 1;
}
..........
ip[4]=2; //dummy usage
cout << ip[4];
return 0;
}
bool add(int *index)
{
int *ptemp=new int[++(*index)];
if (ptemp==0)
{
cerr<<"array allocation fails"<<endl;
return false;
}
memcpy(ptemp,ip,(*index)*sizeof(int));
ptemp[*index-1]=0;
if (ip)
delete [] ip;
ip=ptemp;
return true;
}
Last edited by transcendental; Oct 18th, 2001 at 11:32 PM.
I'm a VB6 beginner.
-
Oct 19th, 2001, 01:50 AM
#5
Hyperactive Member
There is some mistakes in the code.
There is some mistakes in the code.
I have edited and corrected it.
Do not use the code previously posted.
-
Oct 19th, 2001, 03:36 PM
#6
Thread Starter
Lively Member
Thanks, but now I have another problem. I have defined a class 'tb_textbox' and a GLOBAL variable tb[1] as tb_textbox. I also have another variable boxnum which is an int and keeps track of the number of the elements in tb. I believe that I can copy the contents of tb into a temperate variable, but I don't know how to redefine the global tb from within a function with greater number of element and still keep it global. I tried something like this:
void addtextbox(int num);
tb_textbox temp;
memcpy(temp,tb,sizeof(tb));
tb=new tb_textbox[boxnum+num];
}
This gives me an error at tb=new tb_textbox[boxnum+num]; saying that Lvalue is required.
Question 1: How do you (re)define a global variable from a function?
Question 2: If I put a line like tb_textbox tb[];. It gives me an error saying that the size of tb is either 0 or unknown. I can do this with something like int x[];, so what's different about a class?
Question 3: Is it possible to define an array with a variable number of elements? If I put something like int x[y];, it would give me an error saying that a constant value is required. Any way to do this?
Thanks.
-
Oct 20th, 2001, 02:07 PM
#7
Thread Starter
Lively Member
Man this is hard. Ok, here is the basic outline of what I have so far:
1. I define a global array of class tb_textbox: 'tb_textbox tb[1];'
2. During the program execution I need to either increase or decrease the number of elements in this array. For this I define a function 'addtextbox(int num);
3. In this function, I define a temporary variable that will hold all data from tb so that nothing is lost: 'tb_textbox *temp=tb;'
4. From what I understand, step 3 should copy all data in tb to the memory location 'temp'.
5. Now I have to either redefine tb with the needed number of elements and still keep it global (goes back to how would I define a global variable from a function), or somehow add elements to what’s already there. This also means that I have to be able to define an array with a dynamic number of elements, and not use a pointer.
6. I still have no idea how to do this since I can't use tb as a pointer (meaning I can't define it like: 'tb_textbox *tb'), because then when I try to access a function that was defined in the class tb_textbox from tb[x] (something like: 'tb[1].getdim();'), it gives me an error saying that Structure required on left side of . or .*
Any more suggestions? BTW, thanks a lot transcendental, you've given me some good advice, but now I just need to know some more so that I would be able to put this all together.
-
Oct 20th, 2001, 03:38 PM
#8
Hyperactive Member
Explanation to point 3:tb is a global pointer.In addtextbox(), I declare a temp pointer and allocate the memory bigger by one tb_textbox unit by using 'new'. Then I copy memory contents of tb to temp.
Then I delete off the memory pointed by tb pointer. Now tb points to nothing or randomly. I make tb points to temp,
eg.
tb=temp;
Now both tb and temp points to the same memory. but once the program gets out of addtextbox(), temp will fails to exist bcos temp is not a global variable. Never mind, you still have tb pointing to the same memory.
Trust me,you can use tb like that below but it is your responsibility now, not to access the array greater than its size which means you must keep track of size. One easy way to do that is to made size variable global.
eg,
tb[5].XXXXX=26;
You can do it this way if you have implemented correctly.
tb_textbox *tb= new tb_textbox[2];
tb[0].XXXXX(); //First element
tb[1].XXXXX(); //Second element
You cannot use a pointer without defining where it should point.
eg,
tb_textbox dummy;
tb_textbox *tb=&dummy;
tb[0].getdim();
-
Oct 20th, 2001, 03:58 PM
#9
Thread Starter
Lively Member
I understand what you are saying about how to increase the number of array elements when you have a pointer, but it doesn't work in my situation (or I didn't completely understand how this works ). Here is just an example of my situation:
class tb_textbox{
public:
void addtextbox();
} *tb[1];
void tb_textbox::addtextbox(){}
void main(){
tb[0].addtextbox();
}
Try to compile it. It says that 'Structure required on left of . or .*' referring to tb.addtextbox();. If this would work, then I don't think anything else should be a problem, but if tb is a pointer then I can't access any of its functions defined in tb_textbox. Why does it only want a structure?
-
Oct 20th, 2001, 05:39 PM
#10
A pointer is a variable that points to a location in memory. You define a pointer:
You initialize a pointer with new:
Code:
pInt = new int;
...or...
pInt = new int[10];
The first example allocates enough memory to hold a single int. Assign a value to it by "dereferenceing" the pointer:
Code:
*pInt = 10;
...or...
pInt[0] = 10;
Now, by convention, a dynamic array is called a "vector". A vector stores whatever type of data you want, and can be resized dynamically.
Now, in your case, you are trying to create an array of tb_textbox:
Code:
class tb_textbox
{
//implementation
}; // dont put tb[1] here...
tb_textbox* tb;
int main()
{
tb = new tb_textbox[10];
//now you can use it like an array
return 0;
}
In the last post, you tried to create a pointer to an array of size 1. Lose the "[1]", and do "tb = new tb_textbox[1]".
Z.
-
Oct 20th, 2001, 06:30 PM
#11
Thread Starter
Lively Member
So if I put 'tb=new tb_textbox[x]' what exactly will it do? From I understand it allocates the needed amount of memory to hold tb_textbox[x] and return a pointer to that memory location, right? If tb if declared as local, will it still be local after this operation? And, what happens to all the data that was in tb before this line, is it lost since the location of memory to where tb is pointing changes?
-
Oct 20th, 2001, 06:37 PM
#12
transcendental analytic
Originally posted by MXK
So if I put 'tb=new tb_textbox[x]' what exactly will it do? From I understand it allocates the needed amount of memory to hold tb_textbox[x] and return a pointer to that memory location, right? If tb if declared as local, will it still be local after this operation? And, what happens to all the data that was in tb before this line, is it lost since the location of memory to where tb is pointing changes?
variables are local, memory on the heap never is. If your variable runs out of scope, the memory on the heap is still there, and unaccessible, it's called a memory leak. Same thing if you assign something to tb while tb contains something that is on the heap.
As a thumb rule make sure everything newed get's deleted as well, using delete or delete[] statements. locally allocated dynamical variables should be deleted in the same scope, and dynamical variables on classes are allocated in constructors an deallocated in destructors.
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.
-
Oct 20th, 2001, 07:01 PM
#13
Thread Starter
Lively Member
Ok, I think I finally got it, just making sure that this would work:
Code:
class tb_textbox{
//some functions and vars here
} *tb;
void main(){
int boxnum=1, num=2;
tb=new tb_textbox[boxnum];
tb_textbox *temp=new tb_textbox[boxnum]; //temp var to store tb.
*temp=*tb; //store the contents of tb to temp
tb=new tb_textbox[boxnum+num]; //increase the size of tb to 3 array elements
*tb=*temp; //Copy all contents of temp (former tb) to the new tb
//do whatever else I need with tb.
delete[] temp;
delete[] tb;
}
Sound like this should work? I'm not sure about the line *temp=*tb;. Maybe I should use some mem function like memcpy or memmove. What would you guys suggest?
-
Oct 20th, 2001, 07:09 PM
#14
transcendental analytic
you should use an array of pointers instead
*temp=*tb; will only assign first element, not the whole array. ifyou want to assign the whole array use memcpy, but you don't want to do that. You want to resize the array and that works fine if you have an array of pointers, and just copy the pointers instead of the whole bunch of textboxes
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.
-
Oct 20th, 2001, 07:22 PM
#15
Thread Starter
Lively Member
Wait, but if I have an array of pointers, won't that mean that that array has to be a pointer too if I want to make it dynamic???
Why don't I do this as a diagram:
Let’s say that I’m working with a char tb and a char temp. “1” represents some data, “0” represents empty space or no real valid data:
Original tb[4]: "1111"
I want to be able to add another "1" to tb.
Temp var temp[4]: "0000"
temp is assigned the contents of tb. Now temp is: "1111"
tb is given a new memory location that can hold 5 elements: "00000"
And now temp is copied to tb starting from 1st element till the end of temp. Now tb is: "11110"
I think I have figured everything out up until the copying of temp back to tb. Why did you say I can't (or shouldn't) use memcpy or memmove?
-
Oct 20th, 2001, 09:30 PM
#16
Hyperactive Member
my explanation was for not using a class.
My code and explanation is for expanding a array without using class.
If you forget about tb_textbox and uses int instead by replacing every tb_textbox occurance with int. It will work. addtextbox() is a normal function, not a class function.
If you still want to use class, use the code below
Code:
#include<iostream.h>
#include<memory.h>
class tb_textbox
{
private:
static int size;
public:
int *x;
tb_textbox(void){x=new int[5]; return;};
void addtextbox(void);
~tb_textbox(void){delete [] x; return; };
};
void tb_textbox::addtextbox(void)
{
int *temp=new int[++size];
if (temp==0)
{
cerr<<"array allocation fails"<<endl;
return;
}
memcpy(temp,x,size*sizeof(int));
temp[size-1]=0;
if (x)
delete [] x;
x=temp;
return;
}
int tb_textbox::size=5; //you have to initialise static variables outside and like this.
int main()
{
tb_textbox tb;
tb.addtextbox();
tb.x[0]=2;
tb.addtextbox();
tb.x[1]=3;
cout<<tb.x[0]<<tb.x[1]<<endl;
return 0;
}
x cannot be 1, or else I kept getting illegal operation from windows so I set to 5. Took me some time to figure it out.
Now it is morning over Singapore.
-
Oct 21st, 2001, 10:12 AM
#17
Thread Starter
Lively Member
All right! I've gotten everything to work like I wanted. Just want to say thanks to everyone who helped.
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
|