CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Feb 2016
    Posts
    3

    Question Access violations and lost PBD files

    I have tried writing a code to print Pascals triangle of n size. In it I have used pointers and bidimensional arrays which I am not very skilled with.When I run it I get two error messages, but never at the same run. It complains about access violations (I put the error message next to the code that it was aimed at) which probably depends on that I've used either arrays or pointers in the wrong way. The error messages about lost PBD files, or files that can not be opened, I don't actually know what mean.
    I'd be greatful for any help at all. You could just point me in the right direction. (I don't know if the code does what I want it to but I can't get it to run si I wouldn't know if it doesn't.

    Code:
    #include <iostream>
    #include <new>
    using namespace std;
    
    int setUp(){
    int input;
    
    	cout << "This program will let you create a Pascals triangle of prefered size." << endl;
        cout << "Enter the amount of rows you wish the triangle to have: ";
        cin >> input;
        cout << endl << endl;
    
    return input;
    }
    
    int triangle(int wid){
    int i, j, k;
    int **p, row = wid, col = wid;
    
    	p = new int* [row]; //create rows of triangle
    	for (int row = 0; row < wid; row++)
    		p[row] = new int[col]; //create columns of triangle
    
    	for (i = 0; i < wid; i++)
    		p[0][i] = 1;
    
    	for (i = 0; i < wid; i++)
    		p[i][0] = 1; //fix
    		cout << p[i][0] << endl; //fix
    		/*
    		Error message: Unhandled exception at 0x00da171b in pascalsT.exe: 0xC0000005: Access violation reading location 0xfdfdfdfd.
    		It also complains about not being able to find or open a bunch of PBD files half the time as well. Have I saved the file in the wrong way somehow?
    		*/
    
    		/* 
    		This could come in handy:
    		https://msdn.microsoft.com/en-us/library/6decc55h.aspx on fixing an access violation
    		and learn how to ****ing use and print ****ing arrays man. Both one and multidimensional
    		http://www.cplusplus.com/doc/tutorial/pointers/ (also has a chapter on arrays)
    		*/
    	
    	for (i = 1; i < wid; i++){
    		for (j = 1, k = i; i < wid; i++){
    			p[j][k] = p[j][k-1] + p[j][k-1];
    			cout << p[j][k];
    		}
    			cout << endl;
    	}
    
    return 0;
    }
    
    int main(int){
    int width = setUp();
    	triangle(width);
    
    return 0;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Access violations and lost PBD files

    This is what your debugger is for. Use it to look at the value of i at your cout statement. Perhaps you meant to have cout included as part of your for loop.

  3. #3
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Access violations and lost PBD files

    Maybe your talking about pdb files? Program database files? Try rebuilding your project and debugging as just suggested.
    ahoodin
    To keep the plot moving, that's why.

  4. #4
    Join Date
    Aug 2006
    Posts
    231

    Re: Access violations and lost PBD files

    Raw pointers are the cause of many errors. Learn how to avoid them!

    Here you can use std::vector instead. It can help you by showing assertions or throwing exceptions when something goes wrong, which makes it much easier to find errors.

    You should also add some extra checks, for example to make sure that the input is a value greater than 0.

    If you mean PDB files, then it's probably just a warning. Maybe some library you depend on was built without debug information?

  5. #5
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Access violations and lost PBD files

    There is no doubt that raw pointers cause problems, but they still like to see them in academia as well as at interviews apparently. The extra shared pointer part just adds confusion to the professor or the interviewer. Don't get me wrong, in practice, best use those shared pointers. I know I do.
    ahoodin
    To keep the plot moving, that's why.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Access violations and lost PBD files

    The OP is stepping over the end of the array. No need to confuse things with discussions of the evils of pointers.

  7. #7
    Join Date
    Aug 2006
    Posts
    231

    Re: Access violations and lost PBD files

    Quote Originally Posted by GCDEF View Post
    The OP is stepping over the end of the array. No need to confuse things with discussions of the evils of pointers.
    Regarding the question about access violation, then yes, that's all there's to it.

    But there are lots of wrong things in that code, for example memory leaks. I'm just trying to point the topic starter in the right direction.

    It's unfortunate that people still learn to write such error-prone code. Long functions with lots of loops and pointers, that can often be rewritten in just a few lines of code without any runtime overhead. Then bugs can't hide as easily!

    I believe it's for the best to push people to write good code from the start. Well, maybe this isn't the right place to do so, but a few lines of recommendations can't hurt.

  8. #8
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Access violations and lost PBD files

    Additionally, pdb files contain debugging and state information,
    so if these files have problems, that may lend to trouble debugging
    his project. Hence the re-build.
    ahoodin
    To keep the plot moving, that's why.

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Access violations and lost PBD files

    to print Pascals triangle of n size
    If the requirement is to just print a triangle of a given number of rows, then all you really need are two arrays - one to hold the previous row and one to hold the row being computed. The row being computed can be calculated from the previous one. Unless it is a requirement of the assignment to use pointers and new(), then as TublarX said in post #4 it is better to use vectors. See http://www.cplusplus.com/reference/vector/vector/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Access violations and lost PBD files

    I concurr on the vectors, because of the issues surrounding shared pointers and the use of arrays. You have to create a custom deleter to pass to the shared pointer constructor. This is not really a brand new beginners bag of code with array, but vector could fix that.
    ahoodin
    To keep the plot moving, that's why.

  11. #11
    Join Date
    Feb 2016
    Posts
    3

    Re: Access violations and lost PBD files

    Thank you for the help. I will look into making the code tidier now that I can see what it prints.
    Would it fix the memory leak issues if I put delete[] p; at the end of triangle()? If that isn't all that needs to be done I would really appreciate if you could give me a keyword that I could use when searching for ways to further improve it

  12. #12
    Join Date
    Feb 2016
    Posts
    3

    Re: Access violations and lost PBD files

    There aren't any specific requirements on what to use, it's just that we haven't gone through vectors on the lessons so arrays felt more at home. This assignment isn't actually on the course, it's just something thrown in to keep the ones a bit further away busy and I guess looking into new things. So I can do pretty much all I want with it.
    I hadn't even thought about what I need to keep with me when calculating the new values. It's actually a good idea. Wouldn't have me in over my head with pointers and multidimensional arrays

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Access violations and lost PBD files

    we haven't gone through vectors on the lessons
    At its simplest, vectors can be used like arrays where the size can be set at run-time rather than compile time. Once a vector has been defined with a size then it can be used like an array - but without the issues re new/delete. Consider
    Code:
    #include <vector>
    using namespace std;
    
    int main()
    {
    	constexpr int v_size = 10;
    
    	vector<int> vec(v_size);
    
     	vec[0] = 34;
    	vec[v_size - 1] = 56;
    	vec[1] = vec[0] + 10;
    }
    vec is a vector of size v_size that will hold values of type int. It has v-size number of elements which start at 0 just like arrays. v_size can be set at run-time. Once defined with a size then vec can be used like an array. There's much more to vectors than just using them like an array but for your task this'll do fine. If you want more info have a look at the link I posted in my post #9. Happy coding!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Access violations and lost PBD files

    Would it fix the memory leak issues if I put delete[] p; at the end of triangle()?
    You're on the right lines but no, not quite. For each row you have allocated memory for the columns with new[], so you need to iterate again the rows and use delete[] to free this column memory. Then you use delete[] again to free the memory allocated for the rows. Vectors are so much less hassle/worry to use!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured