hi i was given an assignment to do
i did it but i'm always getting a segmentation fault
i found out where i'm getting the problem but i dont understand why i'm getting the problem

heres the code:
the extra cout's that dont make sense are just to tests to see where the problem is

#include <iostream>
#include <stdlib.h>
using namespace std;

#include "point.h"
                                         
class polygon{
  int n;
  point *points;
public:
  polygon(int n){
    point *points=new point[n];
    this->n=n;
  };
  void init();
  void pprint();
};

void polygon::init(){
  cout<<"n"<<n<<endl;   //
  for(int i=0;i<n;i++){
    cout<<"hehe"<<endl;  //
    points[i].set(rand()%101,rand()%101);   ////this is the place with the segmentation fault
    cout<<"blah"<<endl;  //
  }
};

void polygon::pprint(){
  cout<<"n"<<n<<endl;  //
  for(int i=0;i<n;i++){
    cout<<"bsde"<<endl;  //
    points[i].print();     /////since there is a problem above then this part is likely to have a segmentation fault as well
    cout<<"aeetr"<<endl;  //
  }
};

int main(int argc,char* argv[]){

  if(argc!=2){
    cout<<"wrong number of arguments"<<endl;
    return 1;
  }

  int a=atoi(argv[1]);

  polygon p(a);

  p.init();
  cout<<"pinit done"<<endl;  //

  p.pprint();
  cout<<"pprint done"<<endl;  //

  cout<<"end bye bye"<<endl;  //

  return 0;
};

there are no problems with the point.h point.cpp part but if you need it say so and i'll post that up later

thank you for your help in advance

Dani AI

Generated

correctly spotted the immediate cause: the constructor declares a local points variable that hides the member pointer. The member points therefore remains uninitialized and dereferencing it (as in the loop that calls set) causes the segmentation fault. The point class posted by looks fine, so the crash is due to the pointer/memory management in polygon.

Fixes and safer alternatives:

  • Remove the local declaration so the constructor assigns to the member (or use an initializer list to set n and the member pointer). Also provide a destructor that calls delete[] for the allocated array.
  • Because raw pointers bring copy/assignment and exception-safety pitfalls, either implement the rule of three/five (copy ctor, copy assignment, destructor) or disable copying.
  • Prefer RAII: use std::vector<point> or std::unique_ptr<point[]> to manage the array automatically and avoid manual new/delete.

Debugging and prevention tips:

  • Enable compiler warnings (-Wall -Wextra -Wshadow) to catch shadowing.
  • Build with debug symbols (-g) and use AddressSanitizer (-fsanitize=address) or Valgrind to pinpoint invalid reads/writes.
  • After replacing the shadowed assignment with a proper member initialization or moving to a container, the init/pprint loops should stop producing segmentation faults.

Credit: for identifying the shadowing; for prompting the point class check; for posting the reproducing code.

Recommended Answers

All 4 Replies

could you please post the point class code.

this is point.h code

#ifndef POINT_H
#define POINT_H

class point{
  int x,y;
 public:
  int getX() const;
  int getY() const;
  void set(int x,int y);
  void print() const;
};

#endif

the code for point.cpp

#include <iostream>
using namespace std;

#include "point.h"

int point::getX() const{return x;}
int point::getY() const{return y;}
void point::set(int x,int y){
  this->x=x;
  this->y=y;
}
void point::print() const{
  cout<<"("<<x<<", "<<y<<")\n";
}
class polygon{
  int n;
  point *points;
public:
  polygon(int n){
    point *points=new point[n];
    this->n=n;
  };

At the line 6 you initialize a local variable points . The member points remains uninitialized.

ahhhhhh didnt notice that >.<
thank you for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.