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

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    7

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

    Hi!
    I am new to C++ programming and was tinkering around with a program to learn about function pointers. I wrote the following code (using borland c++ compiler). The program compiles without error or warning but when I run the exe file it aborts..error message pops up on windows 7 saying execution of program has stopped working...
    Below is the code..can someone tell me what have I done wrong..I am unable to figure out ..
    Code:
    #include <iostream>
    
    float* Plus(float num1, float num2) {float* res; *res = num1 + num2; return res;}
    typedef float*(*pFunc)(float, float);
    
    pFunc GetPointer(char c)
    {
         if(c == '+') 
             return &Plus;
         else if(c == '-') 
             return &Plus;
         else
             return &Plus;
    
    }
    
    float Calc(float num1, float num2, float* (*pCalcMethod)(float, float))
    
    {
        float x=23;
        
        std::cout<< "result inside Calc method" << *pCalcMethod(num1, num2) << std::endl;
       
        return x;
    }
    
    int main()
    {
        float result = Calc(50, 25, &Plus);
        
        std::cout << "Result: " << result << std::endl;
        system("PAUSE");	
        return 0;
    }

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >>float* res; *res = num1 + num2;

    For starters, you declare a pointer without assigning it then dereference it and try to change its value

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    7
    The error is due to the line "std::cout<< "result inside Calc method" << *pCalcMethod(num1, num2) << std::endl;"

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    7
    Quote Originally Posted by Fordy View Post
    >>float* res; *res = num1 + num2;

    For starters, you declare a pointer without assigning it then dereference it and try to change its value
    Hey thank you Fordy.. I added float* res=new float; .. After doing this the code is working fine
    However I have one doubt..What is the best way to delete the memory assigned to the pointers in the code below
    Code:
    #include <iostream>
    
    float* Plus(float num1, float num2) {float* res=new float; *res = num1 + num2; return res;}
    typedef float*(*pFunc)(float, float);
    
    pFunc GetPointer(char c)
    {
         if(c == '+') 
             return &Plus;
         else if(c == '-') 
             return &Plus;
         else
             return &Plus;
    
    }
    
    float Calc(float num1, float num2, float* (*pCalcMethod)(float, float))
    
    {
        float x=23;
        
        std::cout<< "result inside Calc method" << *pCalcMethod(num1, num2) << std::endl;
       
        return x;
    }
    
    int main()
    {
        float result = Calc(50, 25, &Plus);
        
        std::cout << "Result: " << result << std::endl;
        system("PAUSE");	
        return 0;
    }
    Also in the below code if I use resultF pointer just like res in the earlier code(ie without initializing)..but it does not throw any error..why..??!!

    Code:
    #include <iostream>
    
    
    float Plus(float num1, float num2) {return num1 + num2;}
    
    typedef float(*pFunc)(float, float);
    
    pFunc GetPointer(char c)
    {
         if(c == '+') 
             return &Plus;
         //else
         //    return &Multiply;
         else if(c == '-') 
             return &Plus;
         else
             return &Plus;
    
    }
    
    
    float* Calc(float num1, float num2, char c)
    
    {
        float (*pFuncRes)(float, float)=GetPointer(c);
        
        float *resultF;
        *resultF=pFuncRes(num1, num2);
        
        return resultF;
        
    }
    
    int main()
    {
        float* result = Calc(50, 25, '+');
        
        std::cout << "Result: " << *result << std::endl;
        system("PAUSE");	
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Why do you use pointers to return fundamental types, when they do not need to be shared?. Just pass them by value like you do with the parameters. To free memory you have to call the 'delete' operator on the pointer.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Sourabh Dugar View Post
    Hey thank you Fordy.. I added float* res=new float; .. After doing this the code is working fine
    Except for a memory leak, yeah.
    Quote Originally Posted by Sourabh Dugar View Post
    However I have one doubt..What is the best way to delete the memory assigned to the pointers in the code below
    Store the pointer returned by the function (Plus() or, indirectly, via a function pointer of type pFunc) and, when the value pointed to will not be needed any more, release it (using operator delete).

    Better yet, change your Plus() function so it returns a float, rather than a pointer to float. That way you have no need to dynamically create a float, so no need to release it. Also change the typedef for pFunc accordingly, and code that uses them.

    And try not to use Java techniques in C++.....
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >>However I have one doubt..What is the best way to delete the memory assigned to the pointers in the code below

    For floats, dont bother with pointers as you are using them. Just pass them and return them as variables.

    >>float *resultF; *resultF=pFuncRes(num1, num2);

    Same mistake as before...pointer pointing to nothing, then being dereferenced

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    7
    Yeah Fordy I agree..I can return value.. but I am just a beginner..and was experimenting with pointers with this simple example
    However the second time the program doesn't abort (the resultF case).. I wonder how its working the second time around o_O

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    7
    Grumpy I am a total beginner so I have a doubt ..where is the best place to insert delete..?
    Main func passes &Plus to Calc.. therefore the Plus method returns this pointer to Calc again..Now i need to pass the value of this pointer(from Calc to Main where it will be displayed) using return function..so how do i delete this pointer..??!! Sorry if I sound naive..and thanks for the help..

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    7
    Hey one more thing Grumpy..which technique do u think is java based..?!

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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++.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    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..

  13. #13
    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".

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    The Function Pointer Tutorials - Index

    In case you've never seen it before.

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