Problem with pushback function - "An Access Violation.. "

H

hadad.yaniv

Hello,

i am new to c++,
i hav a vector of typed object:
vector<Man*> People;

When i do a second pushback, even for the same object the program
crash say:
"An Access Violation (Segmentation fault) raised in your program"


what could be the problem??

many thanks.
 
J

John Harrison

Hello,

i am new to c++,
i hav a vector of typed object:
vector<Man*> People;

When i do a second pushback, even for the same object the program
crash say:
"An Access Violation (Segmentation fault) raised in your program"


what could be the problem??

Almost anything. Please post the code that causes the problem,
preferably a whole program.

Debugging C++ isn't easy, if it was possible to fix a bug from a one
line description and an error message, then C++ programmers would get
paid a whole lot less.

Only the code will do, post it.

john
 
H

hadad.yaniv

Almost anything. Please post the code that causes the problem,
preferably a whole program.

Debugging C++ isn't easy, if it was possible to fix a bug from a one
line description and an error message, then C++ programmers would get
paid a whole lot less.

Only the code will do, post it.

john

Here is the problematic function:

void Elections::read_votes()
{
// handeling the first ballot
Ballot * oneballot;
oneballot = read_and_normalize_inputballot();
if (oneballot!=NULL)
{
ballots.push_back(oneballot);
}
//handeling the rest of the ballots
string command;
while (cin>>command)
{
if (command=="InputBallot")
{
oneballot = read_and_normalize_inputballot();
if (oneballot!=NULL)
{
ballots.push_back(oneballot); - crash here!!!!!,
ballots is not null neither.
}
}
else
{
return ;
}
}
}
 
T

tragomaskhalos

On Jul 24, 11:07 pm, John Harrison <[email protected]>
wrote:

[Incomplete code snipped]

There's not enough code here to diagnose the problem.
The best thing to do is to pare the code down to a minimum single
*compilable* body that illustrates the problem, with an int main() and
all, and post that. This has two advantages:
1. People can copy-and-paste your code direcly and compile it
themselves;
2. In paring the code down, you will very likely see the problem
yourself.
HTH.
 
L

Lionel B

Here is the problematic function:

void Elections::read_votes()
{
// handeling the first ballot
Ballot * oneballot;

How is 'Ballot' defined?
oneballot = read_and_normalize_inputballot(); if (oneballot!=NULL)

What does 'read_and_normalize_inputballot()' do?
{
ballots.push_back(oneballot);

How is 'ballots' defined?
}
//handeling the rest of the ballots
string command;
while (cin>>command)
{
if (command=="InputBallot")
{
oneballot = read_and_normalize_inputballot(); if
(oneballot!=NULL)
{
ballots.push_back(oneballot); - crash here!!!!!,
ballots is not null neither.
}
}
else
{
return ;
}
}
}

*Please* try and post an *entire, compilable* program that demonstrates
your problem (see the FAQ):

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

My suspicion would be that the problem is being caused by a bug in some
code that you haven't posted, but who knows?
 
I

Ismo Salonen

Lionel said:
How is 'Ballot' defined?


What does 'read_and_normalize_inputballot()' do?
--snip--

My quess is that read_and_normalize_inputballot() returns address of
local (stack) variable. Or it uses dynamic memory in which case the
allocation size was too small.
Or something else is corrupting heap.
Those are the usual beginner mistakes.

ismo
 
J

Jim Langston

Here is the problematic function:

void Elections::read_votes()
{
// handeling the first ballot
Ballot * oneballot;
oneballot = read_and_normalize_inputballot();
if (oneballot!=NULL)
{
ballots.push_back(oneballot);
}
//handeling the rest of the ballots
string command;
while (cin>>command)
{
if (command=="InputBallot")
{
oneballot = read_and_normalize_inputballot();
if (oneballot!=NULL)
{
ballots.push_back(oneballot); - crash here!!!!!,
ballots is not null neither.
}
}
else
{
return ;
}
}
}

This code, in and of itself, should not produce a problem, so it must be
somewhere we can't see. Such as in read_and_normalize_inputballot();

Please post complete compilable code that reproduces the problem. In this
case include read_and_normalize_inputballot();, definition of ballots, and a
main that executes and causes the error.

I suspect that just by you doing this you'll find the problem. Or when you
do this, you'll find it's no longer crashing, and find the error is
elsewhere.

Access violations gernally mean that the computer is trying to read memory
it doesn't "own", that something is pointing to somewhere it shouldn't. A
lot of things can cause this, not only a pointer just pointing to somewhere
it shouldn't, but heap/stack corruption, array overflows, etc... Usually
reducing the code to the smallest code that reproduces the problem shows the
error, as when you take something thought to be unrelated out it suddently
starts to work.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top