Thread: Copy array into pointer

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    204

    Copy array into pointer

    I have written this code, and at first glance it does what I want, however I am worried that a) I am overwriting the array that is apssed from chord.getPattern() b) Im getting a memory leak that I want to get rid of, and c) is there generally a /what is the neater way to do it:

    Code:
    uint8_t* ChordBuilder::invert(uint8_t count, Chord chord) {
    	temp = chord.getPattern();
    	chord.invert(true);
    	//TODO count is how many times to invert. Moves root aswell however
    
    	for (uint8_t i = 0; i < count; i++){
    		temp = doInversion(temp, chord.getSize());
    	}
    	return temp;
    }
    
    uint8_t* ChordBuilder::doInversion(uint8_t* pattern, uint8_t size) {
    
    	temp = new uint8_t[size];
    	temp[0] = pattern[1];
    	for (int i = 1; i < size - 1; i++) {
    		temp[i] = pattern[i + 1];
    	}
    	temp[size - 1] = pattern[0] + 12;
    	return temp;
    }
    temp is a member variable of ChordBuilder - and is expressed as:
    Code:
     uint8_t* temp;
    I dont want the pattern that chord stores, and passes with getPattern() to change - I fear it is at the moment?
    I would rather not use the "new" but I cant think how to get rid of it, however Im not sure where I would need to put the "delete"?

    Thanks
    Alex

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Cool, a music program!

    So doInversion moves all the "notes" down one element in the array, then takes the first note of the original array and puts it last, but an octave (12 semitones) higher. I.e., it calculates the next chord inversion.

    But because "temp" is a member variable (but why?), and you're using it for two purposes, you lose what is essentially the input to doInversion as soon as you assign the new array to temp.

    I'd remove temp as a member variable (the name alone is a clue that it's not really a member), and make it a local variable to each function.

    It doesn't look to me that the pattern that chord stores is necessarily changed by what you're doing (assuming a minimum of insanity hiding in the missing details).

    Where to put the delete is up to you. I don't know what you're doing with the data, so I can't say.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    Yeah, it doesnt need to be a member, its just It goes out of scope when a function finishes so I cant access it later, thats why I made it a member. I would rather do this without new/delete but I cant figure out how?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You'd have to explain exactly what your program is trying to do and possibly post the whole thing, otherwise I can't tell if it's possible to do it without new/delete or not.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by a.mlw.walker
    temp is a member variable of ChordBuilder
    What does "temp" mean? It is a common abbreviation for "temporary", but that would be an unusual choice of name for a member variable, which usually is more long lived.

    Quote Originally Posted by a.mlw.walker
    I would rather do this without new/delete but I cant figure out how?
    Here is a rather direct translation of doInversion to use a vector:
    Code:
    std::vector<uint8_t> ChordBuilder::doInversion(uint8_t* pattern, uint8_t size) {
     
        std::vector<uint8_t> temp(size);
        temp[0] = pattern[1];
        for (int i = 1; i < size - 1; i++) {
            temp[i] = pattern[i + 1];
        }
        temp[size - 1] = pattern[0] + 12;
        return temp;
    }
    Obviously, you cannot directly substitute this because then the return type won't match what is expected.

    Related to this: what does chord.getPattern() return? It presumably returns a pointer to uint8_t, but why? Why not return a vector? Why not return an object of class type? If you must return a pointer, then who owns what the pointer points to?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why does DoInversion not take a vector directly?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    The reason I'm not using vectors is because this is on an 8 bit embedded platform. Its basically C, however using C++ I can use classes which I prefer. If I wanted to use vectors I would need the STL, however I could wrap an array in a struct couldn't I, then pass around the struct.

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    this is on an 8 bit embedded platform
    I would rather do this without new/delete but I cant figure out how?
    O_o

    Yeah, that would probably be a good idea.

    What is `Chord'?

    I could wrap an array in a struct couldn't I, then pass around the struct.
    Is that not what `Chord' is behind the scenes?

    *shrug*

    I'm going to assume `Chord' is or could be made such a thing.

    In any event, you can just change the way you process the target chord relative to what oogabooga offers.

    This is a little C for my tastes, but you should get an idea.

    Code:
    // You don't need a class aware of all possible mutations.
    // You only need a clean interface to allow mutations.
    // With that in mind, the `NextInversion' is just a function.
    Chord & NextInversion
    (
        Chord & fDestination
      , const Chord & fSource
      , uint8_t fDistance = 1
    )
    {
        // ...
        // Calculate the next `fDistance' inversions of `fSource' and copy the result into `fDestination'.
        // ...
        return(fDestination);
    }
    // ...
    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't a static array copy values from a dynamic array
    By c++noob145 in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2013, 10:25 AM
  2. copy string by pointer
    By mohsen in forum C Programming
    Replies: 6
    Last Post: 01-25-2012, 11:59 AM
  3. How to copy the content of an array into a pointer
    By firstoption in forum C Programming
    Replies: 2
    Last Post: 09-22-2011, 09:31 AM
  4. Copy char array to char pointer
    By Suseela in forum C Programming
    Replies: 9
    Last Post: 08-06-2009, 12:49 PM
  5. Copy character array to char * pointer
    By NuNn in forum C Programming
    Replies: 3
    Last Post: 02-20-2009, 12:33 AM