-1

I am trying to make a vector of Uint8 from Uint8*, but for reason some of the values are not same. Here is my code.

std::vector<Uint8> wav_vector = {}; 
Uint8* wav_buffer_;   
for (unsigned int i = 0; i < wav_length_; i++) {
  wav_vector.push_back(wav_buffer_[i]);
}

Then later I am trying to verify the values:

for (unsigned int i = 0; i < wav_length_; i++){
    if (wav_buffer_[i]!=wav_vector[i]){
      cout<<i<<endl;
      printf("Orignal Buffer %u\n", wav_buffer_[i]);
      printf("Vector Buffer %u\n", wav_vector[i]);

    }
  }

These are my sample outputs:

Index: 0
Orignal Buffer 208
Vector Buffer 40

Index: 1
Orignal Buffer 72
Vector Buffer 3

Index: 2
Orignal Buffer 111
Vector Buffer 183

Index: 3
Orignal Buffer 1
Vector Buffer 97

Index: 4
Orignal Buffer 0
Vector Buffer 79

Index: 5
Orignal Buffer 0
Vector Buffer 127

Index: 8
Orignal Buffer 120
Vector Buffer 40

Index: 9
Orignal Buffer 251
Vector Buffer 3

Index: 10
Orignal Buffer 182
Vector Buffer 183

Index: 16
Orignal Buffer 0
Vector Buffer 176

Index: 17
Orignal Buffer 0
Vector Buffer 200

Index: 18
Orignal Buffer 0
Vector Buffer 109

Index: 19
Orignal Buffer 0
Vector Buffer 1

Index: 24
Orignal Buffer 0
Vector Buffer 176

Index: 25
Orignal Buffer 0
Vector Buffer 200

Index: 26
Orignal Buffer 0
Vector Buffer 109

Index: 27
Orignal Buffer 0
Vector Buffer 1

Index: 32768
Orignal Buffer 16
Vector Buffer 120

Any help would be really appreciated.

Note: I tried this and it works. But I want to do using for loop as I have some other audio formats which will require bit shifting.

std::vector<Uint8> wav_vector(&wav_buffer_[0], &wav_buffer_[wav_length_]);
aak
  • 107
  • 1
  • 4
  • 12
  • 1
    Your `wav_buffer_` pointer is properly initialized or is it just pointing somewhere? – Thomas B Preusser Sep 23 '16 at 21:40
  • 1
    Sidenote: Initializing a `vector` from an array does not require an explicit loop: `std::vector wav_vector(wav_buffer_, wav_buffer_+wav_length_);` Done. – ShadowRanger Sep 23 '16 at 21:45
  • its pointing somewhere in constructor. Uint8* wav_buffer_; AudioFile::AudioFile(SDL_AudioSpec* wav_file, Uint8* wav_buffer, Uint32 wav_length) : wav_file_(wav_file), wav_buffer_(wav_buffer), wav_length_(wav_length){ //SDL_FreeWAV(wav_buffer); } – aak Sep 23 '16 at 21:46
  • @ShadowRanger I tried that but getting an error: error: no match for call to ‘(std::vector) (Uint8*&, Uint8*)’ wav_vector(wav_buffer_, wav_buffer_ + wav_length_); – aak Sep 23 '16 at 21:51
  • 1
    What do you see if you print the array elements during the loop that does the copy? – Barmar Sep 23 '16 at 22:09
  • My suspicion is that something is modifying the array between the copy and print codes. – Barmar Sep 23 '16 at 22:13
  • @Barmar no, there is no code in copying and printing part. However, I tried this and it works. std::vector wav_vector(&wav_buffer_[0], &wav_buffer_[wav_length_]); I need same thing in for loop. – aak Sep 23 '16 at 22:21
  • 1
    There's no reason why the loop shouldn't work. What do you see if you put `cout << wav_buffer_[i];` in the copying loop? Do you see the same values that you see in the loop that prints the differences between the array and vector? – Barmar Sep 23 '16 at 22:22
  • @Barmar I tried that and its printing out same vector buffer values on each index as shown above in output. Thats weird, it should be same as original buffer value. – aak Sep 23 '16 at 22:51
  • Is there a good reason why you're using `Uint8` instead of the standard `uint8_t`? – Barmar Sep 23 '16 at 22:56
  • What if you change the comparison loop to use `cout` instead of `printf`? Just grasping at straws here. – Barmar Sep 23 '16 at 22:58
  • @Barmar Uint8 is a "SDL.h" type. I tried using uint8_t using the shortcut method of copying and its working fine too. When I do cout , it doesnt display anything. – aak Sep 23 '16 at 23:56

2 Answers2

5

Looks like there is a constructor for std::vector that can copy from an iterator (or pointer).

You could do something like:

std::vector<uint8_t> my_vector(&wave_buffer[0], &wave_buffer[N]);
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • Thanks, that works, but i want to try it using for loop as I have some other Audio formats, for eg 16 bits, 32 bits, signed 16 bit, signed 8 bit, unsigned 16bit, unsigned 8 bit(Current code is for it). Some of it requires bit shifting to convert everything into uint8_t as wav_buffer_ is of type Uint8 in for loop which I am not completely sure for now. – aak Sep 23 '16 at 22:04
0

Maybe you can try with memcpy:

#include <iostream>
#include <vector>

int main()
{
    uint8_t arr[3] = {1, 2, 3};
    std::vector<uint8_t> vec(3);
    memcpy(vec.data(), arr, 3);
    for (auto i : vec)
        std::cout << static_cast<int>(i) << " ";
    std::cout << std::endl;
}

More details about copying array to vector can be found: https://www.techiedelight.com/convert-array-vector-cpp/

Hope to be helpful.

waltermitty
  • 453
  • 3
  • 12