473,322 Members | 1,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Segmentation fault.

hi, im doing a stage 2 compsys paper in uni.
got stuck with the project
could some1 have a look at my code and tell me why there's segmentation fault, plz?

this project is simplified to find the white rectangle on the red wall of a given image.

my main:

Expand|Select|Wrap|Line Numbers
  1.     BMP bmpIn;
  2.         bmpIn.load("test3.jpg");
  3.     bmpIn.moveCursorTo(0,30);
  4.  
  5.     firstvertex = bmpIn.findFirstWhite();
  6.     one.x = firstvertex.x;
  7.     one.y = firstvertex.y;
  8.     cout<<one.x<<" "<<one.y<<endl;
  9.  
  10.     jb = bmpIn.findVertices2(one.x, one.y);
  11.     lj = bmpIn.findVertices3(one.x, one.y);
  12.  
  13.     cout<<jb.x<<"."<<jb.y<<endl;
  14.     cout<<lj.x<<"."<<lj.y<<endl;
where the cursor keeps track where we are. and all the declare stuff are not here

cpp file:

Expand|Select|Wrap|Line Numbers
  1. OJ BMP::findVertices2(int a , int b)
  2. {
  3.     int x, y;
  4.     int c = 1;
  5.     OJ jb;
  6.     vector<int>sox;
  7.     vector<int>soy;
  8.     sox[0] = getCursorX();
  9.     soy[0] = getCursorY();
  10. cout<<a<<"   "<<b<<endl;
  11. cout<<"hi"<<endl;
  12.     for (x = a; x < a + 30; x++){
  13.         for(y = b; y< 85;y++){
  14.         if (testwhite(x,y)==1)
  15.         {
  16.         sox[c] = x;
  17.         soy[c] = y;
  18.         c++;
  19.         y = b;
  20.         x++;
  21. cout<<"fk"<<endl;
  22.         }
  23.         if (sox[c] - sox[c -1] > 10 )
  24.         {
  25.         jb.x = sox[c-1];
  26.         jb.y = soy[c-1];
  27.         return jb;
  28. cout<<"yuou"<<endl;
  29.         }
  30.         if (y ==  84 )
  31.         {
  32.         jb.x = sox[c-1];
  33.         jb.y = soy[c-1];
  34. cout<<"lol"<<endl;
  35.         }
  36.     }}
  37. }
  38.  
  39. OJ BMP::findVertices3(int a , int b)
  40. {
  41.     int x, y;
  42.     int c = 1;
  43.     OJ lj;
  44.     vector<int>hix;
  45.     vector<int>hiy;
  46.     hix[0] = getCursorX();
  47.     hiy[0] = getCursorY();
  48.     for (x = a; x > a + 30; x--){
  49.         for(y = b; y < 85;y++){
  50.         if (testwhite(x,y)==1)
  51.         {
  52.         hix[c] = x;
  53.         hiy[c] = y;
  54.         c++;
  55.         y = b;
  56.         x++;
  57.         }
  58.         if (hix[c] - hix[c -1] > 10 )
  59.         {
  60.         lj.x = hix[c-1];
  61.         lj.y = hiy[c-1];
  62.         return lj;
  63.         }
  64.         if (y ==  84 )
  65.         {
  66.         lj.x = hix[c-1];
  67.         lj.y = hiy[c-1];
  68.         }
  69.     }}
  70.  
  71. }
where OJ is a struct i created in the header file with x and y as its member
testwhite() is basically the red green and blue values for defining white.
Oct 20 '07 #1
1 1246
Banfa
9,065 Expert Mod 8TB
I am going to explain the errors in 1 function but you are making them generally through the code posted.

Expand|Select|Wrap|Line Numbers
  1. OJ BMP::findVertices2(int a , int b)
  2. {
  3.     int x, y;
  4.     int c = 1;
  5.     OJ jb;
  6.     vector<int>sox;
  7.     vector<int>soy;
  8.     sox[0] = getCursorX();
  9.     soy[0] = getCursorY();
  10. cout<<a<<"   "<<b<<endl;
  11. cout<<"hi"<<endl;
  12.     for (x = a; x < a + 30; x++){
  13.         for(y = b; y< 85;y++){
  14.         if (testwhite(x,y)==1)
  15.         {
  16.         sox[c] = x;
  17.         soy[c] = y;
  18.         c++;
  19.         y = b;
  20.         x++;
  21. cout<<"fk"<<endl;
  22.         }
  23.         if (sox[c] - sox[c -1] > 10 )
  24.         {
  25.         jb.x = sox[c-1];
  26.         jb.y = soy[c-1];
  27.         return jb;
  28. cout<<"yuou"<<endl;
  29.         }
  30.         if (y ==  84 )
  31.         {
  32.         jb.x = sox[c-1];
  33.         jb.y = soy[c-1];
  34. cout<<"lol"<<endl;
  35.         }
  36.     }}
  37. }
  38. }
You declare 2 vectors sox and soy, you immediately access them using the index [0] and later access them using the the index [c] and [c-1]. Noting that c is initialised to 1 and increments this is not in itself a problem.

The problem is that at no time do you ever allocate any storage to the vectors sox and soy so all these accesses are to invalid addresses and that is causing you segmentation fault. If you had used the member function <vector>.at(...) instead of [] you would be getting out of range exceptions, the at member range checks it's input but [] is not required to do this.

There are 2 ways to allocate memory to a vector:
  1. allocate it as you put entries in the vector using the method push_back. This function increases the array size by 1, allocating extra data if required and then inserts the value it is given in the new (final) position. This can be very inefficient if adding a lot of elements to the array because each reallocation of data is likely to require copying the current entire array from one location to another. This can be mitigated by calling reserve(...) first which allocates memory ready for an increase in size.
  2. allocate it all in one go using the method resize. This member function alters the size of the vector to any size(memory permitting) and allocates data for the vector. You can then access it using [] or .at
Oct 20 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.