944,168 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 10529
  • C++ RSS
Jan 27th, 2005
0

template array initialization

Expand Post »
hey folks,

i have a template created that holds an array of type T elements. my problem is this ...

i am converting this code from a souped up Array class that takes ints as it's elements to this template Array<T> class that will hold T elements. in the original class, i knew that i was going to be dealing with ints as the elements. so i had a default constructor and a copy constructor that both called a private init method to initialize the array depending on what was passed in.

C++ Syntax (Toggle Plain Text)
  1. void IntArray::
  2. init ( const int *data, int sz)
  3. {
  4.  
  5. assert( (0 <= sz) && (sz <= MAXINT));
  6.  
  7. d_num_elements = sz;
  8. if ( 0 == d_num_elements ){ // if no elements in array
  9. d_array = NULL; // array points to NULL
  10. }
  11. else{
  12. d_array = new int [d_num_elements];
  13. // allocate memory
  14. // if new returns NULL then no
  15. // more available memory
  16. assert ( NULL != d_array );
  17. }
  18. for (int i=0; i < sz; i++ ){
  19.  
  20. if ( NULL == data ){
  21. self [i] = 0;
  22. }
  23. else{
  24. self[i] = data[i];
  25. }
  26. }
  27. }

so how do i conver this to make it work for type T? i can't initialize the elements to 0 ...

thanks
crq
Similar Threads
crq
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crq is offline Offline
24 posts
since Jan 2005
Jan 28th, 2005
0

Re: template array initialization

C++ Syntax (Toggle Plain Text)
  1. void IntArray::
  2. init ( const T *data, int sz)
  3. {
  4. assert( (0 <= sz) && (sz <= MAXINT));
  5.  
  6. d_num_elements = sz;
  7. if ( 0 == d_num_elements ){ // if no elements in array
  8. d_array = NULL; // array points to NULL
  9. }
  10. else{
  11. d_array = new T [d_num_elements];
  12. }
  13. for (int i=0; i < sz; i++ ){
  14. if ( NULL == data ){
  15. self[i] = T();
  16. }
  17. else{
  18. self[i] = data[i];
  19. }
  20. }
  21. }
>assert ( NULL != d_array );
This is a very bad idea for two reasons. First, assert is meant for debugging program errors. Memory allocation failing is not a program error, it's a runtime error that needs to be addressed other than abruptly terminating and giving the user a cryptic error message designed for programmers. Your first assert is good (though an exception would be better, see below), but your second is not. Also, classes should NEVER terminate the program. Throw an exception and have the calling application decide what to do. Finally, new doesn't return a null pointer by default anymore, it throws a bad_alloc exception. Once again, this is something that the calling application needs to handle, so you can just let the exception propagate.

I'll assume that your operator[] checks for d_array being null, otherwise the loop has a bug.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 28th, 2005
0

Re: template array initialization

i have 2 constructors that use this function

C++ Syntax (Toggle Plain Text)
  1. template<class T>
  2. Array<T>::
  3. Array ( int sz )
  4. {
  5.  
  6. assert ( 0 <= sz <= MAXINT );
  7. cout << "!!! IntArray default constructor called" <<endl;
  8.  
  9. init ( NULL, sz );
  10. }
  11.  
  12. template<class T>
  13. Array<T>::
  14. Array ( const Array<T>& x )
  15. {
  16. cout << "!!! IntArray copy constructor called" <<endl;
  17.  
  18. init ( x.d_array, x.size() );
  19. }

since the default constructor assigns the number of elements, there would be a NULL declared for the array. i just don't know what to put in the init() method (for either of these) to make the initialization work for a template. i can't initialize everything to 0 becuase what i pass in MAY not be ints. i can't initialize all the T elements of the array to NULL can i?

thanks
crq


Quote originally posted by Narue ...
C++ Syntax (Toggle Plain Text)
  1. void IntArray::
  2. init ( const T *data, int sz)
  3. {
  4. assert( (0 <= sz) && (sz <= MAXINT));
  5.  
  6. d_num_elements = sz;
  7. if ( 0 == d_num_elements ){ // if no elements in array
  8. d_array = NULL; // array points to NULL
  9. }
  10. else{
  11. d_array = new T [d_num_elements];
  12. }
  13. for (int i=0; i < sz; i++ ){
  14. if ( NULL == data ){
  15. self[i] = T();
  16. }
  17. else{
  18. self[i] = data[i];
  19. }
  20. }
  21. }
>assert ( NULL != d_array );
This is a very bad idea for two reasons. First, assert is meant for debugging program errors. Memory allocation failing is not a program error, it's a runtime error that needs to be addressed other than abruptly terminating and giving the user a cryptic error message designed for programmers. Your first assert is good (though an exception would be better, see below), but your second is not. Also, classes should NEVER terminate the program. Throw an exception and have the calling application decide what to do. Finally, new doesn't return a null pointer by default anymore, it throws a bad_alloc exception. Once again, this is something that the calling application needs to handle, so you can just let the exception propagate.

I'll assume that your operator[] checks for d_array being null, otherwise the loop has a bug.
crq
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crq is offline Offline
24 posts
since Jan 2005
Jan 28th, 2005
0

Re: template array initialization

>i can't initialize everything to 0 becuase what i pass in MAY not be ints.
I don't see what your problem is. If you read my answer you'll see that I used the default constructor for T (which works for built in types as well) as the replacement for 0. Did you try it? Or did you just assume that I haven't got a clue and proceed to give me irrelevant code snippets with the same question again?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 28th, 2005
0

Re: template array initialization

Quote originally posted by Narue ...
>i can't initialize everything to 0 becuase what i pass in MAY not be ints.
I don't see what your problem is. If you read my answer you'll see that I used the default constructor for T (which works for built in types as well) as the replacement for 0. Did you try it? Or did you just assume that I haven't got a clue and proceed to give me irrelevant code snippets with the same question again?
so sorry. i didn't realize that you had made changes to the code. the changes were minor fixes that make a world of difference. i didn't see them though. i thought you were quoting the code and had put it up to show me why the asserts did/didn't work. now i see that you took out the one that wasn't ok, and that you inserted the T. thanks for your help. i will try it now. didn't mean to imply that you "haven't got a clue". obviously, i am the one who hasn't got a clue. i won't bother you again.

thanks
crq
crq
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crq is offline Offline
24 posts
since Jan 2005
Jan 28th, 2005
0

Re: template array initialization

>i thought you were quoting the code and had put it up to show me why the asserts did/didn't work.
Fair enough. I can understand that mistake if you aren't familiar with my posting habits. Please ignore my previous comments.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Secure File Transfer Method with C++???
Next Thread in C++ Forum Timeline: Using Division Operator





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC