vijayan121 1,152 Posting Virtuoso

Are they compiled separately then linked?

Yes.

The linking may be done either statically or at load/run time from a .dll or .so

vijayan121 1,152 Posting Virtuoso

I pretty much get what was posted (need to look up sprintf), but the code below gives me the error that "fname, MAX_FILE_NAME_LENGTH, and strlen"

Why have you started messing around with all this stuff? Your origininal idea was far superior; just that you made a careless typo. Fix that and move on to the next task at hand.

// ofstream write ("test\\" (variable+".txt").c_str()  ); 
// this the place where I'm having trouble

ofstream write( ( "test\\" + variable + ".txt" ).c_str() ) ; 
// you wouldn't have any trouble now
vijayan121 1,152 Posting Virtuoso

Each constructor and destructor gets called each time the loop is executed...

If this is what you want, arkoenig has already given you the solution.

The purpose is to create a brand new object each time the loop is executed for as long as the loop runs. Once the loop terminates the objects will be destroyed.

If this is really what you need to do, use a std::vector<>

{
    std::vector<DataType> objects ;
    while( whatever ) // each time through the loop
    {
        objects.emplace_back( /*...*/ ) ; // construct a new object in situ
        objects.back().do_something() ; // use the object
        // ...
    }
}
// all the objects would have been destroyed by this tine
vijayan121 1,152 Posting Virtuoso

See the posts by arkoenig (towards the end) in this thread:
http://www.daniweb.com/software-development/cpp/threads/360216/c-value-initialization

vijayan121 1,152 Posting Virtuoso

while const int may not even be given storage because the compiler may just treat it as if it were a macro by inserting its value wherever it is used.

Yes, an object of type const int need not be stored anywhere.

But then, the same is also true of any object, even modifiable objects.

If we have int m = 23 ; m is an lvalue, and its adress can be taken: int* p = &m ; p will not be equal to the nullptr and p will not compare equal to a pointer to any object other than m; *p will alias m. All these are requirements of a conforming C++ implementatrion. That m should be actually stored somewhere is not.

The semantic descriptions in this International Standard defne a parameterized nondeterministic abstract machine. This International Standard places no requirement on the structure of conforming implementations. In particular, they need not copy or emulate the structure of the abstract machine. Rather, conforming implementations are required to emulate (only) the observable behavior of the abstract machine.
Foot note: This provision is sometimes called the 'as-if' rule, because an implementation is free to disregard any requirement of this International Standard as long as the result is as if the requirement had been obeyed, as far as can be determined from the observable behavior of the program....

Consequently:

Under the 'as-if' rule an implementation is allowed to store two objects at the same machine address …

vijayan121 1,152 Posting Virtuoso

The point of the exercise is to use list throughout rather than vector.

Yes; the point of the exercise is to understand the difference between using a sequence container like std::vector<> that provides random access to its values and one like std::list<> that does not.

double median( list<double> vec )
{
    typedef list<double>::size_type vec_sz ;
    vec_sz size = vec.size() ;
    if(size==0) throw domain_error("median of an empty list");

    // std::list<> does not provide random access to the values
    // std::list<>::iterator is not a random_access_iterator
    // we can't use std::sort() which requires random acccess iterators
    // http://en.cppreference.com/w/cpp/algorithm/sort
    // instead use the member function list<>::sort
    // http://en.cppreference.com/w/cpp/container/list/sort

    // sort(vec.begin(),vec.end());
    vec.sort() ;

    vec_sz mid=size/2;

    // std::list<> does not provide random access;
    // it does not have a subscript operator
    // we have to iterate linearly using its bidirectional iterator
    // keeping a count of the position and keeping track of the previous value

    /*
    for(list<double>::const_iterator it=vec.begin(); it!=vec.end(); ++it)   // Start of "list" median calculation
        {
            if(it == mid)
            {
                return size%2==0 ? (vec[*it]+vec[*it-1])/2 : vec[*it];
            }
        }
    */

    list<double>::size_type position = 0 ;
    double previous ;
    for( list<double>::iterator iter = vec.begin() ; iter != vec.end() ; ++iter )
    {
        if( position == mid ) return size%2==0 ? ( previous + *iter ) / 2 : *iter ;
        previous = *iter ;
        ++position ;
    }
}

If you have a current (C++11) compiler:

double median( list<double> seq )
{
    auto size = seq.size() ;
    if(size==0) throw …
vijayan121 1,152 Posting Virtuoso

Build the program from within the CodeBlocks IDE.

Start the command line interpreter (Start Menu -> Run... -> cmd on Windows XP ), switch to the directory which contains the executable (typically <Project Directory>\bin\Debug) and run it from the command prompt.
For example: > test.exe how now brown cow

Srinivas0 commented: clear explanation on command line parameters +2
vijayan121 1,152 Posting Virtuoso

"The most effective way to increase your knowledge is to try new problems in a controlled way. Pick one aspect of C++ that you haven't understood before and write a program that, aside from using that one aspect, uses only things that you have already mastered. Then do what it takes to understand what your program is doing - and why."
- Andrew Koenig and Barabara Moo in 'Ruminations on C++'.

So write a small program; something like this:

#include <iostream>

int main( int argc, char* argv[] )
{
    std::cout << "#arguments (argc): " << argc << '\n' ;
    for( int i=0 ; i<argc ; ++i )
        std::cout << "argument #" << i << " (argv[" << i << "]): " << argv[i] << '\n' ;
}

The run it from the command line with different command lines, look at the output and reason about it. For example, with a program called test:

> ./test
> ./test one two three four
> ./test -one -two=2 three,four

If you are just into arrays, have read something about at, to deepen your understanding write a small program:

#include <iostream>

int main( int argc, char* argv[] )
{
    enum { N = 4 } ;
    short array[N] = { 0, 1, 2, 3 } ;
    std::cout << "#elemebts: " << N << '\n'
              << "size of an element: " << sizeof( array[0] ) << '\n'
              << "size of array: " << sizeof(array) << '\n' ;

    for( int i …
vijayan121 1,152 Posting Virtuoso

"Another effective technique is to explain your code to someone else. This will often cause you to explain the bug to yourself. Sometimes it takes no more than a few sentences, followed by an embarrassed ``Never mind, I see what's wrong. Sorry to bother you.'' This works remarkably well; you can even use non-programmers as listeners. One university computer center kept a teddy bear near the help desk. Students with mysterious bugs were required to explain them to the bear before they could speak to a human counselor."
- Brian W. Kernighan and Rob Pike in 'The Practice of Programming'

vijayan121 1,152 Posting Virtuoso

> s this legal? (For example, can I set a GUIComponent as the return of a function and instead return a subclass?)

Yes. For example:

GUIComponent* getComponent() { return new TextLabel( /* ... */ )

is fine, provided TextLabel is a derived class of GUIComponent.


> Why is the compiler complaining?

The compile-time type of the result of the function is a pointer to GUIComponent (though the run-time of the pointed object might be TextLabel). And the compiler cannot see a member-function getFont() in GUIComponent.


> What would work?

A simple way would be to perform a type-safe run-time down-cast to TextLabel and call getFont() on the result of the cast. For example:

GUIComponent* gui_component = guiList[0]->getComponent(1) ;

TextLabel* text_label = dynamic_cast<TextLabel*>(gui_component) ;

if( text_label != nullptr ) 
{
   // use text_label
   auto font = text_label->getFont() ;
   // etc
}
else
{
   // this component is not a TextLabel 
}

See: http://www.bogotobogo.com/cplusplus/dynamic_cast.php

epicbeast9022 commented: Detailed, correct response! +1
vijayan121 1,152 Posting Virtuoso

You have not opened the stream successfully - the FILE* pointer was NULL at the time you did an fseek().

Verify that fopen() returned a non-NULL pointer. (Perhaps you are using a relative path to file which is no longer correct for the project copied into a different directory).

vijayan121 1,152 Posting Virtuoso

You need to add Sally.cpp to the project.

Menu: Project->Add Files ... IIRC.

vijayan121 1,152 Posting Virtuoso

Without using arrays and without using strings? That makes it interesting.
Something like a linked list would obviously work, but perhaps storing each character in the local frame of a (recursive) function would be the simplest.

#include <iostream>

void read_and_print_reverse()
{
    char c = std::cin.get() ;
    if( c != '\n' )
    {
        read_and_print_reverse() ;
        std::cout << c ;
    }
}

int main()
{
    std::cout << "enter a sentence (terminate it with a new-line): " ;
    read_and_print_reverse() ;
}
vijayan121 1,152 Posting Virtuoso

> Or something purely mathematical.

A problem in combinatorics.

A sequence of N integers contains the numbers [1,N] in some random order. For example:

enum { N = 25 } ;
std::vector<int> seq ;
for( int i = 0 ; i < N ; ++i ) seq.push_back(i+1) ;
// the sequence contains N integers [ 1, 2, 3 ... N-1, N ]
std::random_shuffle( seq.begin(), seq.end() ) ; // in some random order

The tranformation of this sequence is a sequence of N integers where each element is the count of integers appearing later in the original sequence that are less than the number in that position.

std::vector<int> transform_sequence( const std::vector<int>& seq )
{
    std::vector<int> result_seq( seq.size(), 0 ) ;

    for( std::vector<int>::size_type i = 0 ; i < seq.size() ; ++i )
        for( std::vector<int>::size_type j = i+1 ; j < seq.size() ; ++j )
            if( seq[j] < seq[i] ) ++result_seq[i] ;

    return result_seq ;
}

If the original sequence is: [ 5 2 7 3 1 6 8 4 ]
The transformation of it is: [ 4 1 4 1 0 1 1 0 ]

If the original sequence is: [ 3 1 2 5 6 4 ]
The transformation of it is: [ 2 0 0 1 1 0 ]

If the original sequence is: [ 3 6 9 7 2 8 1 4 5 ]
The transformation of it is: [ 2 4 6 4 1 3 0 0 0 …

vijayan121 1,152 Posting Virtuoso

> well, I posts this because when I search internet for the difference between for and while,
> many people say it work the same and tell that the only difference is the form.

These two are equivalent:

for( int i = 0 ; i < 10 ; ++i ) {}

{ int i = 0 ; while( i < 10 ) { ++i ; } }
"The for statement
[B]for( for-init-statement  condition ; expression ) statement[/B]

is equivalent to
[B]{
    for-init-statement 
    while( condition )  
    {
      statement 
      expression ;
    }
}[/B]

except that 
a. names declared in the for-init-statement are in the same declarative-region as those declared in the condition, 
b. a continue in statement (not enclosed in another iteration statement) will execute expression before re-evaluating condition." - [B]IS[/B]
vijayan121 1,152 Posting Virtuoso

> It has no effect

For standard types, a unary + always yields an r-value.

unsigned short s = 8 ;
unsigned short& r = s ; // s is an l-value
unsigned short&& rvr = +s ; // +s yiels an r-value

For standard types, a A unary + performs a promotion where applicable.

unsigned short s = 8 ;
auto x = s ; // type of x is unsigned short
auto y = +s ; // type of y is signed int
    //(unsigned int if int cannot represent the full range of unsigned short)

char c = 'A' ;
std::cout << c << ' ' << +c << '\n'

For user-defined types, the overloaded unary + operator could do anything one pleases.

#include <iostream>
#include <string>
#include <boost/xpressive/xpressive.hpp>

using namespace boost::xpressive ;

void foo( const std::string& str, const sregex& re )
{
    sregex_iterator iter( str.begin(), str.end(), re ), end ;
    for( ; iter != end ; ++iter ) std::cout << '[' << (*iter)[0] << "] " ;
    std::cout << '\n' ;
}

int main()
{
    std::string str = "abcd1234efgh789ij" ;

    foo( str, _d ) ; // prints 7 matches:  [1] [2] [3] [4] [7] [8] [9]

    foo( str, +_d ) ; // prints 2 matches: [1234] [789]
}
vijayan121 1,152 Posting Virtuoso
void foo( boost::asio::ip::tcp::iostream& stm )
{
    std::size_t bytes_available = stm.rdbuf()->available() ;
    // ...

    // the native socket descriptor can be retrieved for platform specific 
    // socket functionality not available through asio. for example:
    auto fd = stm.rdbuf()->native_handle() ; // version 1.48. 
    // IIRC, used to be stm.rdbuf()->native() earlier
    ::ifreq interface_request ;
    ::ioctl( fd, SIOCGIFNETMASK, &interface_request ) ;
    // ...
}
vijayan121 1,152 Posting Virtuoso

> My professor said this is useful in the event that your program is being "attacked."

Useful, only if you are using C-style arrays of char.

Don't create a problem that did not exist in the first place, and then try to solve it; just use std::string instead. std::string is not vulnerable to buffer overflow exploits on user input; if you use the member function at() instead of [] for element access, it is safe even if there are programming errors. Unless you decide to use its buffer as a C-style array of modifiable chars, in which case you deserve to be subject to every bad thing that could possibly happen.

The second problem with restricting the number of characters that can be entered is that if the input is too long, crud is left in the stream buffer which has to be than read and discarded prior to the next input. Just reading in whatever is entered, and then truncating the string if it turns out to be too long is simpler.

Something like:

std::string name ;
    constexpr std::string::size_type MAX_CHARS = 20 ;

    if( std::cout << "name (max " << MAX_CHARS << " chars)? " && std::getline( std::cin, name  ) )
    {
        if( name.size() > MAX_CHARS )
        {
            std::cerr << name << " is too long; truncating it\n" ;
            name = name.substr( 0, MAX_CHARS ) ;
        }
        // use name
    }

    // else i/o error
vijayan121 1,152 Posting Virtuoso

> My intentions are to first take everything from the file into a variable,
> then save everything from Start to the first comma minus the comma to a second variable,
> and so on so forth till EOF where each string or value between commas is saved as a separate variable

To split a std::string into separate tokens using a single delimiter, std::getline() and std::istringstream turn out to be quite handy.

std::vector< std::string > split( const std::string& str, char delimiter = ',' )
{
    std::vector< std::string > result ;
    std::istringstream stm(str) ;
    std::string token ;
    while( std::getline( stm, token, delimiter ) ) result.push_back(token) ;
    return result ;
}
Xaviorin commented: I have no rep power so here is a +zero thank you! +0
vijayan121 1,152 Posting Virtuoso

> Alternative once you have the index of the decimal point in the string, remove it,
> reverse the string, and insert the decimal point back into the string

Yes, Lerner. The simple way is the best way - if you want to manipulate text, treat it as text; if you want to perform arithmetic, treat it as a number.


> before using a stringstream to convert the string back to a floating point value

Just read it in as a string. Also sidestep the murky area of floating point rounding errors. Something like:

bool reverse_amounts( const char* in_path, const char* out_path )
{
    std::ifstream input(in_path) ;
    std::ofstream output(out_path) ;
    std::string id, amount ;
    while( input >> id >> amount ) output << id << ' ' << reverse_amount(amount) << '\n' ;
    return input.eof() && output.good() ;
}

> Such a mess. Much easier if file format restricted to known number of digits
> to the right of the decimal point.

It is a known number of digits to the right of the decimal point. Just that it is not a constant known at compile-time.

std::string reverse_amount( std::string amount )
{
    constexpr char DOT = '.' ;
    auto dot_pos = amount.find( DOT ) ; // locate the dot
    bool found_dot = dot_pos != std::string::npos ;
    if(found_dot) amount.erase( dot_pos, 1 ) ; // remove it
    std::reverse( amount.begin(), amount.end() ) ; // reverse the string
    if(found_dot) amount.insert( dot_pos, 1, DOT ) ; …
vijayan121 1,152 Posting Virtuoso

Use std::istringstream

std::string str = "I have a dog" ;
std::istringstream stm(str) ;
string word ;
while( stm >> word ) // read white-space delimited tokens one by one 
{
   // put word into array
}
vijayan121 1,152 Posting Virtuoso

> Is the member variable from the base class renamed?

No. There is no need to rename it; the fully qualified name of x in A is A::x and the fully qualified name of x in B is B::x.

Normal scoping rules apply for name look up; if a name is not found in an inner scope, the outer scope is looked up. The name of the base class behaves in a way similar to an outer scope.

#include <iostream>

struct A
{
    int x ;
    int y ;
};

struct B : A
{
    int x ;
    void foo( int x ) ;
};

void B::foo( int x )
{
    std::cout << x << ' ' // local variable 'x'
              << B::x << ' ' // member variable 'x' in B
              << A::x << '\n' ; // member variable 'x' inherited from A

    std::cout << y << ' ' // no local variable 'y', this->y
              << B::y << ' ' // no member variable 'y' in B, so A::y
              << A::y << '\n' ; // member variable 'y' in A (inherited by B)
}

> but how would this be implemented in C let's say ...? How would the class D as struct look ?
> To make it short , how is actually INHERITANCE IMPLEMENTED?
> ..my guess is that all compilers somehow go the same way ...

Compilers implement it by having an anonymous base class sub-object in the derived class …

vijayan121 1,152 Posting Virtuoso

Use std::result_of<> instead of boost::result_of<>

#include <functional>

struct S
{
    double operator() ( char, int& ) const ; // omitting const here is a (very) bad idea
};

int main()
{
    return sizeof( std::result_of< S(char, int&) >::type ) == sizeof(double) ;
}

From Boost documentation:
"If decltype is not enabled, then automatic result type deduction of function objects is not possible. Instead, result_of uses the following protocol to allow the programmer to specify a type. When F is a class type with a member type result_type, result_of<F(T1, T2, ..., TN)> is F::result_type. When F does not contain result_type, result_of<F(T1, T2, ..., TN)> is F::result<F(T1, T2, ..., TN)>::type when N > 0 or void when N = 0. Note that it is the responsibility of the programmer to ensure that function objects accurately advertise their result type via this protocol"

vijayan121 1,152 Posting Virtuoso

> Is this kind of assignment well defined?

yes; it is well-defined and well-behaved. Calls release() on the auto_ptr.

vijayan121 1,152 Posting Virtuoso

Have a look at RapidXML, perhaps? Extremely good as long as DTD support is not required.
http://rapidxml.sourceforge.net/

From the Boost Property Tree documentation:
"The XML format is an industry standard for storing information in textual form. Unfortunately, there is no XML parser in Boost as of the time of this writing. The library therefore contains the fast and tiny RapidXML parser (currently in version 1.13) to provide XML parsing support. RapidXML does not fully support the XML standard; it is not capable of parsing DTDs and therefore cannot do full entity substitution."

There was a discussion about this in lib.boost.devel and the consensus there was to use RapidXML.

vijayan121 1,152 Posting Virtuoso

> What should we set for the template parameter of the packaged_task?#1 or #2?

#1 if the packaged_task is boost::packaged_task<> which is defined this way:

template< typename RESULT_TYPE > class packaged_task
{
    public:
        // construction and destruction
        template< typename FUNCTION_TYPE > explicit packaged_task( const FUNCTION_TYPE& f ) ;

        explicit packaged_task( RESULT_TYPE(*f)() ) ;

        template< typename FUNCTION_TYPE > explicit packaged_task( FUNCTION_TYPE&& f ) ;

        // ...

#2 if the packaged_task is std::packaged_task<> which is defined this way:

template<typename> class packaged_task ; // generalization is undefined

template< typename  RESULT_TYPE,  typename... ARG_TYPES >
class packaged_task< RESULT_TYPE( ARG_TYPES... ) > // what we use is this specialization
{
public:
    typedef RESULT_TYPE result_type;

    // construction and destruction
    packaged_task();

    template <typename FUNCTION_TYPE > explicit packaged_task( const FUNCTION_TYPE& f ) ;

    explicit packaged_task( RESULT_TYPE(*f)() ) ;

    template <typename FUNCTION_TYPE > explicit packaged_task( FUNCTION_TYPE&& f ) ;

    // ...

Using std::packaged_task<>

struct my_task : std::binary_function< bool, double, int >
{
    result_type operator() ( double d, int i ) const
    {
       return std::cout << "my_task: { " << d << ", " << i << " }\n" ;
    }
};

void test_it()
{
  std::packaged_task < bool( double, int ) > atask( my_task, 7.84, 6789 ) ;
  std::unique_future<bool> future( atask.get_future() ) ;
  std::thread( std::move( atask ) ) ;
  // do something
  future.wait() ;
}
vijayan121 1,152 Posting Virtuoso

> I'm trying to pass fout between functions.

You cannot pass a stream object to a function by value - the copy constructor is private / deleted.

Pass by reference instead: void my_function( std::ostream& fout ) ;

vijayan121 1,152 Posting Virtuoso

Hint:
Each element in {3,5,7,9,11} is the corresponding element in {2,4,6,8,10} plus one.
Each element in {6,10,14,18,22} is the corresponding element in {3,5,7,9,11} multiplied by two.

vijayan121 1,152 Posting Virtuoso

> What are the cases would make us like to use std::promise rather than std::package?

std::future and std::promise are fairly low level, primitive constructs. AFAIK, these are expected to be the building blocks used to create high level abstractions (like lock-free data structures).

The basic idea is a good one - separate the communication channel (std::promise) from the thread creation and synchronization mechanisms. This allows a lot of flexibility in the creation of higher level constructs.


> What is the meaning of ...

typedef int result_type ;
std::packaged_task<result_type> // #1. is std::packaged_task<int>  
std::packaged_task< result_type() > // #2. is std::packaged_task< int() >

In #1, the template parameter is an int.
In #2, the template parameter is a nullary function which returns an int.

vijayan121 1,152 Posting Virtuoso

> But I used <td(.*)</td> .. and it finds one match.. Any Ideas?

With the given text, it should find just one match. That repeat operator is greedy; it will consume as much input as possible.

Use the non-greedy repeat operator instead: <td.*?/td>

Repeat: Use boost::sregex_iterator, perhaps?

Like this:

#include <string>
#include <vector>
#include <boost/regex.hpp>

std::vector<std::string> preg_match_all( const std::string& str, const boost::regex& regex )
{
    std::vector<std::string> matches ;
    boost::sregex_iterator begin( str.begin(), str.end(), regex ), end ;
    for( ; begin != end ; ++begin ) matches.push_back( begin->str() ) ;
    return matches ;
}
vijayan121 1,152 Posting Virtuoso

std::future<> and std::promise<> form a pair; together they provide an asynchronous inter-thread communication channel through which a value (or an exception) can be passed from one thread to another. std::promise<> is used by the function which wants to set a value, which can then be retrieved by another thread through the associated std::future<>.

std::packaged_task gives an easy to use wrapper over this: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2709.html

vijayan121 1,152 Posting Virtuoso

Use boost::sregex_iterator, perhaps?

http://www.boost.org/doc/libs/1_47_0/libs/regex/doc/html/boost_regex/ref/regex_iterator.html
(There is an example at the end of the page.)

vijayan121 1,152 Posting Virtuoso

You might first want to learn using a game programming library; for instance Allegro.
http://alleg.sourceforge.net/

And then learn to use a library for 3D graphics; for instance OpenGL.

Ideally, post your questions on boards that specializes in game programming; for instance GameDev
http://www.gamedev.net/index

vijayan121 1,152 Posting Virtuoso

Looks good to me.

Test it out to make sure it's working.

With variadic templates (using the earlier 'indexof'):

template< typename T > struct unique ;

template<> struct unique< typelist<> > { typedef typelist<> type ; } ;

template< typename FIRST, typename ...REST > struct unique< typelist<FIRST,REST...> >
{
    typedef typename unique< typelist<REST...> >::type urtype ;
    typedef typename std::conditional<
                        indexof< FIRST, typelist<REST...> >::value == -1,
                        typename push_front< FIRST, urtype >::type, urtype >::type type ;
};
vijayan121 1,152 Posting Virtuoso

> I found a solution from
> htp://stackoverflow.com/questions/6032089/position-of-a-type-in-a-variadic-template-parameter-pack

Needlessly over-elaborate, isn't it? This would suffice:

template< typename T, typename U, int N = 0 > struct indexof ;

template< typename T, int N > struct indexof< T, typelist<>, N > { enum { value = -1 } ; };

template< typename T, typename FIRST, typename ...REST, int N >
struct indexof< T, typelist<FIRST,REST...>, N >
            : std::conditional< std::is_same<T,FIRST>::value,
                                std::integral_constant<int,N>,
                                indexof< T, typelist<REST...>, N+1 > >::type {} ;

All that we need to do is recursively unpack the parameters one by one, keeping a count of how many have been unpacked so far, and ending the recursion if the type has been found (or the last parameter has been unpacked).

vijayan121 1,152 Posting Virtuoso
vijayan121 1,152 Posting Virtuoso

In most cases where typelists were used in old C++ (for example in tuple<>) , we do not need them any more - variadic templates would do the job better, and more efficiently too. As such, IMHO, libraries like Boost MPL would be rarely used; most of the verbose and somewhat intractable code in these libraries would eventually become thin wrappers over variadic parameter packs any way.

For example,

#include <tuple>

template< typename... TYPES > struct typelist {};

template< typename... TYPES > struct size {} ;
template< typename... TYPES > struct size< typelist<TYPES...> >
{ enum { value = sizeof...(TYPES) } ; } ;

template< typename T, typename... TYPES > struct push_front {} ;
template< typename T, typename... TYPES > struct push_front< T, typelist<TYPES...> >
{ typedef typelist< T, TYPES... > type ; } ;

template< typename... TYPES > struct pop_front {} ;
template< typename T, typename... TYPES > struct pop_front< typelist< T, TYPES... > >
{ typedef typelist<TYPES...> type ; } ;

template< unsigned int N, typename... TYPES > struct at {} ;
template< unsigned int N, typename... TYPES > struct at< N, typelist<TYPES...> >
{ typedef typename std::tuple_element< N, std::tuple<TYPES...> >::type type ; };

// .. etc
vijayan121 1,152 Posting Virtuoso

Test immediately after (not before) reading a line.

while( /*! myfile.eof()*/ getline(myfile,buffer) )
{
    /*getline(myfile,buffer);*/
    // ...
vijayan121 1,152 Posting Virtuoso

It appears that

a. There are going to be a very large number of DataStruct and Action objects (since the memory footprint of DataStruct is quite small).

b. DataStruct objects are added, but never removed (since the key is based on the size of the map).

In that case, using both a std::map<> and a std::set<> is unnecessary; a std::vector<> (with the key as the position in the sequence) and a std::set<> would suffice.

Something like this:

struct DataStruct
{
    double xData, yData ;
    std::vector<DataStruct>::size_type keyVal ;
    friend bool operator< ( const DataStruct& a, const DataStruct& b ) ;
};


class MapClass
{
    static std::vector<DataStruct> seq ;
    static std::set<DataStruct> set ;

    public:
        typedef std::vector<DataStruct>::size_type pos_t ;

        static pos_t AddDataStruct( const DataStruct& ds )
        {
            auto iterator = set.find( ds ) ;
            if( iterator != set.end() ) return iterator->keyVal ;
            else
            {
                pos_t pos = seq.size() ;
                seq.push_back(ds) ;
                seq.back().keyVal = pos ;
                set.insert( seq.back() ) ;
                return pos ;
            }
        }

        static const DataStruct& get( pos_t pos ) { return seq[pos] ; }
};

class Action
{
   MapClass::pos_t dataKey ;

   void do_stuff()
   {
      const DataStruct& ds = MapClass::get(dataKey) ;
      // ...
   }
};

with some additional trickery, more memory can be saved - make the value_type of the std::set<> a position in the sequence held by std::vector<>

If Datastruct objects also need to be removed as the program is running, this won't work; you could use boost::bimap<> in that case.
http://www.boost.org/doc/libs/1_47_0/libs/bimap/doc/html/index.html


       
Jsplinter commented: excellent! +3
vijayan121 1,152 Posting Virtuoso
WaltP commented: What is it with BOOST? Is it the cure-all of all problems? -4
vijayan121 1,152 Posting Virtuoso

> Where did you get that quote from?

A Microsoft Knowlege Base article, circa 2005

> any global or static object (non-volatile or volatile) will be flushed before a write on any volatile object.
> I am correct in thinking this?

Yes, the documentation is pretty clear - in Microsoft C++ volatile enforces acquire and release ordering for all volatile variables as well as all non-volatile variables with a static storage duration.

This is usually all that you require. However, there are situations where sequentially consistent ordering is necessary.
See: http://www.justsoftwaresolutions.co.uk/threading/memory_models_and_synchronization.html

vijayan121 1,152 Posting Virtuoso

> 1) I can`t do what I intended to do initially if I use pure C++99. It is impossible.

It is impossible to do it in portable code. And you have to accept that the code generated would be largely sub-optimal.

> I could do it with C++11. (.... I won`t bet anything that Microsoft will make it happen anytime soon,...)

Perhaps sooner than you expect; I suppose they are interested in the performance of the code generated by their compiler. Converting all the libraries to exploit C++11 will take some time though.

In the interim, Anthony Williams' JustThread library is (commercially) available. http://www.stdthread.co.uk/

> My current C++ (VS2009) allows me to do what I want, actually very easily, simply because:
> a) the keyword "volatile" meaning has been changed to a proprietary-sort of meaning

Not really 'changed'. They have just added on non-standard semantics to volatile. As has every mainstream compiler writer; each one in a different way.

The Microsoft compiler treats volatile in the manner that Java (1.5+) does - close to volatile T being treated as C++11 would treat volatile std::atomic<T>

> so that it now actually means it will flush *everything I wanted to be flushed*: any temporary variable ...

I'm not too sure of that unless it has changed after 2005. This is what the Microsoft documentation used to say:

Declaring a variable as volatile prevents the compiler from reordering references to that …

vijayan121 1,152 Posting Virtuoso

The volatile keyword was introduced at the time when C had no notion of concurrency other than time-slicing on a single processor. The notion of volatile in C++ is the same as what it is in C.

This article by Myers and Alexandrescu is instructive: http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf

This is what they have to say about volatile:

One might hope that the above fully volatile-qualified code would be guaranteed by the Standard to work correctly in a multithreaded environment, but it may fail for two reasons.
First, the Standard’s constraints on observable behavior are only for an abstract machine defined by the Standard, and that abstract machine has no notion of multiple threads of execution. As a result, though the Standard prevents compilers from reordering reads and writes to volatile data within a thread, it imposes no constraints at all on such reorderings across threads. At least that’s how most compiler implementers interpret things. As a result, in practice, many compilers may generate thread-unsafe code from the source above. If your multithreaded code works properly with volatile and doesn’t work without, then either your C++ implementation carefully implemented volatile to work with threads (less likely), or you simply got lucky (more likely). Either case, your code is not portable.

And their conclusion:

Finally, DCLP and its problems in C++ and C exemplify the inherent difficulty in writing thread-safe code in a language with no notion of threading (or any other form of concurrency). Multithreading considerations are pervasive, …

vijayan121 1,152 Posting Virtuoso

> I know how to send data in one struct and receive it on the other side, because there is constant size of structure.

Assuming that you are not converting to/from a common network data representation of the struct, this will work only if the POD struct involved has the same binary layout at both ends. This would be the case if both ends run on the same processor architecture, the same OS, are compiled with the same version of the same compiler and with the same compiler flags.

If for some absolutely compelling reason you decide to transmit POD structs in native binary form, and there is more than one such struct involved, one idea would be to wrap them in a discriminated union for send; and unwrap based on the type field after recv.

struct one { /*...*/ } ;
struct two { /*...*/ } ;
// ...

struct any
{
    enum struct_type { POD_ONE, POD_TWO /*, ... */ } ;

    any( const one& data ) : type(POD_ONE), pod_struct_one(data) {}
    any( const two& data ) : type(POD_TWO), pod_struct_two(data) {}
    // ...

    struct_type type ;
    union
    {
        one pod_struct_one ;
        two pod_struct_two ;
        // ...
    };
};
vijayan121 1,152 Posting Virtuoso
template< typename T > struct Node 
{
	/*struct*/ Node( T data )
	{
		this->data = data;
		previous = NULL;
		next = NULL;
	}
	T data;
	/*struct*/ Node *previous; // keyword struct is superfluous here
	/*struct*/ Node *next;
} /*NODE*/ ; 

template< typename T > class LinkedList
{
    protected:
	/*NODE*/ Node<T>* head;
	/*NODE*/ Node<T>* tail;
};
vijayan121 1,152 Posting Virtuoso
float num[3][3];

   std::cout << "Enter 9 numbers: ";

   for (int i = 0; i < 3; ++i )
      for( int j = 0 ; j < 3 ; ++j )
	 std::cin >> num[i][j];

   for (int i = 0; i < 3; ++i )
   {
      for( int j = 0 ; j < 3 ; ++j ) std::cout << num[i][j] << ' ' ;
      std::cout << '\n' ;
   }

See: http://www.cplusplus.com/doc/tutorial/arrays/

vijayan121 1,152 Posting Virtuoso

> how do I use the complete path like you said above, vijayan121?

See: http://en.wikipedia.org/wiki/Fully_qualified_file_name

On Windows, it would start with a drive letter; something like: std::fopen( "C:\\directory1\\directory2\\directory3\\input.txt", "r" ) ;

vijayan121 1,152 Posting Virtuoso

> Is there a specific location where I have to put my input.txt file?

Either place input.txt in the working directory of your program.

Or use a complete path like "C:\\some_dir\\etc\\etc\\input.txt" instead of a relative patch.

vijayan121 1,152 Posting Virtuoso

> I want to ether copy it to another location

#include <fstream>

int main()
{
    std::ifstream fin( "myfile.dat", ios_base::in|std::ios_base::binary ) ;
    std::ofstream fout( "myfile.cpy", ios_base::out|std::ios_base::binary ) ;
    fout << fin.rdbuf() ;
}

> or (more likely) send it through zlib's deflate.

Simplest if you use a C++ wrapper over zlib.
For example, using the Boost Iostream library http://www.boost.org/doc/libs/1_47_0/libs/iostreams/doc/index.html

#include <fstream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/zlib.hpp>

int main()
{
    std::ifstream fin( "myfile.dat", ios_base::in|std::ios_base::binary ) ;
    std::ofstream fout( "myfile.z", ios_base::out|std::ios_base::binary ) ;

    boost::iostreams::filtering_streambuf< boost::iostreams::output > stmbuf ;
    stmbuf.push( boost::iostreams::zlib_compressor() ) ;
    stmbuf.push(fout) ;

    boost::iostreams::copy( fin, stmbuf );
}
Ancient Dragon commented: good suggestions :) +17
vijayan121 1,152 Posting Virtuoso

> how about this?
> std::shared_ptr<A> pa = std::make_shared<A>() ;
> also more efficient than old semantic?

Yes, and for the same reason. Requires A to have an accessible default constructor, though.


> is it because the cause to copy the object of shared_ptr is very cheap?

shared_ptr<> hs move constructors and move assignment operators; it is extremely efficient when the source of the copy or assignment is a temporary or is no longer needed. In many other cases, the compiler would be able to apply copy-elision.

Even if a copy has to be made, it is quite cheap (copy a pointer and increment a reference count) if concurrency is not involved. If not, there would be a quality of implementation issue.