Static memory being overwritten?

This is a discussion on Static memory being overwritten? within the C++ Programming forums, part of the General Programming Boards category; --EDIT-- realized it had to do with my char[] size. thanks. I am trying to create a PAKer and having ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    54

    Static memory being overwritten?

    --EDIT-- realized it had to do with my char[] size. thanks.


    I am trying to create a PAKer and having issues with my variables suddenly changing out of no where. As soon as i call strcpy in the cody below the packHeader suddenly gets filled up with the name of the file being strcpy'ed and very large number for its data.

    im clueless as to what is going on with my data...

    Code:
    ///////////////////////////////////////////////////////////////////////
    // Function to pack a single file into an opened pak.
    //
    // Input:   file - filepath of file to pack
    ///////////////////////////////////////////////////////////////////////
    bool PAKUtility::Pack( const char *file )
    {
                    m_newFile.open(file, std::ios_base::in | std::ios_base::binary);
    
    	if(!m_newFile.is_open())
    		return false;
    
    	const int READ_SIZE = 1024;
    	char data[READ_SIZE];
    
    	// read existing main header
    	m_PakFile.seekg(0, std::ios_base::beg);
    	m_PakFile.seekp(0, std::ios_base::beg);
    
    	PAKHEADER packHeader;
    	m_PakFile.read((char*)&packHeader, sizeof(PAKHEADER));
    
    	// build & write new header for new file
    	m_newFile.seekg(0, std::ios_base::end);
    
    	FILEHEADER fh;
    	strcpy(fh.name, file);
    	fh.fileOffset = packHeader.dirOffset;
    	fh.fileLength = m_newFile.tellg();
    	m_newFile.seekg(0, std::ios_base::beg);
    
    	m_PakFile.seekg(packHeader.dirOffset, std::ios_base::beg);
    
    	FILEHEADER* fHeaders = NULL;
    
    	int numHeaders = (packHeader.dirSize >> 6);
    	if(numHeaders > 0)
    	{
    		fHeaders = new FILEHEADER[numHeaders];
    		for(int i = 0; i < numHeaders; i++)
    			m_PakFile.read((char*)&fHeaders[i], sizeof(FILEHEADER));
    	}
    
    	// append the new file in chunks
    	m_newFile.seekg(0, std::ios_base::beg);
    	m_PakFile.seekp(packHeader.dirOffset, std::ios_base::beg);
    
    	while (m_newFile.good())
    	{
    		m_newFile.read(data, READ_SIZE);
    		m_PakFile.write(data, m_newFile.gcount());
    	}
    
    	if(numHeaders > 0)
    		for(int i = 0; i < numHeaders; i++)
    			m_PakFile.write((char*)&fHeaders[i], sizeof(FILEHEADER));
    
    	m_PakFile.write((char*)&fh, sizeof(FILEHEADER));
    
    	// Update header
    	packHeader.dirOffset += fh.fileLength;
    	packHeader.dirSize += 64;
    
    	m_PakFile.seekp(0, std::ios_base::beg);
    	m_PakFile.write((char*)&packHeader, sizeof(PAKHEADER));
    
    	m_newFile.close();
    	m_newFile.clear();
    
    	CListBox* pList = (CListBox*)theApp.GetMainWnd()->GetDlgItem(IDC_LIST2);
    
    	int counter = pList->GetCount();
    
    	for(int i = counter - 1; i > 0; i--)
    		pList->DeleteString(i);
    
    	for(int i = 0; i < numHeaders; i++)
    		pList->AddString(fHeaders[i].name);
    
    	delete [] fHeaders;
    	return true;
    }
    Last edited by JeremyCAFE; 06-17-2006 at 06:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 12:41 PM
  2. uploading file to http server via multipart form data
    By Dynamo in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2008, 05:36 AM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 06:05 PM
  4. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 08:54 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM

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