0

我有这个非常简单的 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
4

6 回答 6

10

您的阵列具有自动存储持续时间,并且一旦返回areas就会超出范围;area那么取消引用pointer是未定义的行为。请使用 C++ 习惯用法,例如std::stringandstd::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)
[…]

而我的版本没有产生错误。

于 2012-09-22T16:56:39.583 回答
3

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

不幸的是,您的编译器不够聪明,无法识别这一点,例如gcc我必须返回areas以获得警告:warning: address of local variable ‘areas’ returned

于 2012-09-22T17:00:05.327 回答
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);   
}
于 2012-09-22T18:03:21.417 回答
1

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

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

        // ...

        return pointer;
}

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

于 2012-09-22T17:01:06.417 回答
0

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

于 2012-09-22T17:01:49.270 回答
0

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

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

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

于 2012-09-22T16:57:04.753 回答