CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Threaded View

  1. #1
    Join Date
    May 2004
    Posts
    28

    Lots of small [interesting] questions or "odds of C++ ?"

    Greetings all,

    Just a few intro words. I'm not new to programming, but I am sort of to C++. It's just that I didn't learn it as things are usually learnt (I read a little of this, a little of that, etc..), so I got tired of this and decided to learn it well once and for all. Now, I love C++ (who doesn't eh? ), but coming from Java and Delphi/Object Pascal some things just freaking me out about C++. With C++ being the most flexible language of all times, why in the world there's so much troubles with the arrays!? I mean, what gives, there isn't even a specific function defined which returns the size of the array! Thank God there's sizeof But wait, even that is screwed up! Read on

    Anyway, I'm sure something of what I'm going to ask has already been asked and most likely replied, but I have quite a few small questions so I thought I'd create a new thread, and will later on add my "small" questions here as they appear.

    Also, please notice my questions are of the "why" nature, in most of the cases I know the solutions, I just want to know WHY it is like this in C++, indepth explanations are more than welcome too

    Ok, in no specific order:

    // Problem #1 ----------------------------------------------------------
    // {

    This code works:

    Code:
    #include <iostream>
    
    using namespace std;
    
    const int ARR_SIZE = 9;
    
    int main()
    {
    	long arr[ARR_SIZE];
    	int arr_length;
    
    	for (int i=0; i<ARR_SIZE; i++)
    	{
    		arr[i] = i;
    	}
    
    	arr_length = sizeof(arr)/sizeof(arr[0]);
    	cout << arr_length << endl;
    
    	return 0;
    }
    Output:
    Code:
    9
    Press any key to continue


    Why doesn't the following code work? :

    Code:
    #include <iostream>
    
    using namespace std;
    
    const int ARR_SIZE = 9;
    
    template <class T>
    int arr_len(T* arr)
    {
    	return sizeof(arr)/sizeof(arr[0]);
    }
    
    int main()
    {
    	long arr[ARR_SIZE];
    	int arr_length;
    
    	for (int i=0; i<ARR_SIZE; i++)
    	{
    		arr[i] = i;
    	}
    
    	arr_length = arr_len(arr);
    	cout << arr_length << endl;
    
    	return 0;
    }
    Output:
    Code:
    1
    Press any key to continue
    I mean it compiles just fine, but the result is always "1". Which btw would be logical since the NAME of the array is the POINTER to its first element, so:
    sizeof(arr)/sizeof(arr[0]);
    in the case of the arr_len function sizeof(arr) and sizeof(arr[0]) is one and the same thing, and the result of course is *one* (1), but why then does the compiler treat the name of the array in main() function as sizeof(arr[X])*ARR_SIZE, where X = number of any element in the "arr" array ?
    Amazing

    // } // end of problem #1


    // Problem #2 ----------------------------------------------------------
    // {

    We all know that there are dynamic arrays and static arrays. In Delphi I have absolutely NO problems with arrays as they're super flexible. Delphi supports both 1) dynamic arrays, 2) static arrays and 3) static arrays which can be allocated during run-time. Dynamic arrays are not supported by Java, by I can bypass this problem by creating a new array and copy data from one array to the other, approximately like this (Java pseudocode):

    Code:
    int arr1[];
    int user_value;
    user_value = get_int_from_user();
    arr1 = new int[user_value];
    
    int arr_size = length(arr1);
    int arr2[];
    arr2 = new int[arr_size*2];
    
    for (int i=0; i<arr_size; i++)
    {
        arr2[i] = arr1[i];
    }
    Both arr1 and arr2 are static arrays, but their size is UNKNOWN until *run-time*, so you can guess that using the above technique I can code a function which will change the size of the array, which will make an illusion of a dynamic array.
    Now in C++ I can't even do that ! Please don't go telling me about STL's vector class (I know it's "dynamic"), I'm concerned about usual arrays. Why can't this bit of code just work?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int size;
    	cout << "Please input an integer: ";
    	cin >> size;
    	int arr[size];
    
    	return 0;
    }
    YES, I can do it this way:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int size;
    	cout << "Please input an integer: ";
    	cin >> size;
    	int* arr;
    	arr = new int[size];
    
    	return 0;
    }
    and that's cool, BUT take a look at problem #3 then

    // } // end of problem #2

    // Problem #3 ----------------------------------------------------------
    // {
    Assume we have the following code, which works just fine:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int test;
    	test = 7;
    	int* ptrInt= &test;
    
    	cout << "The address of \'test\' value = " << ptrInt;
    	cout << "\nThe value of \'ptrInt\' pointer = " << *ptrInt << endl;
    
    	return 0;
    }
    Now I want to create an ARRAY OF POINTERS to int type. Okay, this works:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int* arr[3];
    	int var1 = 1;
    	int var2 = 2;
    	int var3 = 3;
    	arr[0] = &var1;
    	arr[1] = &var2;
    	arr[2] = &var3;
    
    	cout << "Contents of the array:\n";
    	cout << arr[0] << endl;
    	cout << arr[1] << endl;
    	cout << arr[2] << endl;
    
    	return 0;
    }
    But now I have that situation when size of the array is not known until run-time, so I do the following (this code DOES NOT WORK):

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int size;
    	cout << "Please input an integer: ";
    	cin >> size;
    	int* arr;
    	arr = new int[size];
    	int var1 = 1;
    	int var2 = 2;
    	int var3 = 3;
    	arr[0] = &var1; // ERROR: CANNOT CONVERT FROM int* to int
    	arr[1] = &var2;// ERROR: CANNOT CONVERT FROM int* to int
    	arr[2] = &var3;// ERROR: CANNOT CONVERT FROM int* to int
    
    	cout << "Contents of the array:\n";
    	cout << arr[0] << endl;
    	cout << arr[1] << endl;
    	cout << arr[2] << endl;
    
    	return 0;
    }
    Why has the array become a usual INT type array? How do I create an array of pointers to say INT type using the new operation which lets me allocate array's size during run-time?

    } // end of problem #3


    // Problem #4 ----------------------------------------------------------

    This code works just fine and destructor is called:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class foo
    {
      private:
        int data;
      public:
        foo(int);
        ~foo();
    };
    
    foo::foo(int value) : data(value) {}
    
    foo::~foo()
    {
    	cout << "Message from destructor.\n";
    }
    
    int main()
    {
    	foo dummy(3);
    
    	return 0;
    }

    Output:
    Code:
    Message from destructor.
    Press any key to continue
    This code works just fine too and destructor is called as well:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class foo
    {
      private:
        int data;
      public:
        foo(int);
        ~foo();
    };
    
    foo::foo(int value) : data(value) {}
    
    foo::~foo()
    {
    	cout << "Message from destructor.\n";
    }
    
    int main()
    {
    	foo* dummy = new foo(3);
    	delete dummy;
    
    	return 0;
    }
    Output:
    Code:
    Message from destructor.
    Press any key to continue
    And here's what I'm concerned about. For example I allocate memory using operator "new" the following way:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class foo
    {
      private:
        int data;
      public:
        foo(int);
        ~foo();
    };
    
    foo::foo(int value) : data(value) {}
    
    foo::~foo()
    {
    	cout << "Message from destructor.\n";
    }
    
    int main()
    {
    	foo& ref = *(new foo(3));
    
    	return 0;
    }
    Output:
    Code:
    Press any key to continue
    As you can see no destructor is called automatically. And I can't use delete operation to a reference variable. Problem - how do I free memory after allocating it this way?

    } // end of problem #4

    Okay, I have a lot more questions, but I'm already tired of typing I'll post more sometime later, and meanwhile, any help would be greatly appreciated

    Oleg
    Last edited by Xatrix; October 18th, 2005 at 08:28 AM.

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

Featured