首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++ 函数调用有关问题

2012-09-04 
C++ 函数调用问题遇到一个问题:建立一个头文件,其函数为指针类型,被主函数调用后,原地址所对应的数值变成-

C++ 函数调用问题
遇到一个问题:建立一个头文件,其函数为指针类型,被主函数调用后,原地址所对应的数值变成-1.45682e-144
代码如下:

头文件:
# include <math.h>
# include <vector>
# include <cstdlib>
# include <ctime>

using namespace std;

const double *discrete_normal(const int &no_signs)
{
  double sigma = 1.0; //风险;
double mue = 0.0; //期望;
vector<double>normal(1); //N(mue, sigma)随机数组;
normal[0] = 0.0;
double x = 0.0; //自变量x;
int calculate = 0; //数组元素总数;
int volume = 0; //某x对应的数量;
double f = 0; //因变量f(x);
//----------------------------------制作离散型标准正太分布样本空间;
for(int i = 0; i < 400; ++i)
{
x = (double)i/100.0;  
f = exp(-0.5*((x - mue)/sigma)*((x - mue)/sigma))/(sqrt(2*3.1415926)*sigma);
volume = floor(f*15000); 
calculate += volume;

for(int j = 0; j < volume; ++j)
{
normal.push_back(x);
}
}
//----------------------------------随机取no_signs个值制作成数组;
 

vector<double>sel_normal(1);
int l = calculate*((double)rand()/(double)RAND_MAX);
sel_normal[0] = normal[l];

srand(unsigned(time(0)));
for(int k = 1; k < no_signs; ++k)
{
l = calculate*((double)rand()/(double)RAND_MAX);
sel_normal.push_back(normal[l]);
  }

  const double *p = &sel_normal[0];
  return p;
}


主函数如下:
# include <math.h>
# include <vector>
# include <iostream>
# include "test_random_normal.h"

using namespace std;

void main()
{
const int no_signs = 2;
const double *receive;
receive = discrete_normal(no_signs); 

for(int i = 0; i < no_signs; ++i)
cout << *(receive + i) << " " << receive + i << endl;
}


输出结果如下:-1.45682e+144 03958B8
  -1.45682e+144 03958C0  



请问为什么会这样?我期望输出两个double 类型的两位小数。
大虾门帮帮忙吧~~

[解决办法]
vector<double>sel_normal(1);

函数执行完 局部变量就销毁了 可以定义成全局变量
[解决办法]
@1L(3L) +
1楼给出了你的程序出错的原因:你返回了一个局部变量的地址,当函数结束后,局部变量销毁,该变量中的内容是随机的(垃圾值),所以你运行得到的是错误的值(相应地址中的值是随机的垃圾值)。

当然如1楼所说,把 sel_normal 作为全局变量,应该可以解决你的问题(我简单的测试了下,可以),但是这种方法不太可取,楼主想得到的是 sel_normal 中的值,所以可以将 sel_normal 定义在 main 中,作为引用传递到你的 discrete_normal 中,下面给出代码(简单的修改了下)和输出示例:

C/C++ code
# include <cmath># include <vector># include <cstdlib># include <ctime># include <iostream>using namespace std;void discrete_normal(const int &no_signs, vector<double> &out_sel_normal){    double sigma = 1.0; //风险;    double mue = 0.0; //期望;    vector<double> normal(1); //N(mue, sigma)随机数组;    normal[0] = 0.0;    double x = 0.0; //自变量x;    int calculate = 0; //数组元素总数;    int volume = 0; //某x对应的数量;    double f = 0; //因变量f(x);    //-------------------------------制作离散型标准正太分布样本空间;    for(int i = 0; i < 400; ++i)    {        x = (double)i/100.0;           f = exp(-0.5*((x - mue)/sigma)*((x - mue)/sigma))/(sqrt(2*3.1415926)*sigma);        volume = (int)(floor(f*15000));          calculate += volume;        for(int j = 0; j < volume; ++j)        {            normal.push_back(x);         }    }    //-------------------------------随机取no_signs个值制作成数组;    int l = 0;    srand(unsigned(time(0)));    for(int k = 0; k < no_signs; ++k)    {        l = (int)(calculate*((double)rand()/(double)RAND_MAX));        out_sel_normal.push_back(normal[l]);    }}int main(){    const int no_signs = 2;    vector<double> sel_normal;    discrete_normal(no_signs,sel_normal);          // 控制输出小数点后两位    cout.precision(2);     // 浮点精度控制,2位精度    cout.setf(ios::fixed); // 精度从小数点后算起    for(int i = 0; i < no_signs; ++i)    {              cout << sel_normal[i] << " " << endl;    }    cin.get();    return 0;}// 输出示例:/*0.430.32*/ 


[解决办法]
问题有三:
一是楼主返回了已经销毁的空间的指针。
二是vector并不保证内部所存东西的连续性,所以必须用迭代器遍历。
三是随机访问vector的下标千万不要超过容器的容量。
另外,强烈建议楼主按照下列两本书的代码风格来写纯C++程序,拜托了。
《C++primer》第3版〈C++程序设计语言〉特别版。

热点排行