Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: access violation at 0x321e4429: read of address 0x0000f2ed


This question is not answered. Helpful answers available: 1. Correct answers available: 1.


Permlink Replies: 5 - Last Post: May 17, 2017 3:10 PM Last Post By: Asger Joergensen Threads: [ Previous | Next ]
boualem Ait Mes...

Posts: 12
Registered: 4/19/17
access violation at 0x321e4429: read of address 0x0000f2ed  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 16, 2017 2:25 PM
i am manipulating a large amount of data here is an exemple of a loop i am using but i get message of access violation can anyone help me please :

this is a simple program but i am having trouble and i am using the new parallel loop

original loop

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 
tab.clear();
for(int i=0;i<10000;i++)
tab.push_back(i);
 
 
}


and this is the parallel one


void __fastcall TForm1::Button2Click(TObject *Sender)
{
tab.clear();
 
TParallel::For(NULL,0,10000,myF);
 
 
}
 
void __fastcall TForm1::myF(TObject *Sender , int j)
{
tab.push_back(j);
 
}
 
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: access violation at 0x321e4429: read of address 0x0000f2ed
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 16, 2017 3:43 PM   in response to: boualem Ait Mes... in response to: boualem Ait Mes...
boualem Ait Messaoudene wrote:
i am manipulating a large amount of data here is an exemple of a loop i am using but i get message of access violation can anyone help me please :

There are three issues with your code:

1. your multi-threaded code is pushing 1 more value into the tabs container than your single-threaded code does. Your single-threaded code does not push i=10000, but your multi-threaded code does, because the high index of TParallel.For() is inclusive.

2. you are clearing the tabs container, but you are not pre-allocating it afterwards. Thus, every push_back() call has the potential of reallocating the container's internal memory. That is fine in a single-threaded scenario, but not in a multi-threaded scenario. But even in the single-threaded scenario, you can avoid multiple reallocations by pre-allocating one time.

3. even if you solve the allocation error, you are not protecting the container from concurrent access, so you will have push_back() calls that will overlap each other, fighting each other for access to the last element in the container, thus potentially corrupting the data that is pushed.

With that said, try something more like this instead:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    tab.clear();
    tab.reserve(10000);
    for(int i = 0; i < 10000; i++)
        tab.push_back(i); 
}


void __fastcall TForm1::Button2Click(TObject *Sender)
{
    tab.clear();
    tab.resize(10000);
    TParallel::For(NULL,0,9999,myF);
}
 
void __fastcall TForm1::myF(TObject *Sender , int j)
{
    tab[j] = j;
}


--
Remy Lebeau (TeamB)
boualem Ait Mes...

Posts: 12
Registered: 4/19/17
Re: access violation at 0x321e4429: read of address 0x0000f2ed  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 17, 2017 1:58 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
boualem Ait Messaoudene wrote:
i am manipulating a large amount of data here is an exemple of a loop i am using but i get message of access violation can anyone help me please :

There are three issues with your code:

1. your multi-threaded code is pushing 1 more value into the tabs container than your single-threaded code does. Your single-threaded code does not push i=10000, but your multi-threaded code does, because the high index of TParallel.For() is inclusive.

2. you are clearing the tabs container, but you are not pre-allocating it afterwards. Thus, every push_back() call has the potential of reallocating the container's internal memory. That is fine in a single-threaded scenario, but not in a multi-threaded scenario. But even in the single-threaded scenario, you can avoid multiple reallocations by pre-allocating one time.

3. even if you solve the allocation error, you are not protecting the container from concurrent access, so you will have push_back() calls that will overlap each other, fighting each other for access to the last element in the container, thus potentially corrupting the data that is pushed.

With that said, try something more like this instead:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    tab.clear();
    tab.reserve(10000);
    for(int i = 0; i < 10000; i++)
        tab.push_back(i); 
}


void __fastcall TForm1::Button2Click(TObject *Sender)
{
    tab.clear();
    tab.resize(10000);
    TParallel::For(NULL,0,9999,myF);
}
 
void __fastcall TForm1::myF(TObject *Sender , int j)
{
    tab[j] = j;
}


--
Remy Lebeau (TeamB)

it worked with 1000 but not with a bigger value because i need to work with large amount of data like 1000000 i still have a access violation message

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: access violation at 0x321e4429: read of address 0x0000f2ed  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 17, 2017 10:35 AM   in response to: boualem Ait Mes... in response to: boualem Ait Mes...
boualem Ait Messaoudene wrote:
it worked with 1000 but not with a bigger value because i need to work with large amount of data like 1000000 i still have a access violation message

The code I gave you will handle 1000000 items just fine, so something else has to be wrong in code you have not shown. Where EXACTLY does the debugger say the exception is occurring? You ARE running the code in the debugger, aren't you? If not, you should be.

--
Remy Lebeau (TeamB)
boualem Ait Mes...

Posts: 12
Registered: 4/19/17
Re: access violation at 0x321e4429: read of address 0x0000f2ed  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 17, 2017 12:56 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
boualem Ait Messaoudene wrote:
it worked with 1000 but not with a bigger value because i need to work with large amount of data like 1000000 i still have a access violation message

The code I gave you will handle 1000000 items just fine, so something else has to be wrong in code you have not shown. Where EXACTLY does the debugger say the exception is occurring? You ARE running the code in the debugger, aren't you? If not, you should be.

--
Remy Lebeau (TeamB)

yes it' true Mr Lebeau as i said i have to deal with large amount of data so i tried 10000000000 and it is not working ?
and yes i am using the debugger
the exception is std::bad_alloc
Asger Joergensen

Posts: 370
Registered: 11/18/08
Re: access violation at 0x321e4429: read of address 0x0000f2ed  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 17, 2017 3:10 PM   in response to: boualem Ait Mes... in response to: boualem Ait Mes...
Hi boualem

boualem Ait Messaoudene wrote:

yes it' true Mr Lebeau as i said i have to deal with large amount of data so i tried
10000000000 and it is not working ?
and yes i am using the debugger
the exception is std::bad_alloc

10000000000 is a 34bit number, so an int is not enough, you might need to
check the max_size()

info: std::vector::reserve Exceptions
std::length_error if new_cap > max_size().
any exception thrown by Allocator::allocate() (typically std::bad_alloc)

Best regards
Asger
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02