CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2009
    Posts
    9

    Talking Questions from a java developer (Auto-ptr , number representation , portability )

    hi , i am java developer and i just finished learning c++.
    Now here are my questions :

    1) why is auto-ptr useful .

    You use pointers if you have as goal to maintain variables when they are out of scope,
    why use auto-ptr that when they are out of scope they get destroyed. Doesn’t this
    destroy the whole concept of pointers ?
    This is what a normal variable does, right ? So why use auto-ptr instead of a normal variable.

    2) Choosing how to represent an number :
    -------------------------------------

    I want to represent numbers in such a way like java.
    a) integers store 32 bit no matter the compiler / architecture.
    Long store 64 bit no matter the compiler / architecture.

    b) numbers are stored with the same endianess e.g in java all are stored in big-endian.

    c) Why I want this ? because later if I use networking I want not to have problems like
    “What if in one machine these variable is stored as a 32 bit and in another as a 64
    Bit”.
    Am I missing something ? lets handle endianess problem by assuming that you convert all to big endian when you sent them to other computers. But how do you handle the fact that each number will have different number of bits. They will cause overflow and when sending a packet you wont be able to assume what the size is (maybe c++ has a function that tells). So how will you read it correctly from someone that you don’t know his architecture.

    d) I have heard rumors that float is not portable especial if you use them in networks ?
    why and how do you handle them ?

    e) I want also to do something extra. Assume that at start I represent a number as int, maybe later in the programming phase, I decide that int is not worth for me, and I want to change it to long without writing an extra lines of code. Do you use templates ? or typedefs ? what is the differences / advantages of each approach ? Will the program became slower because of the use of typedefs ? or they are always inlined by the compiler?

    4) can you in c++ choose an option that will allow you to inline all code (maybe a compiling flag ?), in java you could do this by putting final keyword in front of a class. Yes I know that putting code in .h will inline that method, but is there are easier way ? lets say that I want to inline the program only when I release it to the public, but when I run the program for my self, I want it to be normal (so its more readable and i have reduced compiling costs).

    5) anyone know what makes c++ a not portable language for other operating systems (linux)? I don’t care about compiler incompabilities assuming that all will use visual studio 2008 but i care about operating systems. Any links ? Why were the creators of c++ so shortsighted ? they could always make all functions portable by checking the operating system and responding in the appropreate way .

    6) shared_ptr: does it work exactly like the way java works?
    e.g you can use it safely without ever deleting it (of course, if you are not careful you may have memory leak e.g if the reference counter never goes to 0).

    thanks, i am looking forward to receiving your answers.

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Questions from a java developer (Auto-ptr , number representation , portability

    Quote Originally Posted by anonymous12345 View Post
    1) why is auto-ptr useful .
    They can be useful inside of classes that wrap manual memory allocation behind a 'friendly' interface, so that when the object goes out of scope, the memory is automatically deleted.

    Most of the time it's better to use normal variables or the STL container classes.

    I want to represent numbers in such a way like java.
    a) integers store 32 bit no matter the compiler / architecture.
    Long store 64 bit no matter the compiler / architecture.
    The usual way to do this is to include a header that contains typedefs for the required types, using preprocessor macros to choose the appropriate set

    Code:
    #ifdef WINDOWS
    typedef char Int8
    typedef int Int32;
    typedef long long Int64;
    #endif
    b) numbers are stored with the same endianess e.g in java all are stored in big-endian.
    The endianess is machine dependent. Extracting the bytes in a particular order is simple though, using shifting operators and masking.

    Code:
    int i = 0x12345678;
    
    // Get MSB
    char c = (i >> 24) & 0xFF;
    c) So how will you read it correctly from someone that you don’t know his architecture.
    You must talk with an agreed protocol.

    d) I have heard rumors that float is not portable especial if you use them in networks ?
    why and how do you handle them ?
    Probably as text.

    e) I want also to do something extra. Assume that at start I represent a number as int, maybe later in the programming phase, I decide that int is not worth for me, and I want to change it to long without writing an extra lines of code. Do you use templates ? or typedefs ? what is the differences / advantages of each approach ? Will the program became slower because of the use of typedefs ? or they are always inlined by the compiler?
    Templates and typedefs are both valid. It depends of what you are trying to do at the time.

    4) can you in c++ choose an option that will allow you to inline all code (maybe a compiling flag ?), in java you could do this by putting final keyword in front of a class. Yes I know that putting code in .h will inline that method, but is there are easier way ? lets say that I want to inline the program only when I release it to the public, but when I run the program for my self, I want it to be normal (so its more readable and i have reduced compiling costs).
    There is an 'inline' keyword that can be prepended to a function definition, but it is only a 'hint' to the compiler. The compiler optimisation phase will inline various functions if it thinks that it will produce more efficient code.

    5) anyone know what makes c++ a not portable language for other operating systems (linux)? I don’t care about compiler incompabilities assuming that all will use visual studio 2008 but i care about operating systems. Any links ? Why were the creators of c++ so shortsighted ? they could always make all functions portable by checking the operating system and responding in the appropreate way .
    There are standard libraries that allow C++ code to be compiled for a particular platform. The main reason for not having the functions check at runtime is efficiency. C++ is designed to be an efficient language, where the programmer 'only pays for what he uses'. Java gets around the problem by having a JVM created for each platform, that inserts itself between the application and OS/hardware. C++ does not have a standard ABI (application binary interface) that makes this possible (though there's no real technical reason why it couldn't)
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  3. #3
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Questions from a java developer (Auto-ptr , number representation , portability

    Just responding to some of these:
    Quote Originally Posted by anonymous12345 View Post
    hi , i am java developer and i just finished learning c++.
    Now here are my questions :

    1) why is auto-ptr useful .
    Because assigning an auto_ptr (e.g. when returning a value from a function) can be significantly cheaper than assigning an object.
    Quote Originally Posted by anonymous12345 View Post
    I want to represent numbers in such a way like java.
    a) integers store 32 bit no matter the compiler / architecture.
    Long store 64 bit no matter the compiler / architecture.

    b) numbers are stored with the same endianess e.g in java all are stored in big-endian.

    c) Why I want this ? because later if I use networking I want not to have problems like
    “What if in one machine these variable is stored as a 32 bit and in another as a 64
    Bit”.
    Am I missing something ?
    Let's think about this for a moment. Endianess is something that is hardwired into the processor. So a little-endian processor has no "native" way of dealing with big-endian numbers.

    What you want to do is force some processors to use a non-native representation of numbers all the time, turning a simple operation like adding two numbers into a sequence of several operations, just to avoid a little work when transferring numbers over the network. To me this sounds like going out every morning throughout the year with a snow shovel, just so that you are prepared for when the snow comes. (The analogy assumes, you are not living in Alaska.)

    Network programming is always done in a layer. It's that layer that should ensure parameters that are passed over the network are not platform dependent.
    e) I want also to do something extra. Assume that at start I represent a number as int, maybe later in the programming phase, I decide that int is not worth for me, and I want to change it to long without writing an extra lines of code. Do you use templates ? or typedefs ? what is the differences / advantages of each approach ? Will the program became slower because of the use of typedefs ? or they are always inlined by the compiler?
    Typedefs are exactly meant for that. Your question implies that you have not really understood templates yet.
    4) can you in c++ choose an option that will allow you to inline all code (maybe a compiling flag ?), in java you could do this by putting final keyword in front of a class. Yes I know that putting code in .h will inline that method, but is there are easier way ? lets say that I want to inline the program only when I release it to the public, but when I run the program for my self, I want it to be normal (so its more readable and i have reduced compiling costs).
    Leave optimization to the compiler. If performance is insufficient, then start profiling and possibly inlining functions. Don't go the reverse way.
    5) anyone know what makes c++ a not portable language for other operating systems (linux)?
    Who told you that? C++ is standardized since 1998 and a standard C++ program will run on many, many platforms (including PCs, Unix workstations, Mainframes, Embedded systems) for which C++ compilers are available.
    I don’t care about compiler incompabilities assuming that all will use visual studio 2008 but i care about operating systems. Any links ? Why were the creators of c++ so shortsighted ? they could always make all functions portable by checking the operating system and responding in the appropreate way .
    All functions provided as part of the C++ standard are portable and work on all platforms. What you are probably thinking of are the Windows libraries which are developed by Microsoft. So your question is really: Why was Microsoft so short-sighted and did not port their Windows library to run on Operating Systems other than Microsoft Windows. You figure out the answer to that question yourself.
    6) shared_ptr: does it work exactly like the way java works?
    No. With C++ shard_ptr, the object is deleted when the last reference to it was deleted (e.g. by leaving scope). In Java, objects are deleted at some undefined time after that (when the garbage collection kicks in) or mabe never. The difference might seem insignificant to you now, but it is one of the most valid criticisms of Java. Read about RAII to understand the point.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  4. #4
    Join Date
    Jul 2009
    Posts
    9

    Question Re: Questions from a java developer (Auto-ptr , number representation , portability

    1)

    Quote Originally Posted by JohnW@Wessex View Post
    The main reason for not having the functions check at runtime is efficiency. C++ is designed to be an efficient language, where the programmer 'only pays for what he uses'.
    You can achieve portability without checking it runtime :

    if you are not carefull, or maybe you are microsoft ??
    ------------------------------------------------------
    #ifdef WINDOWS
    run normally without problems
    #endif

    #ifdef LINUX
    System.crash();
    Throw blue or black screen of death.
    while(true) { doNothing() ;}
    System.shutdown();
    #endif

    I think that treuss is right.

    All functions provided as part of the C++ standard are portable and work on all platforms. What you are probably thinking of are the Windows libraries which are developed by Microsoft. So your question is really: Why was Microsoft so short-sighted and did not port their Windows library to run on Operating Systems other than Microsoft Windows. You figure out the answer to that question yourself.
    2)

    Because assigning an auto_ptr (e.g. when returning a value from a function) can be significantly cheaper than assigning an object.
    You mean that by assigning an object its like calling operator= ? then thats why it is slower.


    However the use you mentioned is like a rename operation e.g instead of calling a variable as a we call it b (since they point one the same place).

    However wouldnt the compiler be smart enought to understand that if the object state doesnt change (thats why we used a reference in the first place, because we knew it was safe and nothing bad will happen), there is no need to do a call to operator= ?

    e.g

    foo a ;
    foo b = a ;
    b.getName();

    wouldnt the following be the same as :

    foo a ;
    a.getName(); //getName is const


    3)
    assigning an auto_ptr (e.g. when returning a value from a function)
    if you return a auto_ptr from a function when will it be "out of scope".
    if "out of scope" happens before return then we will have serious trouble !!!!!!!!!!!!!!!
    what happens if the calling function manages to take the correct address, but delete is called before it is able to be used.

  5. #5
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Questions from a java developer (Auto-ptr , number representation , portability

    Quote Originally Posted by anonymous12345 View Post
    5) anyone know what makes c++ a not portable language for other operating systems (linux)? I don’t care about compiler incompabilities assuming that all will use visual studio 2008 but i care about operating systems. Any links ? Why were the creators of c++ so shortsighted ? they could always make all functions portable by checking the operating system and responding in the appropreate way .
    C++ will work on more platforms that java, I promise you that. There is a compiler for almost every platform, big and small. If you plan to use code on platforms other than windows, don't use visual studios, use GNU. GNU is more widely used, I believe and cross compile-able unless you use a platform specific library. To avoid using platform specific libraries, look at wxWidgets. They take all OS stuff and put it in a wrapper, the backend is very different between Linux / Mac / Windows / ... but the interface is the same.

  6. #6
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Questions from a java developer (Auto-ptr , number representation , portability

    Quote Originally Posted by anonymous12345 View Post
    2) However the use you mentioned is like a rename operation e.g instead of calling a variable as a we call it b (since they point one the same place).

    However wouldnt the compiler be smart enought to understand that if the object state doesnt change (thats why we used a reference in the first place, because we knew it was safe and nothing bad will happen), there is no need to do a call to operator= ?
    Illustrated: Assume you have a class, which is expensive to copy. You want to pass it around as reference whenever you can, but some time you have to switch ownership of the object. That's when usually a copy is created:
    Code:
    class BigClass {
            char array[10000];
    };
    
    class OtherClass {
    private:
            BigClass b;
    public:
            OtherClass(const BigClass& rhs) : b(rhs) {}
    };
    
    int main() {
            BigClass bc; // One object of class BigClass exists (bc)
            OtherClass oc(bc); // Another object of class BigClass exists (oc.b)
    } // Both objects are deleted
    BigClass is correctly passed to the OtherClass constructor by reference, however, as OtherClass wants to "own" its BigClass object, it creates a (expensive) copy via the copy constructor. In main, bc will continue to exist until it runs out of scope itself.

    Now the same example with auto_ptr:
    Code:
    #include <memory>
    
    class BigClass {
            char array[10000];
    };
    
    class OtherClass {
    private:
            std::auto_ptr<BigClass> b;
    public:
            OtherClass(std::auto_ptr<BigClass>& rhs) : b(rhs) {}
    };
    
    int main() {
            std::auto_ptr<BigClass> bc(new BigClass()); // One object of class BigClass exists
            OtherClass oc(bc); // Object is transferred to oc 
    } // oc is deleted leading to oc.b being deleted.
    Using auto_ptr, only one object of class BigClass is created throughout the whole program.
    Quote Originally Posted by anonymous12345 View Post
    3)

    if you return a auto_ptr from a function when will it be "out of scope".
    if "out of scope" happens before return then we will have serious trouble !!!!!!!!!!!!!!!
    what happens if the calling function manages to take the correct address, but delete is called before it is able to be used.
    If you return by value, a copy of the object is created in the calling functions scope and then the object in the called function scope is destroyed when the function returns. Note that compilers may (and often will) optimize this away by directly creating the object in the calling functions scope.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  7. #7
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Questions from a java developer (Auto-ptr , number representation , portability

    Quote Originally Posted by anonymous12345 View Post
    #ifdef WINDOWS
    run normally without problems
    #endif

    #ifdef LINUX
    System.crash();
    Throw blue or black screen of death.
    while(true) { doNothing() ;}
    System.shutdown();
    #endif
    I assumed he meant at 'runtime', not 'compile time'.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Questions from a java developer (Auto-ptr , number representation , portability

    A couple of things.....

    1) Use of #ifdefs for portability is acceptable, but should be restricted to the tightest scope possible and hidden behind a common interface. This is essentially what cross-platform libraries do---they provide a common interface, while providing multiple backends for various OSes.

    2) Use of auto_ptr is *not* recommended. Not for any of the reasons anonymous12345 was concerned about, but simply because it's not a very good smart pointer class----its semantics are in many ways non-obvious, so it's easy to screw up with it. It is recommended to use shared_ptr, scoped_ptr, or unique_ptr from Boost or C++0x as appropriate instead.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured