dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: buffer overflow

  1. #1
    Join Date
    Aug 2004
    Posts
    43,023

    buffer overflow

    [Originally posted by drop]

    I am using a function for calculate a factorial of a number but when I write a high number (ex.: 9999999) it produce an error of buffer overflow, how can I resolve this?
    The function is :

    Function factorial(ByVal factor As Long)
    factor = factor - 1
    If factor = 0 Then
    factorial = 1
    Exit Function
    End If
    factorial = factorial(factor) + (factor + 1)
    End Function

  2. #2
    Join Date
    Aug 2004
    Posts
    43,023

    Re:buffer overflow

    [Originally posted by Mike Lyons]

    You're probably exceeding the capacity of the Long data type.

    Try a Double and ignore the fractional portion.

    Mike

  3. #3
    Join Date
    Aug 2004
    Posts
    43,023

    Re:buffer overflow

    [Originally posted by Ed Ardzinski]

    Even using long data you can only calculate the factorial up to about n=171 or so...

    I wrote a code snippet that calculates it up to about n=680, but the return is a string representing the result in scientific notation.

    Check out

    http://www.freevbcode.com/ShowCode.Asp?ID=3530

  4. #4
    Join Date
    Aug 2004
    Posts
    43,023

    Re:buffer overflow

    [Originally posted by Mikekay2]

    You are calculating factorial incorrectly, factorial is found by multiplying not summing.
    e.g. 4! = 1*2*3*4 = 24
    not 1+1+2+3+4 = 11 which your code will give

    The other problem apart from the number been too large is that you are recursively calling the function many times, you will have x instances of the function on the stack if x is the number to get factorial of. As the stack is only able to hold a limited number of calls before you run out of memory this may cause you code to crash.


    Try using a non-recursive approach e.g:

    Dim I As Long
    Dim Result As Double
    ' if x is number to get factorial of

    Result=1
    For I = 2 To X
    Result=Result*I
    Next

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center