为什么我得到以下输出?

时间:2012-09-22 16:51:12

标签: c++

我有这个非常简单的c ++程序。多年来我做了一些C ++,所以我想我可能会再给它一次旋转。但我对我得到的输出感到惊讶。它应该是一个简单的程序,它已经让我有些头痛。

//2.cpp
//This program asks for the radius of the circle and
//prints the area of that circle 

#include <iostream>
#include <stdlib.h>

int* area(char* radius[], int size)
{
        int* pointer;
        int areas[size];
        pointer = areas;
        for(int i = 0; i < size; i++)
        {
                areas[i] = 3.1416*atoi(radius[i])*atoi(radius[i]);
        }
        return pointer;
}
void print(char* radius[], int* area1, int size)
{
        std::cout<<area1[2]<<std::endl; //This prints fine
        for(int i = 0; i < size; i++)
        {
                std::cout << area1[i]; //This doesn't
                std::cout << "Area for " << radius[i] << " is: " << area1[i] << std::endl;
        }
}

int main(int argc, char* argv[])
{
        if(argc > 1)
        {
                print(&argv[1],area(&argv[1],argc-1),argc-1);
        }
        else
        {
            //Please ignore this
        }
    return 0;
}

输入

./a.out 1 4 2 7 8

输出: -

12
134520896
Area for 1 is: 134520896
10
Area for 4 is: 10
-1217419175
Area for 2 is: -1217419175
-1217056780
Area for 7 is: -1217056780
-1217056780
Area for 8 is: -1217056780

6 个答案:

答案 0 :(得分:10)

您的areas数组具有自动存储持续时间,并且在area返回后超出范围;然后解除引用pointer是未定义的行为。请使用C ++惯用语,如std::stringstd::vector,而不是C指针。

以下是您的代码的改进版(但仍然不是最佳版):

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstddef>

std::vector<double> area(const std::vector<double>& radius)
{
  std::vector<double> areas(radius.size());
  for (std::size_t i = 0; i < radius.size(); i++) {
    areas[i] = 3.1416 * radius[i] * radius[i];
  }
  return areas;
}

void print(const std::vector<double>& radius, const std::vector<double>& area)
{
  for (std::size_t i = 0; i < radius.size(); i++) {
    std::cout << area[i]; //This doesn't
    std::cout << "Area for " << radius[i] << " is: " << area[i] << std::endl;
  }
}

int main(int argc, char* argv[])
{
  if (argc > 1) {
    std::vector<double> radii;
    radii.reserve(argc - 1);
    for (int i = 1; i < argc; ++i) {
      radii.push_back(std::atof(argv[i]));
    }
    print(radii, area(radii));
  }
}

不幸的是编译器经常无法检测到这样的错误。使用像Valgrind这样的工具来查找它们。例如,通过Valgrind运行原始代码会给我带来许多错误:

$ valgrind ./a.out 1 4 2 7 8
==18488== Memcheck, a memory error detector
==18488== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==18488== Using Valgrind-3.8.0 and LibVEX; rerun with -h for copyright info
==18488== Command: ./a.out 1 4 2 7 8
==18488== 
==18488== Conditional jump or move depends on uninitialised value(s)
==18488==    at 0x4EC5D16: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/libstdc++.so.6.0.17)
==18488==    by 0x4EC5F4C: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/libstdc++.so.6.0.17)
==18488==    by 0x4EC8E45: std::ostream& std::ostream::_M_insert<long>(long) (in /usr/lib/libstdc++.so.6.0.17)
==18488==    by 0x400A01: print(char**, int*, int) (in /tmp/a.out)
==18488==    by 0x400B0E: main (in /tmp/a.out)
==18488== 
==18488== Use of uninitialised value of size 8
==18488==    at 0x4EBB133: ??? (in /usr/lib/libstdc++.so.6.0.17)
==18488==    by 0x4EC5D37: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/libstdc++.so.6.0.17)
==18488==    by 0x4EC5F4C: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib/libstdc++.so.6.0.17)
==18488==    by 0x4EC8E45: std::ostream& std::ostream::_M_insert<long>(long) (in /usr/lib/libstdc++.so.6.0.17)
==18488==    by 0x400A01: print(char**, int*, int) (in /tmp/a.out)
==18488==    by 0x400B0E: main (in /tmp/a.out)
[…]

虽然我的版本没有错误。

答案 1 :(得分:3)

return pointer; - 您正在返回一个指向局部变量的指针,该局部变量在堆栈上分配,并在退出函数时超出范围。

不幸的是,您的编译器不够聪明,无法识别,例如使用gcc我必须返回areas才能收到警告:warning: address of local variable ‘areas’ returned

答案 2 :(得分:2)

如果您重新开始使用C ++,我认为您应该看一下新标准 C ++ 11 。从维基百科页面开始。这是我的代码版本:

#include <algorithm>
#include <string>
#include <vector>
#include <iostream>

std::vector<double> area(const std::vector<double> & radii ) {
  std::vector<double> areas(radii.size());
  std::transform (radii.begin(), radii.end(), areas.begin(),
          [](const double &r){ return 3.15159*r*r; } );
  return areas;
}

void printit(const std::vector<double> &r, const std::vector<double> &a) {
  for(size_t i = 0; i < r.size(); ++i) {
    std::cout << "Area for " << r.at(i) << " is: " << a.at(i) << std::endl;
  }
}

int main(int argc, char* argv[]) {
  std::vector<double> r(argc-1);
  std::transform (argv+1, argv+argc,r.begin(),
           [](const std::string &r){ return stod(r); } );
  auto areas=area(r);
  printit(r,areas);   
}

答案 3 :(得分:1)

您正在从此函数返回指向无效内存的指针。

int* area(char* radius[], int size)
{
        int* pointer;
        int areas[size];
        pointer = areas;

        // ...

        return pointer;
}

int areas[size]仅在此功能期间存在。您正在返回指向未定义内存的指针。

答案 4 :(得分:0)

areas是一个整数,但你把一个浮点数放进去......

计算给出可能与正确结果不同的结果的半径时!

areas[i] = 3.1416*atoi(radius[i])*atoi(radius[i]);

答案 5 :(得分:0)

Radius是一个指向字符串“1”的指针,但是你正在单步执行argc值,这是错误的。