My most amazing error yet! [complicated]

This is a discussion on My most amazing error yet! [complicated] within the C++ Programming forums, part of the General Programming Boards category; >> Are you even allowed to just take the address of the first element of a vector, and assume that ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Are you even allowed to just take the address of the first element of a vector, and assume that everything is contiguous from that point?

    Yes, the code you quoted is correct. The vector version has the advantage of automatic memory management as well, which is why it is preferred over your solution.
    Last edited by Daved; 10-31-2006 at 02:36 PM.

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >	WNDCLASSEX wcex;		
    >	ZeroMemory(&wcex, sizeof(wcex));
    >	wcex.cbSize	= sizeof(WNDCLASSEX);	
    >	wcex.style	= CS_HREDRAW | CS_VREDRAW;
    >	wcex.lpfnWndProc = (WNDPROC) WndProc;
    >	wcex.hInstance= hInstance;
    >	wcex.hCursor= LoadCursor(NULL, IDC_ARROW);
    >	wcex.lpszClassName= name;
    It looks like you never called RegisterClassEx to register the window class.

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Pedantically, even reading an uninitialised value may cause a trap for anything which isn't a char.
    Wow, I did not know that. I knew it was true for unitialized pointers, but not other types.

  4. #19
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    Quote Originally Posted by swoopy
    Code:
    >	WNDCLASSEX wcex;		
    >	ZeroMemory(&wcex, sizeof(wcex));
    >	wcex.cbSize	= sizeof(WNDCLASSEX);	
    >	wcex.style	= CS_HREDRAW | CS_VREDRAW;
    >	wcex.lpfnWndProc = (WNDPROC) WndProc;
    >	wcex.hInstance= hInstance;
    >	wcex.hCursor= LoadCursor(NULL, IDC_ARROW);
    >	wcex.lpszClassName= name;
    It looks like you never called RegisterClassEx to register the window class.
    I just checked my code, that was a copy/past error. My code had lots of extra white space, so I must have removed that by accident when formatting it for my post. The RegisterClassEx is in my actual code.

  5. #20
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    If your "test" code is no more than a few hundred lines, then ZIP it up in an attachment, and include small examples of the .ini and .raw files.

    Simply posting random snippets, with random bits of key information removed by your over zealous editing is just going to keep throwing up "you missed...." responses.

    Basically, do a "build->Clean" then ZIP the project.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #21
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    It wouldn't let me upload a .zip, so here are five of the files, and below are the contents of the ini:

    WindowName = Glitch
    Resolution = 640*480
    Fullscreen = 0
    Attached Files Attached Files

  7. #22
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    What are vertices? 1 vertex is 1 byte? If not, you got an error here I think

    Code:
    bool heightMap::readRawFile(char *fname)
    {
    	std::vector<BYTE> in(numVertices);
    	inFile.read((char*)&in[0], in.size());
    	return true;
    }

  8. #23
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    > fread(fileText, 1, count, theFile);
    I see from the rest of your code that you're reading a text file.

    Some points.
    1. You don't look at the return result of fread
    2. You don't put a \0 at the end to make it a string
    3. You don't allocate space for this \0

    So perhaps
    Code:
        while( fread(temp, 1, 1,theFile) == 1 ) 
        {
          count++;
        }
        
        filetext = new char[count+1];
        rewind(theFile);              
        int n = fread(filetext, 1, count, theFile);
        if ( n > 0 ) filetext[n] = '\0'
    > delete(filetext);
    1. You do it in TWO places, so you're likely to end up deleting it twice (which is worse than not deleting it at all).
    2. Since you new'ed an array, you need to delete an array.
    delete [ ] filetext;

    If you want to delete it in closeFile(), at least do this so the dtor is harmless
    delete [ ] filetext;
    filetext = 0;



    And man, your comments are indented SO far off to the right as to be totally meaningless.

    Finally, take out all those while() loops which are your 'fix' for the problem. Whatever the answer will be, you can be sure that the answer won't be using them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #24
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    Quote Originally Posted by Salem
    > fread(fileText, 1, count, theFile);
    I see from the rest of your code that you're reading a text file.

    Some points.
    1. You don't look at the return result of fread
    2. You don't put a \0 at the end to make it a string
    3. You don't allocate space for this \0

    So perhaps
    Code:
        while( fread(temp, 1, 1,theFile) == 1 ) 
        {
          count++;
        }
        
        filetext = new char[count+1];
        rewind(theFile);              
        int n = fread(filetext, 1, count, theFile);
        if ( n > 0 ) filetext[n] = '\0'
    Thanks, I'm trying that out I

    EDIT: I don't actually know if it's working better or not because I'm still getting the errors, but it definitely looks cleaner than what I had before.

    Quote Originally Posted by Salem
    > delete(filetext);
    1. You do it in TWO places, so you're likely to end up deleting it twice (which is worse than not deleting it at all).
    2. Since you new'ed an array, you need to delete an array.
    delete [ ] filetext;

    If you want to delete it in closeFile(), at least do this so the dtor is harmless
    delete [ ] filetext;
    filetext = 0;
    Thanks for pointing that out, I've fixed that.

    Quote Originally Posted by Salem
    And man, your comments are indented SO far off to the right as to be totally meaningless.
    Sorry bout that, my computer has a widescreen resolution so they looked fine on it.

    Finally, take out all those while() loops which are your 'fix' for the problem. Whatever the answer will be, you can be sure that the answer won't be using them.
    [/QUOTE]

    I know that they're not an elegant solution, but they were working when the only problem was the code not initialising variables, but there's too much weird stuff going on, so they've been removed. (I believe they were commented out in the version that I uploaded?)


    I've tested it after trying all those things, and I'm still getting the same errors. Did you see anything in it that seemed blatantly wrong at this point? I've been going through it for three or four weeks now and a fresh pair of eyes is always useful.
    Last edited by therabidwombat; 11-01-2006 at 02:10 AM.

  10. #25
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    Upload your latest, and I'll have another look tonight.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #26
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    Here's my up to date stuff, I went through it all again a couple times in search of something wrong, but I didn't see anyting ><. As of current, a lot of stuff is commented out because I suspected that it might be related to the issue, although nothing changed when I commented most of it out.

    Thanks again for all your help.
    Attached Files Attached Files

  12. #27
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    I don't have the patience to crawl through your chaotic mix of C and C++ comments.

    It makes no sense to me where the real code begins.

    Randomness prevails.

    You're still not deleting the string properly.

    > although nothing changed when I commented most of it out.
    Except a lot of class member variables which were being initialised are now garbage.

    For a start, I would make each CONSTRUCTOR actually do something useful, like make sure every variable is initialised, and every pointer is NULL.
    At least then you might stand a chance of getting consistency.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #28
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    Quote Originally Posted by Salem
    I don't have the patience to crawl through your chaotic mix of C and C++ comments.

    It makes no sense to me where the real code begins.
    It begins in the WinMain function in Glitch.cpp.

    Quote Originally Posted by Salem
    Randomness prevails.

    You're still not deleting the string properly.

    > although nothing changed when I commented most of it out.
    Except a lot of class member variables which were being initialised are now garbage.
    Which class members specifically? When I've been running it, the same class members have been garbage since basically the very beggining...commenting out stuff hasn't fixed that.

    Quote Originally Posted by Salem
    For a start, I would make each CONSTRUCTOR actually do something useful, like make sure every variable is initialised, and every pointer is NULL.
    At least then you might stand a chance of getting consistency.
    ok

  14. #29
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    About the only thing I can come up with is this:
    >game *Glitch = new game(); //lookit that! Glitch is a game now.

    You do this outside of any function, and I don't know the ramifications. It seems to compile, but just for fun, I would try moving the new part into WinMain:
    Code:
    //Keep global
    game *Glitch;
    
    //Add to WinMain
       Glitch = new game();

  15. #30
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    I just tried that, it didn't change anything. Was worth a shot though

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PGO is amazing
    By cyberfish in forum Tech Board
    Replies: 8
    Last Post: 02-13-2009, 12:30 AM
  2. When done right, PC games are amazing
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-13-2008, 06:32 PM
  3. Amazing Pool Tricks
    By hk_mp5kpdw in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-07-2005, 08:50 PM
  4. Hiking in the Rockies..Amazing!
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 01-11-2005, 10:36 AM
  5. It's amazing...
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 01-18-2002, 02:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21