Segmentation fault

A

Alex Vinokur

Baloff said:
Hello

I never got this error before, I would appreciate help. thank you
$ make
g++ -c -o main.o main.cpp
g++ -g -Wall -o proj1 main.o
$ ./proj1
Segmentation fault

******************************code******************************
#include <iostream>
using namespace std;

struct person
{
int year;
};

void set(person* ag, int yr){
ag->year = yr;
}

void get(person* ag){
cout << "my age is "
<< ag->year << " years"
<< endl;
}

int main(){ --------------------
person* jack;
// Here jack is not instance but pointer
set(jack, 22);
get(jack);
--------------------
person jack;
set(&jack, 22);
get(&jack);
}

******************************makefile******************************
OBJS = main.o
CFLAGS = -g -Wall

CFLAGS= -g -Wall
proj1 : $(OBJS)
$(CXX) $(CFLAGS) -o $@ $(OBJS)

clean :
rm -f $(OBJS) proj1

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
 
B

Baloff

Hello

I never got this error before, I would appreciate help. thank you
$ make
g++ -c -o main.o main.cpp
g++ -g -Wall -o proj1 main.o
$ ./proj1
Segmentation fault

******************************code******************************
#include <iostream>
using namespace std;

struct person
{
int year;
};

void set(person* ag, int yr){
ag->year = yr;
}

void get(person* ag){
cout << "my age is "
<< ag->year << " years"
<< endl;
}

int main(){
person* jack;
set(jack, 22);
get(jack);
}

******************************makefile******************************
OBJS = main.o
CFLAGS = -g -Wall

CFLAGS= -g -Wall
proj1 : $(OBJS)
$(CXX) $(CFLAGS) -o $@ $(OBJS)

clean :
rm -f $(OBJS) proj1
 
B

Baloff

pointer of type person, right? so why 'set' does not take it since it
is a pointer of type person?
Or
int main(){
person* jack = new jack();
did you mean new person();?
 
A

Alex Vinokur

Baloff said:
pointer of type person, right? // Right.
so why 'set' does not take it since it
is a pointer of type person?
// Because 'jack' is not initialized, no 'person' instance is created.
// jack points to no 'person' instance.

// Must be:
person* jack = new person ();
// Now 'jack' points to some 'person' instance.

[snip]

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
 
O

Old Wolf

Alex said:
// Must be:
person* jack = new person ();

Those brackets are a waste of space (have you been spending
too much time in Java? :)

person *jack = new person;

or, to keep to good RAII principles,

std::auto_ptr<person> jack (new person);
 
E

Earl Purple

Old said:
Those brackets are a waste of space (have you been spending
too much time in Java? :)

person *jack = new person;

or, to keep to good RAII principles,

std::auto_ptr<person> jack (new person);

Given this all seems to be written in C, why not use malloc?
And post this to comp.lang.C

If you are going to use C++ then use class member functions (including
the constructor) to get/set. (Unless this is one of those occasions
when you must be able to port to C but are implementing in C++).
 
O

Old Wolf

Earl said:
Given this all seems to be written in C, why not use malloc?

The first line of the OP's code was:
#include said:
And post this to comp.lang.C

He would have gotten flamed out of comp.lang.c for posting that.
If you are going to use C++ then use class member functions (including
the constructor) to get/set.

Why? There's nothing wrong with any of the code in this thread.
Classes are allowed to have public member variables.
 
K

Karl Heinz Buchegger

Baloff said:
pointer of type person, right? so why 'set' does not take it since it
is a pointer of type person?

set() does take it.
The problem is different.
jack is just a pointer. It may or may not to a person object.
At the moment it does not point to any person object. So set()
tries to alter some values in an object that does not exist.
Just because you have a pointer doesn't mean you have an object
also.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top