Thread: Array Allocation Troubles

  1. #1
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254

    Array Allocation Troubles

    I do the following:
    Code:
        unsigned char* tmp_data = new unsigned char[this->_level_size*this->_level_size*4 + 1];
        tmp_data[this->_level_size*this->_level_size*4] = '\0';
    
        std::ifstream in_file(levelmap.c_str(), std::ios::in | std::ios::binary);
        in_file.read((char *)&tmp_data,this->_level_size*this->_level_size*4);
        in_file.close();
    Yet, it often segfaults. When I run with GDB, it says that the address pointed to by "tmp_data" is "out of bounds." When I run with valgrind, I get similar errors. This code seems like it should be perfectly innocent. What am I doing wrong?
    Last edited by CrazyNorman; 05-20-2006 at 08:42 AM.
    Programming Your Mom. http://www.dandongs.com/

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > This code seems like it should be perfectly innocent. What am I doing wrong?
    You need to look at how you got to this point.

    Like you said, this->_level_size is trash so start by looking at how you create the object in question.
    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.

  3. #3
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    Sorry, I meant to say that "tmp_data" is out of bounds (or rather the location that it is pointing to). level_size is 1024, and has no trouble whatsoever.
    Programming Your Mom. http://www.dandongs.com/

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > (char *)&tmp_data
    Probably because you're trashing the pointer itself (which is only say 4 bytes), with the 4MB of data you're reading from the file.

    Drop the & if you want the data to go into allocated memory.
    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.

  5. #5
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    Oops. Thanks for pointing that out.
    Programming Your Mom. http://www.dandongs.com/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to array with dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 22
    Last Post: 04-07-2009, 09:56 AM
  2. memory allocation for array
    By lehe in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2009, 09:05 PM
  3. dynamic 2D array allocation
    By deprekate in forum C Programming
    Replies: 5
    Last Post: 03-03-2009, 04:25 AM
  4. 3d array allocation
    By C_ntua in forum C Programming
    Replies: 18
    Last Post: 10-22-2008, 03:16 PM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM