dhairyashil
dhairyashil

Reputation: 621

storing addresses of std::list elements; memory

I am creating std::list of struct elements. With a certain criterion, I want to store addresses of few elements (because those addresses don't change(?)) from the list into std::vector for quick access in another usage. An example of the things is given below

#include <iostream>
#include <vector>
#include <list>

struct Astruct{
  double x[2];
  int rank;
};


int main(int argc, char *argv[]) {


  std::list<Astruct> ants; 
  std::vector< Astruct* > ptr; 

  for (auto i = 0; i != 20; ++i) {
    Astruct local;
    local.x[0] = 1.1;
    local.x[1] = 1.2;
    local.rank = i;
    // put in list
    ants.push_back(local); 


    // store address of odd numbers
    // rather than temperory address, permenent address from list is needed
    if(local.rank %2 == 0) ptr.push_back(&local);
  }

  // print the selected elements using addresses from the list
  for(int num = 0; num != ptr.size(); num++){
    Astruct *local;
    local = ptr.at(num);
    std::cout << " rank  " << local->rank << "\n";
  }

  /*
  // quick way to check whether certain address (eg 3rd element) exists in the std::vector
  std::list<Astruct>::iterator it = ants.begin();
  std::advance(it , 2);
  for(int num = 0; num != ptr.size(); num++){
    if(it == ptr.at(num)) std::cout << " exists in vector \n " ;
  }

  */
  // print memory in bytes for all variables
  std::cout << " sizeof Astruct " << sizeof(Astruct) << "\n";
  std::cout << " sizeof ants " << sizeof(ants) << "\n";
  std::cout << " sizeof ptr " << sizeof(ptr) << "\n";
}
  1. What's the way to access an address of a particular element from the list?
  2. Is it efficient method to add elements to list? (in first for loop)
  3. What is the quickest way to check whether certain address exists in the vector? (shown in comment block)
  4. How to determine the memory size in bytes for different variables here? (end of the code)

Thanks.

Upvotes: 2

Views: 921

Answers (1)

prog-fh
prog-fh

Reputation: 16805

  1. What's the way to access an address of a particular element from the list?

    • address=&(*iterator);
  2. Is it efficient method to add elements to list? (in first for loop)

    • the first loop does not use the list at all! (Ah, OK, after edition it does)
    • all the addresses which are stored in the vector refer to a local variable which disappears after each iteration; this is undefined behaviour (very probably, but nothing is certain, all these addresses are the same)
  3. What is the quickest way to check whether certain address exists in the vector? (shown in comment block)

    • usualy std::find() from <algorithm> is suitable.
  4. How to determine the memory size in bytes for different variables here? (end of the code)

    • std::cout << " sizeof Astruct " << sizeof(Astruct) << "\n"; is OK
    • std::cout << " sizeof ants " << size(ants)*sizeof(Astruct) << "\n"; is an approximation since we don't know the overhead of the list and its nodes
    • std::cout << " sizeof ptr " << size(ptr)*sizeof(Astruct *) << "\n"; is an approximation since we don't know the overhead of the vector

Upvotes: 2

Related Questions