how can i store address of array elements in an another array element

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tekinalp67
    New Member
    • Oct 2008
    • 11

    how can i store address of array elements in an another array element

    hi,
    i am trying to implement a B+ tree. There is no specification in the implementation so i am using arrays to implement the B+ tree. However, there is a problem that i encountered which is I cannot store the address of an array element in an another array element. it is purpose is reaching the childs. The other thing that i want to mention is that the bucket size of the b+ tree is not specific. It will be determined at the beginning of the program. For example it can store 4,6 or 10 data. it does not matter. Here is my code any help will be appreciated.

    Code:
    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    using namespace std;
    #define length(a) (sizeof a / sizeof a[0])
    class Node{
    public:
    	int count;
    	int leaf;
    	int *data;
    	int *pointer;
    	Node(int a){
    		leaf = 1;
    		count = 0;
    		data = new int[a];
    		pointer = new int[a+1];
    
    	}
    };
    void insert(Node *node, int data, int line){
    	Node *temp = new Node(line);
    	temp = node;
    	int i = 0;
    	if(temp->leaf == 1){
    		if(temp->count != line){
    			while(temp->data[i] < data){
    				i++;
    			}
    			for (int j = line; j > i; j--){
    				temp->data[j] = temp->data[j - 1];
    			}
    			temp->data[i] = data;
    			temp->count++;
    
    		}
    		else{
    			Node *temp2 = new Node(line);
    			Node *temp3 = new Node(line);
    			//temp->pointer[0] = &(temp2->data[0]);
    			//temp->pointer[1] = &(temp3->data[0]);
    			for(int i = 0; i < 2; i++){
    				temp2->data[i] = temp->data[i];
    			}
    			for(int i = 2; i < 5; i++){
    				temp3->data[i] = temp->data[i];
    			}
    			for(int i = 0; i < 4; i++){
    				temp->data[i] = 0;
    			}
    			temp->data[1]=temp3->data[1];
    			
    		}
    	}
    	
    }
    /*void deneme1(){
    	int a[3];
    	int *b[4];
    	b[1] = &(a[1]);
    	cout << b[1] << endl;
    	cout << &(a[1]) << endl;
    
    
    }*/
    
    int main(){
    	Node *temp = new Node(4);
    	Node *temp2 = new Node(4);
    	temp2->data[0] = 2;
    	cout << &temp2->data[0] << endl;
    	temp->pointer[0] = (int)(&temp2->data[0]);
    	//long x = &temp2->data[0];
    	//temp->pointer[0] = &temp2->data[0];
    	temp->data[0] = 3;
    	temp->data[1] = 12;
    	temp->data[2] = 45;
    	insert(temp,34,4);
    	for(int i = 0; i < 4; i++)
    		cout << temp->data[i] << endl;
    
    	cout << "benim  " << temp->pointer[0] <<endl;
    
    	/*cout << sizeof(temp->data) << endl;
    	cout << length(temp->data) << endl;
    	cout << temp->data[0] << endl;
    	cout << temp->data[1] << endl;*/
    	return 0;
    
    
    }
    Thank you very much for your helps.
    Last edited by JosAH; Nov 1 '08, 11:59 AM. Reason: added [code] ... [/code] tags
  • AmeL
    New Member
    • Oct 2008
    • 15

    #2
    Could you please put the code bracket around your code for readability; and write us the error message which compiler produce ( with line number ) to make us easy to solve your problem, if that is a syntax error ?

    Comment

    • archonmagnus
      New Member
      • Jun 2007
      • 113

      #3
      Originally posted by tekinalp67
      hi,
      However, there is a problem that i encountered which is I cannot store the address of an array element in an another array element.
      I'm not sure what you mean by your statement. When I compile your code (using GCC version 4.1.2), I get the following output:
      [code=text]
      0x804a068
      3
      12
      34
      45
      benim 134520936
      [/code]

      In your code you have the following:
      [code=cpp]
      // This prints: "0x804a068"
      cout << &temp2->data[0] << endl;
      temp->pointer[0] = (int)(&temp2->data[0]);
      ...
      // This prints: "benim 134520936"
      cout << "benim " << temp->pointer[0] <<endl;
      [/code]

      The observant reader will note that 134520936_(Dec) =0x804a068_(Hex ). So it seems you are correctly storing the address of an array element into another element. Am I understanding your inquiry correctly?

      To make the comparison a bit easier, you could have used the lines:
      [code=cpp]
      // This prints: "0x804a068"
      cout << &temp2->data[0] << endl;
      temp->pointer[0] = (int)(&temp2->data[0]);
      ...
      // This prints: "benim 0x804a068"
      cout << "benim 0x" << hex << temp->pointer[0] <<endl;
      [/code]

      Comment

      • tekinalp67
        New Member
        • Oct 2008
        • 11

        #4
        Originally posted by AmeL
        Could you please put the code bracket around your code for readability; and write us the error message which compiler produce ( with line number ) to make us easy to solve your problem, if that is a syntax error ?
        Ok I will. But there is no error messages. The problem is cannot getting the address of desired element correctly.
        Code:
        #include<iostream> #include<stdio.h> #include<stdlib.h> using namespace std; #define length(a) (sizeof a / sizeof a[0]) class Node{ public: 	int count; 	int leaf; 	int *data; 	int *pointer; 	Node(int a){ 		leaf = 1; 		count = 0; 		data = new int[a]; 		pointer = new int[a+1]; 	} }; void insert(Node *node, int data, int line){ 	Node *temp = new Node(line); 	temp = node; 	int i = 0; 	if(temp->leaf == 1){ 		if(temp->count != line){ 			while(temp->data[i] < data){ 				i++; 			} 			for (int j = line; j > i; j--){ 				temp->data[j] = temp->data[j - 1]; 			} 			temp->data[i] = data; 			temp->count++; 		} 		else{ 			Node *temp2 = new Node(line); 			Node *temp3 = new Node(line); 			//temp->pointer[0] = &(temp2->data[0]); 			//temp->pointer[1] = &(temp3->data[0]); 			for(int i = 0; i < 2; i++){ 				temp2->data[i] = temp->data[i]; 			} 			for(int i = 2; i < 5; i++){ 				temp3->data[i] = temp->data[i]; 			} 			for(int i = 0; i < 4; i++){ 				temp->data[i] = 0; 			} 			temp->data[1]=temp3->data[1]; 		} 	} } void deneme1(){ 	int a[3]; 	int *b[4]; 	b[1] = &(a[1]); 	cout << b[1] << endl; 	cout << &(a[1]) << endl; } int main(){ 	Node *temp = new Node(4); 	Node *temp2 = new Node(4); 	temp2->data[0] = 2; 	cout << &temp2->data[0] << endl; 	temp->pointer[0] = (int)(&temp2->data[0]); 	//long x = &temp2->data[0]; 	//temp->pointer[0] = &temp2->data[0]; 	temp->data[0] = 3; 	temp->data[1] = 12; 	temp->data[2] = 45; 	insert(temp,34,4); 	for(int i = 0; i < 4; i++) 		cout << temp->data[i] << endl; 	cout << "benim  " << temp->pointer[0] <<endl; 	cout << "benim 00" << hex << temp->pointer[0] <<endl; 	cout << *&temp2->data[0] << endl; 	/*cout << sizeof(temp->data) << endl; 	cout << length(temp->data) << endl; 	cout << temp->data[0] << endl; 	cout << temp->data[1] << endl;*/ 	deneme1(); 	return 0; }
        In the line "temp->pointer[0] = (int)(&temp2->data[0]);"
        thank you

        Comment

        • tekinalp67
          New Member
          • Oct 2008
          • 11

          #5
          Originally posted by archonmagnus
          I'm not sure what you mean by your statement. When I compile your code (using GCC version 4.1.2), I get the following output:
          [code=text]
          0x804a068
          3
          12
          34
          45
          benim 134520936
          [/code]

          In your code you have the following:
          [code=cpp]
          // This prints: "0x804a068"
          cout << &temp2->data[0] << endl;
          temp->pointer[0] = (int)(&temp2->data[0]);
          ...
          // This prints: "benim 134520936"
          cout << "benim " << temp->pointer[0] <<endl;
          [/code]

          The observant reader will note that 134520936_(Dec) =0x804a068_(Hex ). So it seems you are correctly storing the address of an array element into another element. Am I understanding your inquiry correctly?

          To make the comparison a bit easier, you could have used the lines:
          [code=cpp]
          // This prints: "0x804a068"
          cout << &temp2->data[0] << endl;
          temp->pointer[0] = (int)(&temp2->data[0]);
          ...
          // This prints: "benim 0x804a068"
          cout << "benim 0x" << hex << temp->pointer[0] <<endl;
          [/code]
          first of thank you for your reply.
          I have question about the "x". what is it for
          for example there is an output that i generate.
          000221D8
          3
          12
          34
          45
          benim 139736
          benim 0x221d8
          2
          in here 000221D8 is not same with the 0x221d8.
          Thank you again for your helps.

          Comment

          • tekinalp67
            New Member
            • Oct 2008
            • 11

            #6
            I want to ask another thing. :)
            For example and assume that in my code this line
            Code:
            temp->pointer[0] = (int)(&temp2->data[0]);
            get the address of the temp2->data[0] to the temp->pointer[0]
            after getting the address of the element how can I get the actual value in that address I mean which statement will give it to me.
            The second question is how can i store the address of the elements in the insert method.
            In the code lines
            temp->pointer[0] = &(temp2->data[0]);
            temp->pointer[1] = &(temp3->data[0]);
            thank you. very much.
            I cannot put code tag around the codes because then there are &nbps which I cannot correct. Sorry for the inconvience.
            Last edited by tekinalp67; Nov 1 '08, 12:15 PM. Reason: adding new problem

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by tekinalp67
              For example and assume that in my code this line
              Code:
              temp->pointer[0] = (int)(&temp2->data[0]);
              get the address of the temp2->data[0] to the temp->pointer[0]
              after getting the address of the element how can I get the actual value in that address
              That is all very unsafe what you're doing there: it assumes that sizeof(int) ==
              sizeof(your pointer type). Better define a small union type that can safely hold
              both (one of each) type.

              kind regards,

              Jos

              Comment

              • tekinalp67
                New Member
                • Oct 2008
                • 11

                #8
                Originally posted by JosAH
                That is all very unsafe what you're doing there: it assumes that sizeof(int) ==
                sizeof(your pointer type). Better define a small union type that can safely hold
                both (one of each) type.

                kind regards,

                Jos
                thank you very much. but i am beginner in c++.
                Can you tell me how can i define a small union type.:)
                if you can write that piece of code i will very happy:)
                Thank you very much.

                Comment

                • tekinalp67
                  New Member
                  • Oct 2008
                  • 11

                  #9
                  Code:
                   void deneme1(){
                  	int a[3];
                  	int *b[4];
                  	b[1] = &(a[1]);
                  	cout << b[1] << endl;
                  	cout << &(a[1]) << endl;
                  }

                  In here one of the array is an int array the other is pointer of array. So there is no problem while storig the addresses.
                  How can I make the pointer of array to an array to not get the errors "1>------ Build started: Project: BPlusTreeSecond , Configuration: Debug Win32 ------
                  1>Compiling...
                  1>BPlusTree.cp p
                  1>c:\users\asus \documents\visu al studio 2008\projects\b plustreesecond\ bplustreesecond \bplustree.cpp( 15) : error C2440: '=' : cannot convert from 'int *' to 'int'
                  1> There is no context in which this conversion is possible
                  1>c:\users\asus \documents\visu al studio 2008\projects\b plustreesecond\ bplustreesecond \bplustree.cpp( 25) : error C2109: subscript requires array or pointer type
                  1>c:\users\asus \documents\visu al studio 2008\projects\b plustreesecond\ bplustreesecond \bplustree.cpp( 25) : fatal error C1903: unable to recover from previous error(s); stopping compilation
                  1>Build log was saved at "file://c:\Users\asus\D ocuments\Visual Studio 2008\Projects\B PlusTreeSecond\ BPlusTreeSecond \Debug\BuildLog .htm"
                  1>BPlusTreeSeco nd - 3 error(s), 0 warning(s)
                  ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
                  ".
                  Code:
                  class Node{
                  public:
                  	int count;
                  	int leaf;
                  	int data;
                  	int *pointer;
                  	Node(int a){
                  		leaf = 1;
                  		count = 0;
                  		data = new int[a];
                  		pointer = new int[a+1];
                  	}
                  };
                  In the above on the line 5 data is an ordinary array. it is not a pointer of array. But then there are lots of errors I encountered. Is there anyone who can propose a better solution.
                  Thank you very much.

                  Comment

                  • tekinalp67
                    New Member
                    • Oct 2008
                    • 11

                    #10
                    Please some one help me...
                    I change my code so that I can store the address of the array elements in an array. However, there is another problem which i mentioned earlier is that I cannot reach the element that this address refers. This is my code:
                    Code:
                    #include<iostream>
                    #include<stdio.h>
                    #include<stdlib.h>
                    using namespace std;
                    #define length(a) (sizeof a / sizeof a[0])
                    class Node{
                    public:
                    	int count;
                    	int leaf;
                    	int *data;
                    	int** pointer;
                    	Node(int a){
                    		leaf = 1;
                    		count = 0;
                    		data = new int[a];
                    		pointer = new int*[a+1];
                    	}
                    };
                    void insert(Node *node, int data, int line){
                    	Node *temp = new Node(line);
                    	temp = node;
                    	int i = 0;
                    	if(temp->leaf == 1){
                    		if(temp->count != line){
                    			while(temp->data[i] < data){
                    				i++;
                    			}
                    			for (int j = line; j > i; j--){
                    				temp->data[j] = temp->data[j - 1];
                    			}
                    			temp->data[i] = data;
                    			temp->count++;
                    		}
                    		else{
                    			Node *temp2 = new Node(line);
                    			Node *temp3 = new Node(line);
                    			//temp->pointer[0] = (int)&(temp2->data);
                    			//temp->pointer[1] = (int)&(temp3->data);
                    			for(int i = 0; i < 2; i++){
                    				temp2->data[i] = temp->data[i];
                    			}
                    			for(int i = 2; i < 5; i++){
                    				temp3->data[i] = temp->data[i];
                    			}
                    			for(int i = 0; i < 4; i++){
                    				temp->data[i] = 0;
                    			}
                    			temp->data[1]=temp3->data[1];
                    		}
                    	}
                    }
                    void deneme1(){
                    	int a[3];
                    	int *b[4];
                    	b[1] = &(a[1]);
                    	cout << b[1] << endl;
                    	cout << &(a[1]) << endl;
                    }
                    int main(){
                    	Node *temp = new Node(4);
                    	Node *temp2 = new Node(4);
                    	temp2->data[0] = 2;
                    	cout << &temp2->data << endl;
                    	temp->pointer[0] = (int*)(&temp2->data);
                    	//long x = &temp2->data[0];
                    	//temp->pointer[0] = &temp2->data[0];
                    	temp->data[0] = 3;
                    	temp->data[1] = 12;
                    	temp->data[2] = 45;
                    	insert(temp,34,4);
                    	for(int i = 0; i < 4; i++)
                    		cout << temp->data[i] << endl;
                    	cout << "benim  " << temp->pointer[0] <<endl;
                    	//cout << "benim 0x" << hex << temp->pointer[0] <<endl;
                    	cout << *&temp->pointer[0] << endl;
                    	/*cout << sizeof(temp->data) << endl;
                    	cout << length(temp->data) << endl;
                    	cout << temp->data[0] << endl;
                    	cout << temp->data[1] << endl;*/
                    	//deneme1();
                    	return 0;
                    }
                    on line 11 i changed the code.
                    Please some one help me to fix this.
                    Thank you.

                    Comment

                    • tekinalp67
                      New Member
                      • Oct 2008
                      • 11

                      #11
                      I changed the line 64
                      Code:
                      temp->pointer[0] = (int*)(&temp2->data[0]);
                      with this. Then there is no problem with accessing the first element of the temp2->data which is temp2->data[0].
                      From here how can i access to the second third and so on.Thank you.
                      Tekin

                      Comment

                      • tekinalp67
                        New Member
                        • Oct 2008
                        • 11

                        #12
                        It is very interesting that i discovered that. If i use this code line
                        Code:
                        temp->pointer[0] = (int*)(&temp2->data[0]);
                        I got the output
                        008921A0
                        3
                        12
                        34
                        45
                        benim 008921D8
                        2

                        Which shows that the addresses are different. However, it gets the right element.
                        When I keep the line
                        Code:
                        temp->pointer[0] = (int*)(&temp2->data);
                        like this then I got this output
                        001921A0
                        3
                        12
                        34
                        45
                        benim 001921A0
                        1647064
                        which shows that address are correct but the data is not.
                        Anyone help
                        thank you.

                        Comment

                        • tekinalp67
                          New Member
                          • Oct 2008
                          • 11

                          #13
                          Hi again,
                          I have a question. I cannot get the elements in the Node type.
                          Code:
                          #include<iostream>
                          #include <algorithm>
                          #include<stdio.h>
                          #include<stdlib.h>
                          
                          
                          using namespace std;
                          #define length(a) (sizeof a / sizeof a[0])
                          class Node{
                          public:
                          	int count;
                          	int leaf;
                          	int *data;
                          	int** pointer;
                          	Node(int a){
                          		leaf = 1;
                          		count = 0;
                          		data = new int[a];
                          		pointer = new int*[a+1];
                          	}
                          };
                          Node* insert(Node *node, int data, int line){
                          	Node *temp = new Node(line);
                          	temp = node;
                          	int i = 0;
                          	int counter = 0;
                          	if(temp->leaf == 0){
                          		for(int l = 0; l < line; l++){
                          			if (l == 0){
                          				if(temp->data[l] > data){
                          					counter = l;
                          					break;
                          				}
                          			}
                          			if(l = line-1){
                          				if(temp->data[line-1] < data){
                          					counter = l;
                          					break;
                          				}
                          			}
                          			if(l > 0 && l < line-1){
                          				if (temp->data[l] < data && temp->data[l+1] > data){
                          					counter = l;
                          					break;
                          				}
                          			}
                          		}
                          		//temp->pointer[counter]
                          		[B]Node *childEntry = new Node(4);
                          		//childEntry = *temp->pointer[counter];
                          		insert(*temp->pointer[counter],data,line);[/B]
                          	}
                          	if(temp->leaf == 1){
                          		if(temp->count != line){
                          			while(temp->data[i] < data){
                          				i++;
                          			}
                          			/*if(i == 3){
                          				temp->data[i] = data;
                          			}*/
                          			
                          			for (int j = line; j > i; j--){
                          				temp->data[j] = temp->data[j - 1];
                          			}
                          			temp->data[i] = data;
                          			
                          			temp->count++;
                          			//cout << temp->count << endl;
                          		}
                          		else{
                          			Node *temp2 = new Node(line);
                          			Node *temp3 = new Node(line);
                          			Node *temp4 = new Node(line+1);
                          			temp->pointer[0] = (int*)&(temp2);
                          			//cout << temp->pointer[0] << endl;
                          			temp->pointer[1] = (int*)&(temp3);
                          			//cout << temp->pointer[1] << endl;
                          			for(int k = 0; k < line; k++){
                          				temp4->data[k] = temp->data[k];
                          			}
                          			temp4->data[line] = data;
                          			unsigned int sizeOf = line + 1;
                          			sort(temp4->data, temp4->data + sizeOf);
                          			/*for(int i = 0; i < line + 1; i++)
                          				cout << temp4->data[i] << endl;*/
                          			for(int l = 0; l < line/2; l++){
                          				temp2->data[l] = temp4->data[l];
                          				//cout << temp2->data[l] << endl;
                          				temp2->count++;
                          			}
                          			//cout << temp2->count << endl;
                          			for(int l = line/2; l < line+1; l++){
                          				temp3->data[l-line/2] = temp4->data[l];
                          				//cout << temp3->data[l-2] << endl;
                          				temp3->count++;
                          			}
                          			//cout << temp3->count << endl;
                          			for(int l = 0; l < line; l++){
                          				temp->data[l] = 0;
                          				temp->count--;
                          			}
                          			temp->data[0]=temp4->data[line/2 + 1];
                          			temp->count++;
                          			delete temp4;
                          			temp->leaf = 0;
                          			temp2->leaf = 1;
                          			temp3->leaf = 1;
                          			//cout<<temp->count << endl;
                          		}
                          	}
                          	node = temp;
                          	return node;
                          }
                          /*void deneme1(){
                          	int a[3];
                          	int *b[4];
                          	b[1] = &(a[1]);
                          	cout << b[1] << endl;
                          	cout << &(a[1]) << endl;
                          }*/
                          int main(){
                          	Node *temp = new Node(4);
                          	Node *temp2 = new Node(4);
                          	Node *temp3 = new Node(4);
                          	Node *temp4 = new Node(4);
                          	temp2->data[0] = 2;
                          	temp2->data[1] = 6768;
                          	cout << &temp2 << endl;
                          	temp->pointer[0] = (int*)(&temp2);
                          	//&temp4 = temp->pointer[0];
                          	cout << *&temp->pointer[0] << endl;
                          	//temp4 = (temp->pointer[0]);
                          	//cout << temp4->data[0] << endl;
                          	temp->data[0] = 3;
                          	temp->data[1] = 12;
                          	temp->data[2] = 45;
                          	//insert(temp,34,4);
                          	temp->count = 3;
                          	temp3 = insert(temp,1,4);
                          	temp = insert(temp3,6,4);
                          	//for(int i = 0; i < 4; i++)
                          		//cout << temp3->data[i] << endl;
                          	//cout << "benim  " << temp->pointer[0] <<endl;
                          	//cout << "benim 0x" << hex << temp->pointer[0] <<endl;
                          	//cout << *(temp->pointer[0] + 0) << endl;
                          	/*cout << sizeof(temp->data) << endl;
                          	cout << length(temp->data) << endl;
                          	cout << temp->data[0] << endl;
                          	cout << temp->data[1] << endl;*/
                          	//deneme1();
                          	return 0;
                          }
                          On the line 51, i tried to get the node by giving the address of that node.
                          This action is going on lines 49,50,51. However, it does not accept it. So I am waiting your help about this problem.
                          I have a proposal about that but I cannot implement it so i think that you can help me about that. In the Node class there is a pointer array. If we can change it to hold the Node objects then the problem will be solved.
                          Thank you very much for your helps.

                          Comment

                          • tekinalp67
                            New Member
                            • Oct 2008
                            • 11

                            #14
                            why not somebody help about this problem????

                            Comment

                            Working...