Taking a array threw fucntion and assigning it to local var

This is a discussion on Taking a array threw fucntion and assigning it to local var within the C++ Programming forums, part of the General Programming Boards category; Well i'm takign an array IE Code: int number[4] = { 1, 0 , 2 ,3 } ; then i ...

  1. #1
    Wen Resu
    Join Date
    May 2003
    Posts
    219

    Taking a array threw fucntion and assigning it to local var

    Well i'm takign an array
    IE

    Code:
    int  number[4] = { 1, 0 , 2 ,3 } ;
    
    then i pass it to my function
    
    Number Number::operator= (int *theNumber) {
      int **localNumber = &theNumber;
    }
    I'm just wondering if i am doing this correctly, i'm not at stage in my project were i can realy test it.
    IF i am doing it right could you guys suggest a better way to do it.

    Thanks
    Last edited by Iamien; 01-09-2004 at 10:28 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    Whilst what you wrote is valid, I doubt it's what you want.

    Describe in words what you want
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    since i posted this i think i got it the way i want it

    What i wanted was to be able to set the value of a member of my class by overlaoding the equals operator, which would take an array of int and make the value of the member equal to it.
    the problem i was having was i wans't sure how to take the passed variable, assign it to a local variable, and do it properly. this is what i ended up with

    Code:
    void Number::operator= (const int *theNumber) {
         const int *localNumber = theNumber;
         int currentPos = 0 ;
         while ( localNumber[currentPos] != NULL ) {
              currentPos++;
              }
         Number::Size = currentPos + 1;
         Number::Value = new int[Number::Size];
         currentPos = 0;
         while ( currentPos < Number::Size ) {
             Number::Value[currentPos] = localNumber[currentPos];
              currentPos++;
              }
        Number::Value[currentPos] = NULL;
    
    }

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Several errors.
    1) const int *localNumber = theNumber;
    No need for this.

    2) while ( localNumber[currentPos] != NULL ) {
    This won't work. NULL is an invalid value to compare against integers (though it unfortunatly compiles in C++), and you argument array is not guaranteed to be 0-terminated. In fact the example array from your post isn't. You would have to pass a second argument containing a size, but since that doesn't work with operator =, the function won't work out. You could pass a std::vector though.

    3) Number::Value = new int[Number::Size];
    Possible memory leak. What about this code?
    Code:
    Number n;
    int ar[4] = {1, 2, 3, 4};
    n = ar;
    n = ar;
    After the first assignment, Value is a pointer to dynamic memory. In the second, this pointer gets overwritten by the next allocation, and the old memory is a leak.

    4) while ( currentPos < Number::Size ) {
    A memcpy would be much faster.

    5) Number::Value[currentPos] = NULL;
    Assignment of NULL to int is no more valid than comparison. Besides, what if there is a 0 in the normal array? You class will think the number ends there.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    Thanks. i had already fixxed the NULL termination problem, as my array will only be hold numbers between 0-9 i terminate it with a 10. going over what you said, and fixing what i can.
    Thanks

Popular pages Recent additions subscribe to a feed

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