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 8579313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:35:09+00:00 2026-06-11T20:35:09+00:00

I have this very simple c++ program. It’s been years that I did some

  • 0

I have this very simple c++ program. It’s been years that I did some C++ so I thought I might give it a spin one more time. But I am amazed by the output I am getting. It was supposed to be a simple program and it’s causing me some headaches already.

//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;
}

Input

./a.out 1 4 2 7 8

Output:-

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
  • 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-11T20:35:10+00:00Added an answer on June 11, 2026 at 8:35 pm

    Your areas array has automatic storage duration and goes out of scope once area returns; then dereferencing pointer is undefined behavior. Please use C++ idioms like std::string and std::vector instead of C pointers.

    Here is an improved (but still not optimal) version of your code:

    #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));
      }
    }
    

    Unfortunately compilers often cannot detect such errors. Use a tool like Valgrind to find them. For example, running your original code through Valgrind produces many errors for me:

    $ 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)
    […]
    

    while my version produces no errors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this very simple br2nl function that I use to take a string
I have this very simple example that I am using to learn structs in
I have this scala application which is very simple . All that it does
I have this very simple (yet big) code that doesn't work. The triangle doesn't
I have a very simple program that I am trying to improve performance. One
I have a very simple SDL program that uses only 1MB of memory with
I have no idea what is wrong! This is a very simple program and
I have a very simple program but this is giving me a seg fault.
So I have this very basic program that is trying to send an e-mail,
I have a client requesting a very simple program that stores basic information that

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.