problem in classes and arrays

This is a discussion on problem in classes and arrays within the C++ Programming forums, part of the General Programming Boards category; i have this code: Code: Code: template <typename T> class Array { private: T *nArr; int aSize; public: Array() { ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    98

    problem in classes and arrays

    i have this code:

    Code:
    Code:
    template <typename T> class Array
    {
          private:
          T *nArr;
          int aSize;
          
          public:
          Array() { aSize = 0; }
          ~Array() { delete[] nArr; }
          
          T Element(T nVal)
          {
               T *tArr = new T[aSize+1];
               for(int i=0; i<aSize; i++)
                    tArr[i] = nArr[i];
               tArr[aSize] = nVal;
               delete[] nArr;
               nArr = tArr;
               aSize++;
          }
          
          void show()
          {
               for(int i=0; i<aSize; i++)
               cout<<"["<<i<<"] "<<nArr[i]<<"\n";
          }
    };
    
    
    void func(Array<char*> *array)
    {
         char str[4] = "abc";
         array->Element(str);
         array->show();
    }
    
    int main()
    {
         Array<char*> arr;
         
         func(&arr);
         arr.show();
         
         getch();
    }
    as u can see, i'm creating an object (arr) of type Array<char*> and in the "func()" function i'm trying to assign it a new element ("abc").
    then i print the whole array, first in func() and then in main().
    when i print it in func(), it prints "abc" like it supposed to, but when i print it in main() it doesn't work properly...
    what's the problem?

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    What do you mean by "it doesn't work poperly"? Is your error compile-time or run-time? Are error messages shown and if so, what are they?

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Trace the value of aSize through your program. It's initialized to zero and never increased when it has to be.
    Last edited by joshdick; 04-08-2005 at 08:02 AM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    The problem is that you are pushing the address of a local variable onto your Array object. Once the function exits, that address is no longer valid. There are different ways you could cope with this. One possibility would be to make str static. The better way would be to use an Array<string> object instead of what you are doing.

    Also I suggest adding something else to your constructor:
    Code:
     public:
          Array() { aSize = 0; nArr = 0; }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    Quote Originally Posted by hk_mp5kpdw
    The problem is that you are pushing the address of a local variable onto your Array object. Once the function exits, that address is no longer valid. There are different ways you could cope with this. One possibility would be to make str static. The better way would be to use an Array<string> object instead of what you are doing.

    Also I suggest adding something else to your constructor:
    Code:
     public:
          Array() { aSize = 0; nArr = 0; }
    to use the "string" i need to include string.h? coz i never managed to use it...
    are there any other solutions? how come i can't get the value of str and copy it to the array element instead of using its address?

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    #include <string>

    It will behoove you greatly to learn how to use the string class. It's very easy to use. Here's one link to get you started with that: http://www.bgsu.edu/departments/comp...cs/string.html
    Here's a lecture on them: http://www.cs.drexel.edu/%7Emcs171/W...CurrentSlide=0

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    i changed char* to string but when i compile it, it crashes

  8. #8
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    What errors is your compiler giving you?

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    i don't get any error from my compiler, when i open it, it just crashes

  10. #10
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Well, that single change isn't enough to fix your program. hk_mp5kpdw pointed out that you're setting the pointer nArr to point to tArr which goes out of scope at the end of that function. So, you've got to fix that as well.

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    no that's not that... i'm allocating tArr with 'new' and it ain't removed when the function ends.

  12. #12
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Quote Originally Posted by hiya
    no that's not that... i'm allocating tArr with 'new' and it ain't removed when the function ends.
    Yes and no. You're leaking memory, so in that way, the data pointed to by tArr is still there, eating up system resources. By the pointer tArr, because it is declared within that block of code, dies at the end of that block of code. Gone, finito, out of scope. Any attempt to reference it afterwards is wrong.

  13. #13
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    what i'm trying to do is to create a function to split a string into array, but i can't get it to work. do u know any function to split a string?

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Post the revised code you are trying to get working along with a more detailed description of this "splitting a string into an array" problem you are talking about (like with an example). I would guess that the substr member function of the string container can probably be used to do that.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  15. #15
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    so you're saying that the way i create a dynamic array is wrong? how whould u do it?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help on this problem using classes and arrays
    By john5754 in forum C++ Programming
    Replies: 21
    Last Post: 11-30-2008, 05:35 PM
  2. Problem with classes and Arrays
    By GonzO86 in forum C++ Programming
    Replies: 4
    Last Post: 03-02-2005, 03:35 PM
  3. Arrays with base/derived classes
    By shaeng in forum C++ Programming
    Replies: 3
    Last Post: 05-31-2004, 11:54 AM
  4. Problem with character arrays in classes
    By spoketoosoon in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2004, 02:57 AM
  5. Help with Arrays of Classes
    By GrNxxDaY in forum C++ Programming
    Replies: 15
    Last Post: 07-25-2002, 09:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21