not sure why program crashes

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 2
Reputation: Rehat is an unknown quantity at this point 
Solved Threads: 0
Rehat Rehat is offline Offline
Newbie Poster

not sure why program crashes

 
0
  #1
Jan 15th, 2008
hi, I am doing a lab assignment for my C++ class and it wants us to use serveral pointer indirections for each variable. The program runs but then crashs and it seems to occur during the second iteration of a for loop in the arrayGet function. I'm not sure why, something to do with "Access violation reading location".


  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. bool vectorGet(vector<string> ****);
  8. bool arrayGet(string ***, vector<string> ****);
  9.  
  10. int main()
  11. {
  12.  
  13. vector<string> ****p;
  14. string ***r;
  15. p = new vector<string> ***;
  16. r = new string **;
  17. vectorGet(p);
  18.  
  19.  
  20. for(int x = 0; x < (*(*(*p)))->size(); x++)
  21. {
  22. cout << (*(*(*(*p))))[x] <<endl;
  23. }
  24.  
  25. arrayGet(r, p);
  26.  
  27. for(int y = 0; y < (*(*(*p)))->size(); y++)
  28. {
  29. cout << *(*r)[y] <<endl;
  30. }
  31.  
  32. delete *(*(*p));
  33. delete *(*p);
  34. delete *p;
  35. delete p;
  36.  
  37. delete *(*r);
  38. delete *r;
  39. delete r;
  40.  
  41. }
  42.  
  43. bool vectorGet(vector<string> ****p)
  44. {
  45. *p = new vector<string> **;
  46. *(*p) = new vector<string> *;
  47. *(*(*p)) = new vector<string>;
  48.  
  49. string temp;
  50.  
  51. fstream document("TopicAin.txt");
  52. while(!document.eof() )
  53. {
  54. getline(document, temp);
  55. (*(*(*p)))->push_back(temp);
  56. }
  57.  
  58. return true;
  59. }
  60.  
  61. bool arrayGet(string ***r, vector<string> ****p)
  62. {
  63. *r = new string *;
  64. *(*r) = new string[(*(*(*p)))->size()];
  65.  
  66.  
  67. for(int y = 0; y < (*(*(*p)))->size(); y++)
  68. {
  69. *(*r)[y] = (*(*(*(*p))))[y]; //crash happens here
  70. }
  71.  
  72. return true;
  73. }

The program reads from a file and fills up the vector then copies the vector to a array and then prints out both variables.

Any help would be great, thanks
Last edited by Narue; Jan 15th, 2008 at 6:05 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2
Reputation: Rehat is an unknown quantity at this point 
Solved Threads: 0
Rehat Rehat is offline Offline
Newbie Poster

Re: not sure why program crashes

 
0
  #2
Jan 16th, 2008
I found my problem. LOL how do I delete this post since no one replied to my problem?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC