Array Allocation Troubles
This is a discussion on Array Allocation Troubles within the C++ Programming forums, part of the General Programming Boards category; 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] = '
';
std::ifstream in_file(levelmap.c_str(), std::ios::in ...
-
Amazingly beautiful user.
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.
-
and the hat of mystery
> 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.
-
Amazingly beautiful user.
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.
-
and the hat of mystery
> (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.
-
Amazingly beautiful user.
Oops. Thanks for pointing that out.
Popular pages Recent additions
Similar Threads
-
By cfdprogrammer in forum C Programming
Replies: 22
Last Post: 04-07-2009, 09:56 AM
-
By lehe in forum C++ Programming
Replies: 1
Last Post: 03-31-2009, 09:05 PM
-
By deprekate in forum C Programming
Replies: 5
Last Post: 03-03-2009, 03:25 AM
-
By C_ntua in forum C Programming
Replies: 18
Last Post: 10-22-2008, 03:16 PM
-
By vasanth in forum C Programming
Replies: 2
Last Post: 11-09-2003, 11:09 AM