Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am compiling one sample code. But I am getting the answer when I am compiling that code for input = 5, If I change input from 5 to 6., then once answer came, but next time for same input, throwing a message "return value 3221226356". If again I change the input from 6 to 8, then again that message is coming.


C++
#include <iostream>
#include <string>
#include <cmath>

void loop1(float *a, int &k )                                \
{
    int i = k / 2 ;                                          \
    for (int x = 0 ; x < i ; x++ )                           \
    {
        a[x] = x ;                                           \
        std::cout<<"a = "<< a[x] <<'\n' ;                    \
    }
}

void loop2(float *a, int &i, int &j )                        \
{
    for (int x = i ; x < j ; x++ )                           \
    {
        a[x] = a[x-i] ;                                      \
        std::cout<<"a ["<<x<<"]= "<< a[x] <<'\n' ;           \
    }
}
void loop3(float *a, int &i)
{
    for (int x = 0 ; x < i ; x++ )
    {
        a[x] = x * x * x ;
        a[x] += a[x];

    }
}

int main()                                                   \
{
    int nx ;                                                 \
    float* xpos = NULL;                                      \
    xpos = new float[nx];                                    \

    std::cout<<"enter the value for nx " ;                   \
    std::cin>>nx ;                                           \
    int a = nx/2 ;                                           \
    std::cout<<"a (= nx/2 )= "<< a <<'\n' ;                  \


    loop1(xpos, nx );                                        \
    loop2(xpos, a, nx);                                      \

    for (int x = 0 ; x < nx ; x++ )                          \
    {
        std::cout<<"xpos = "<< xpos[x] <<'\n' ;              \
    }
/*  loop3(xpos, nx );                                        \  */

    for (int x = 0 ; x < nx ; x++ )                          \
    {
        std::cout<<"new  xpos = "<< xpos[x] <<'\n' ;         \
    }
    return 0 ;
}


Result for Input = 5
C++
enter the value for nx 5
a (= nx/2 )= 2
a = 0
a = 1
a [2]= 0
a [3]= 1
a [4]= 0
xpos = 0
xpos = 1
xpos = 0
xpos = 1
xpos = 0
new  xpos = 0
new  xpos = 1
new  xpos = 0
new  xpos = 1
new  xpos = 0

--------------------------------
Process exited after 4.442 seconds with return value 0
Press any key to continue . . .



Result for Input = 6

C++
enter the value for nx 6
a (= nx/2 )= 3
a = 0
a = 1
a = 2
a [3]= 0
a [4]= 1
a [5]= 2
xpos = 0
xpos = 1
xpos = 2
xpos = 0
xpos = 1
xpos = 2
new  xpos = 0
new  xpos = 1
new  xpos = 2
new  xpos = 0
new  xpos = 1
new  xpos = 2

--------------------------------
Process exited after 3.427 seconds with return value 0
Press any key to continue . . .



Result for Input = 8

<pre lang="c++">enter the value for nx 8
a (= nx/2 )= 4
a = 0
a = 1
a = 2
a = 3
a [4]= 0
a [5]= 1
a [6]=
--------------------------------
Process exited after 7.167 seconds with return value 3221226356
Press any key to continue . . .



Have you got any idea about what I did wrong ?
Is there any specific name of such error?
But, I can't find why that failed.

What I have tried:

I tried to vary the Input from one to other. But giving the message.
Posted
Updated 13-Nov-18 22:53pm
Comments
Richard MacCutchan 14-Nov-18 3:06am    
You need to use your debugger to see where the routine is exiting. You also need to remove all those unnecessary backslashes in your source.
KarstenK 14-Nov-18 3:29am    
nx needs some value!!! What about using the debugger?

C++
int nx ;                                                 \
float* xpos = NULL;                                      \
xpos = new float[nx];                                    \

You have not set a value for nx, so your array may be any size between -2,147,483,647 and +2,147,483,647.

And remove those backslashes everywhere, what do you think they are for?
 
Share this answer
 
Look at your code:
int main()                                                   \
{
    int nx ;                                                 \
    float* xpos = NULL;                                      \
    xpos = new float[nx];   
How many floats are allocated in the array? Since you don't set a value for nx it will (if you are lucky, it depends on the compiler and the settings you gave it) default to zero.

And since it's a local variable, the array is on the stack. When you use it, you access parts of the stack that aren't a part of your array at all, and you corrupt them when you write to the array. At that point, anything can happen!

Allocate some suitable size to the array, or allocate the array once you know how many elements it needs to have!
 
Share this answer
 
To fix the obvious in your code
C++
#include <iostream>

void loop1(float a[], size_t k )
{
    size_t limit = k / 2 ;
    for (size_t x = 0 ; x < limit ; x++ )
    {
        a[x] = x ;
        std::cout << "a = " << a[x] << '\n' ;
    }
}

void loop2(float a[], size_t i, size_t j )
{
    for (size_t x = i ; x < j ; x++ )
    {
        a[x] = a[x-i] ;
        std::cout<<"a ["<<x<<"]= "<< a[x] <<'\n' ;
    }
}
void loop3(float a[], size_t i)
{
    for (size_t x = 0 ; x < i ; x++ )
    {
        a[x] = x * x * x ;
        a[x] += a[x];
    }
}

int main()
{
    int nx ;
    float* xpos = NULL;

    std::cout<<"enter the value for nx " ;
    std::cin>>nx ;

    xpos = new float[nx];

    int a = nx/2 ;
    std::cout<<"a (= nx/2 )= "<< a <<'\n' ;

    loop1(xpos, nx );
    loop2(xpos, a, nx);

    for (int x = 0 ; x < nx ; x++ )
    {
        std::cout<<"xpos = "<< xpos[x] <<'\n' ;
    }
/*  loop3(xpos, nx );                                         */

    for (int x = 0 ; x < nx ; x++ )
    {
        std::cout<<"new  xpos = "<< xpos[x] <<'\n' ;
    }
    delete [] xpos;
}


You should give meaningful names to your functions.
You could use std::vector instead of dynamically allocate yourself the array.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900