Thread: The execution of program stops on windows seven abruptly..whats the error..!!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    7
    Quote Originally Posted by grumpy View Post
    As an example, inside your Calc function, you might do this
    Code:
    float* Calc(float num1, float num2, char c)
    
    {
        pFunc pFuncRes =GetPointer(c);   // simpler way of getting the pointer
        
        float *resultF = pFuncRes(num1, num2);     // store the returned pointer
        float returnvalue = *resultF;    // store the value pointed to
        delete resultF;                        //  release the allocated memory
        return returnvalue;    
    }
    Anyway, the best thing you could do is change the return values of your functions from float * to float. Just because you CAN return a pointer, doesn't mean it's a good idea. As exhibited by the hoops jumped through in my example to make things work right.

    It is a moderately common Java technique to dynamically allocate a value, and later forget about it. This works in Java for a number of reasons (eg garbage collection, semantics of "what is a variable") but such techniques are really bad karma in C++.
    Thank you Grumpy for the help..I wrote this code mainly to get a feel of function pointers..so just asking doubts to clarify things
    If i use the code like u suggest..then return type of Calc should be made float not float*..right??
    Also just one more thing..if u have seen my earlier posts..why is it that when I didn't initialize res in Plus the program aborted..but in the second or third post (inside the Calc method) when I use the resultF without initialising it works..!!
    Thanks for the help again..

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Sourabh Dugar View Post
    Also just one more thing..if u have seen my earlier posts..why is it that when I didn't initialize res in Plus the program aborted..but in the second or third post (inside the Calc method) when I use the resultF without initialising it works..!!
    Thanks for the help again..
    It doesn't. Failure to crash your computer does not mean "it works".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program stops before EOF
    By doia in forum C Programming
    Replies: 2
    Last Post: 03-22-2010, 12:10 PM
  2. program terminates abruptly
    By roaan in forum C Programming
    Replies: 3
    Last Post: 08-28-2009, 03:53 PM
  3. Program stops executing
    By bargomer in forum C++ Programming
    Replies: 16
    Last Post: 04-26-2008, 12:05 PM
  4. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  5. Windows 2k Dos- Program Execution Time
    By maththeorylvr in forum C Programming
    Replies: 1
    Last Post: 03-17-2005, 10:03 PM