Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7904445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:04:24+00:00 2026-06-03T10:04:24+00:00

I have a function that generates a lattice of prices and a function that

  • 0

I have a function that generates a lattice of prices and a function that uses that lattice to price the associated option. The generate_lattice function dynamically allocates space for an array, and a pointer to that array is returned. This pointer is what is passed to price_from_tree. Here is the code:

#include <iostream>
#include <math.h>

using std::cout;
using std::endl;
using std::max;
using std::nothrow;

double* generate_lattice(double asset_price, double u, double d, int periods)
{
    int nodes = (periods * (periods + 1)) / 2; //Number of nodes = sum(1, # of periods)
    double * lattice; 

    lattice = new (nothrow) double[nodes]; 
    double up, down;
    int price_index = 0;
    for (int period = 0; period < periods + 1; period++) {
        for (int exp = 0; exp < period + 1; exp++) {
            up = pow(1 + u, exp);
            down = pow(1 - d, period - exp);
            lattice[price_index] = asset_price * up * down;
            price_index++;
        }
    }
    return lattice;
}

double price_from_tree(double* lattice, int periods, double strike, double risk_free, double period_len_years, bool euro = true, bool call = true)
{
    int num_prices = ((periods + 1) * (periods + 2)) / 2;
    double asset_max = lattice[num_prices - 1];
    double asset_min = lattice[num_prices - (periods + 1)];
    double asset_t0 = lattice[0];
    double u = pow(asset_max / asset_t0, pow(periods, -1));
    double d = pow(asset_min / asset_t0, pow(periods, -1));
    double p = (exp(risk_free * period_len_years) - d) / (u - d);
    double p_minus1 = 1 - p;
    int start_node;
    if (euro == true) { start_node = num_prices - periods - 1; }
    else { start_node = 0; }
    int sign;
    if (call == true) { sign = 1; }
    else { sign = -1; }
    for (int node = start_node; node < num_prices; node++) {
        lattice[node] = max(sign * (lattice[node] - strike), 0.0);
    }
    int price_index = num_prices - 1;
    double pv_mult = exp(-risk_free * period_len_years);
    double down_payoff, up_payoff, prev_payoff;
    for (int period = periods + 1; period > 0; period--) {
        for (int node = 0; node < period - 1; node++) {
            down_payoff = lattice[price_index - (node + 1)];
            up_payoff = lattice[price_index - node];
            prev_payoff = pv_mult * (p * up_payoff + p_minus1 * down_payoff);
            if (euro == false) {
                prev_payoff = max(lattice[price_index - (node + 1) - (period - 1)], prev_payoff);
            }
            lattice[price_index - (node + 1) - (period - 1)] = prev_payoff;
        }
        price_index -= period;
    }
    return lattice[0];
}

int main(int argc, char** argv)
{
    double* lattice = generate_lattice(100, 0.10, 0.10, 2);
    double option1 = price_from_tree(lattice, 2, 105, 0.05, 1, true, true);
    cout<<"option 1: "<<option1<<endl;
    double* lattice2 = generate_lattice(100, 0.10, 0.10, 2);
    return 0;
}

When I run the code, I get this output:

option 1: 8.28214
test: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) – 1) * 2])) – __builtin_offsetof (struct
malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size)

= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) – 1)) & ~((2 * (sizeof(size_t)))
– 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)’ failed.
Aborted

------------------
(program exited with code: 134)

All I can find on error 134 is descriptions along the lines of The job is killed with an abort signal. The code returns the correct value for price_from_tree in every case I’ve tried, but if I include multiple calls to generate_lattice it fails with the stated error. Is there a problem with my price_from_tree function that causes confusion within memory? Am I better off using vectors in this case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:04:26+00:00Added an answer on June 3, 2026 at 10:04 am

    You’re corrupting your heap pretty badly. If you run with valgrind (assuming you’re on a Linux machine) there are tons of errors.

    ==23357== Invalid write of size 8
    ==23357==    at 0x400B9E: generate_lattice(double, double, double, int) (so.cpp:21)
    ==23357==    by 0x400FC4: main (so.cpp:68)
    ==23357==  Address 0x595b058 is 0 bytes after a block of size 24 alloc'd
    ==23357==    at 0x4C27FFB: operator new[](unsigned long, std::nothrow_t const&) (vg_replace_malloc.c:325)
    ==23357==    by 0x400B1A: generate_lattice(double, double, double, int) (so.cpp:14)
    ==23357==    by 0x400FC4: main (so.cpp:68)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that generates a dropdown based on a query or array
I have a function that generates an array containing menu items: Item 1 SubItem
I have a function that generates a key of 4 characters that has to
I have a function that generates normal random number matrix having normal distribution using
I have a function that generates a MD5 hash in C# like this: MD5
So I have built a recursive function that generates a collection of Category objects.
I'm attempting to simplify my code with a function that generates an array of
I have a function that generates XML from a source. It works perfectly however,
I have a function that generates Fibonacci numbers: let rec fib n = match
I have a php function that generates HTML code like below function j_uf_SomeFunction($some_var) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.