Adding a local Variable to a Vector Question

This is a discussion on Adding a local Variable to a Vector Question within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm a C programmer developing a basic audio mixer in C++. The program consists of multiple audio tracks each ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    5

    Adding a local Variable to a Vector Question

    Hi,

    I'm a C programmer developing a basic audio mixer in C++. The program consists of multiple audio tracks each containing multiple audio clips. I'm running into problems with the audio clips which I add to a vector class variable as follows:

    Code:
    void AudioTrack::addClip(const char *infilename, unsigned long startIndex) {
    	
    	// Add the clip to the clip vector.
    	AudioClip* ac = new AudioClip(infilename, startIndex);
    	ac->setClipNumber(clipNumber);
    	audioClipVector.push_back(ac);
            ...
    When the program starts I add a couple of tracks and clips for testing. This seems to work OK. However once the program starts running the first track's clip vector shows incorrect values. Here's an example:

    Number of clips in Track 0: 2
    Number of clips in Track 1: 1
    Number of clips in Track 2: 1

    Number of clips in Track 0: 4062199338
    Number of clips in Track 1: 1
    Number of clips in Track 2: 1

    I wanted to check that the problem didn't lie in the way I was adding a local clip variable to the class variable vector - is that OK? Or is this the cause of the problem?

    Cheers,

    Chris

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,659
    It is okay, but are you really unable to store AudioClip objects, rather than pointer, in the vector? If so, you could consider storing a std::tr1::shared_ptr<AudioClip> instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    5
    Quote Originally Posted by laserlight View Post
    It is okay, but are you really unable to store AudioClip objects, rather than pointer, in the vector? If so, you could consider storing a std::tr1::shared_ptr<AudioClip> instead.
    How would this make a difference?

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Objects stored in STL container are already dynamically allocated. You are just adding another level of indirection

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    5
    I wonder if the problem could be that I'm dynamically allocating memory and therefore need a copy constructor?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,659
    Quote Originally Posted by cpsmusic
    I wonder if the problem could be that I'm dynamically allocating memory and therefore need a copy constructor?
    Since you are storing pointers, you can get away without a copy constructor.

    Perhaps you should post more code, e.g., the code that demonstrates the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    5
    Quote Originally Posted by laserlight View Post
    Since you are storing pointers, you can get away without a copy constructor.

    Perhaps you should post more code, e.g., the code that demonstrates the problem.
    Yes, I'll do that however the program is reasonably large so I'll have to work out which parts to post. I'll try and post something later today.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    5
    I've managed to figure out the problem. It wasn't related to the vectors. It was a simple programming oversight

    I recently changed the program so that it could output stereo audio (originally it was mono) and in the process several data buffers had to have their sizes doubled. I missed one - ooops!

    Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Uninitialized local variable
    By steeno321 in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2010, 06:46 PM
  2. From global to local variable
    By urban1990 in forum C Programming
    Replies: 11
    Last Post: 10-12-2010, 11:24 PM
  3. Local variable Question
    By WDT in forum C# Programming
    Replies: 2
    Last Post: 04-02-2009, 05:39 PM
  4. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  5. uninitialized local variable question
    By raist in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2006, 02:00 PM

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