Converting byte arrays to vectors

This is a discussion on Converting byte arrays to vectors within the C++ Programming forums, part of the General Programming Boards category; I have function - Stash::add - to recieve any kind of value, and store that value as a byte array: ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    22

    Converting byte arrays to vectors

    I have function - Stash::add - to recieve any kind of value,
    and store that value as a byte array:

    Code:
    int Stash::add(const void* element) {
    	........
    	........
      // Copy element into storage,
      // starting at next empty space:
      int startBytes = next * size;  /* size - the size of the 
                                      data type passed to add() */
      unsigned char* e = (unsigned char*)element; /* storage is a
                                                  unsigned char array */
      for(int i = 0; i < size; i++)
        storage[startBytes + i] = e[i];
      next++;
      return(next - 1);
    }
    And - Stash::fetch - to get a value at a specific position in
    that byte array:

    Code:
    void* Stash::fetch(int index) {
    	........
    	........
      // Produce pointer to desired element:
      return &(storage[index * size]);
    }
    Now, I have to change this codes' underlying data structure
    to vector<char>.(This means to store each byte of 'element'
    in subsequent positions of the vector,simulating a char array, right?)
    So, I change it like this:

    Code:
    int Stash::add(const void* element) {
      unsigned char* e = (unsigned char*)element;
      for(int i = 0; i < size; i++)
        storage.push_back(e[i]); // now storage is changed to a vector
      next++;
      return(next - 1); 
    }
    
    void* Stash::fetch(int index) {
      int ind = index;
      int trueInd = ind * size;
    	........
    	........
      unsigned char* intstore = new unsigned char[size];
      for(int x=0; x<=size; x++)
       intstore[x] = storage.at(trueInd+x);
      return &intstore; 
    }
    In main(), the function calls are like this:

    Code:
    Stash intStash;
    	........
      for(int i = 0; i < 100; i++)
        intStash.add(&i);
      for(int j = 0; j < intStash.count(); j++)
        cout << *(int*)intStash.fetch(j) << endl;
    Is the changes I have made are correct?
    (If anybody wants the whole code to figure out what's actually happening,please be kind to give a little notification)

    - Wish a beautiful march to all -
    Last edited by kasun; 03-01-2004 at 10:13 AM.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    STL is a good solution. Check out various STL algorithms such as for_each(), etc.

    Kuphryn

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    22
    Thanks, gonna check STL algo soon.
    But for now, please check whether the code is OK,
    'cause it doesn't prints correct values.

    Here's the whole thing:
    Code:
    // header.h
    #include<vector>
    using namespace std;
    
    struct Stash {
      int size;      // Size of each space
      int quantity;  // Number of storage spaces
      int next;      // Next empty space
       // Dynamically allocated array of bytes:
      vector<char> storage;
    
      void initialize(int size);
      void cleanup();
      int add(const void* element);
      void* fetch(int index);
      int count();
      void inflate(int increase);
    };
    Code:
    // Declare structure and functions:
    #include "header.h"
    #include <iostream>
    #include <cassert>
    using namespace std;
    // Quantity of elements to add
    // when increasing storage:
    const int increment = 100;
     
    void Stash::initialize(int sz) {
      size = sz;
      quantity = 0;
      //storage = 0;
      next = 0;
    }
     
    int Stash::add(const void* element) {
     // if(next >= quantity) // Enough space left?
      //  inflate(increment);
      // Copy element into storage,
      // starting at next empty space:-
      unsigned char* e = (unsigned char*)element;
      for(int i = 0; i < size; i++)
        storage.push_back(e[i]); // ***
      next++;
      return(next - 1);
    }
     
    void* Stash::fetch(int index) {
    	int ind = index;
    	int trueInd = ind * size;
      // Check index boundaries:
      assert(0 <= index);
      if(index >= next)
        return 0; // To indicate the end
      // Produce pointer to desired element:
      unsigned char* intstore = new unsigned char[4];
      for(int x=0; x<=size; x++)
       intstore[x] = storage.at(trueInd+x);
      //delete []intstore;
      return &intstore; 
    }
    
    int Stash::count() {
      return storage.size(); // Number of elements in CStash
    }
     
    /*void Stash::cleanup() {
      if(storage != 0) {
        cout << "freeing storage" << endl;
    //    delete []storage;
      }
    } *
    Code:
    // main
    #include "header.h"
    //#include "../require.h"
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
     
    int main() {
      Stash intStash;
      intStash.initialize(sizeof(int));
      for(int i = 0; i < 100; i++)
        intStash.add(&i);
      for(int j = 0; j < intStash.count(); j++)
        cout << "intStash.fetch(" << j << ") = "
             << *(int*)intStash.fetch(j)
             << endl;
    }
    Some are commented out, 'cause not sure whether they are needed with vectors.

  4. #4
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    You are returning a bad pointer I think?

    Since variables declared withing a function are local they are out of scope once the function exits when that happens they are destroyed and the memory the pointer points to is freed.

    Code:
    void* Stash::fetch(int index) {
      int ind = index;
      int trueInd = ind * size;
    	........
    	........
      unsigned char* intstore = new unsigned char[size];
      for(int x=0; x<=size; x++)
       intstore[x] = storage.at(trueInd+x);
      return &intstore;  
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 09:21 AM
  2. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 11:35 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 05:46 PM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM

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