file read and buffer problems

Reply

Join Date: Jan 2008
Posts: 11
Reputation: loushou is an unknown quantity at this point 
Solved Threads: 0
loushou loushou is offline Offline
Newbie Poster

file read and buffer problems

 
0
  #1
Jan 24th, 2008
So finally after a little waking up i fixed my write to a file in raw binary problem. Now I am trying to load the same data from the file i created. Here is the code I came up with.

  1. #include <fstream>
  2. #include ".\vertexs.h"
  3.  
  4. using namespace std;
  5.  
  6. int LoadVerts(char *s_fName, OURCUSTOMVERTEX *p_Verts, short *p_Indices)
  7. {
  8. long i_CountVerts;
  9. long i_CountInds;
  10.  
  11. unsigned char buffer[sizeof(OURCUSTOMVERTEX)];
  12.  
  13. basic_ifstream<unsigned char> f_DataFile;
  14.  
  15. f_DataFile.open(s_fName, std::ios_base::in | std::ios_base::binary);
  16.  
  17. f_DataFile.get(buffer, sizeof(unsigned long));
  18. memcpy_s(&i_CountVerts, sizeof(unsigned long), &buffer, sizeof(unsigned long));
  19. f_DataFile.get(buffer, sizeof(unsigned long));
  20. memcpy_s(&i_CountInds, sizeof(unsigned long), &buffer, sizeof(unsigned long));
  21.  
  22. OURCUSTOMVERTEX *p_temp_Verts = new OURCUSTOMVERTEX[i_CountVerts];
  23. short *p_temp_Indices = new short[i_CountInds];
  24.  
  25. for (int temp = 0; temp < i_CountVerts; temp++)
  26. {
  27. f_DataFile.get(buffer, sizeof(OURCUSTOMVERTEX));
  28. memcpy(&p_temp_Verts[temp], &buffer, sizeof(OURCUSTOMVERTEX));
  29. }
  30.  
  31. for (int temp2 = 0; temp2 < i_CountInds; temp2++)
  32. {
  33. f_DataFile.get(buffer, sizeof(short));
  34. memcpy(&p_Indices[temp2], &buffer, sizeof(short));
  35. }
  36. f_DataFile.close();
  37.  
  38. delete [] p_Verts;
  39. p_Verts = p_temp_Verts;
  40. delete [] p_Indices;
  41. p_Indices = p_temp_Indices;
  42.  
  43. return 0;
  44. }

The struct OURCUSTOMVERTEX is defined in the vertexs.h header file and contains 3 floats (x, y, z) and a unsigned long (color). When I build it no errors are returned at all, not even so much as a warning. But when I run this a system error as follows occurs:

  1. Unhandled exception at 0x7c9377a2 in saveverts.exe: 0xC0000005: Access violation writing location 0x00030ffc.

Through a little breaking i found that the problem is coming in at the following portion of code:

  1. for (int temp2 = 0; temp2 < i_CountInds; temp2++)
  2. {
  3. f_DataFile.get(buffer, sizeof(short));
  4. memcpy(&p_Indices[temp2], &buffer, sizeof(short));
  5. }

After stepping through the function, I found that the problem is originating here:

  1. f_DataFile.get(buffer, sizeof(long));
  2. memcpy_s(&i_CountInds, sizeof(long), &buffer, sizeof(long));

At this point with my sample run the buffer should read 09 00 00 00 ... as the long stored in the file by the save routine stores an unsigned long with value 9, which is what is being read here. Instead the buffer reads 00 09 00 00, which is 900 hex, which is 2304 decimal. here inlays the problem, i believe. In any event, from my perspective this is the root of the problem, and unfortunately I am unable to troubleshoot it further on my own and must ask for help.

Following is the SaveVerts routine, in hopes that it will help:

  1. int SaveVerts(char *s_fName, OURCUSTOMVERTEX *p_Verts, short *p_Indices, long i_CountVerts, long i_CountInds)
  2. {
  3. unsigned char buffer[sizeof(OURCUSTOMVERTEX)];
  4.  
  5. basic_ofstream<unsigned char> f_DataFile;
  6.  
  7. f_DataFile.open(s_fName, std::ios_base::out | std::ios_base::binary);
  8.  
  9. memcpy(&buffer, &i_CountVerts, sizeof(unsigned long));
  10. f_DataFile.write(buffer, sizeof(unsigned long));
  11. memcpy(&buffer, &i_CountInds, sizeof(unsigned long));
  12. f_DataFile.write(buffer, sizeof(unsigned long));
  13.  
  14. for (int temp = 0; temp < i_CountVerts; temp++)
  15. {
  16. memcpy(&buffer, &p_Verts[temp], sizeof(OURCUSTOMVERTEX));
  17. f_DataFile.write(buffer, sizeof(OURCUSTOMVERTEX));
  18. }
  19.  
  20. for (int temp2 = 0; temp2 < i_CountInds; temp2++)
  21. {
  22. memcpy(&buffer, &p_Indices[temp2], sizeof(short));
  23. f_DataFile.write(buffer, sizeof(short));
  24. }
  25. f_DataFile.close();
  26.  
  27. return 0;
  28. }

And finally this is the section of code in assembly (for all the ASM gurus out there) once the program crash and debug pops:

  1. 7C93779C mov edi,edi
  2. 7C93779E push ebp
  3. 7C93779F mov ebp,esp
  4. 7C9377A1 push ecx
  5. 7C9377A2 push ecx <**************problem line
  6. 7C9377A3 push edi
  7. 7C9377A4 mov edi,7C97C320h
  8. 7C9377A9 cmp dword ptr ds:[7C97C320h],edi
  9. 7C9377AF jne 7C942DA0
  10. 7C9377B5 xor al,al
  11. 7C9377B7 pop edi
  12. 7C9377B8 leave
  13. 7C9377B9 ret 8

Thanks in advance for any insights.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: file read and buffer problems

 
0
  #2
Jan 24th, 2008
The assembly line only points to where the IP was when the error was identified. (So it doesn't help.)

In any case, the problem is that unsigned char buffer[ n ]; creates a variable (buffer) which is, by itself, a pointer to a list of bytes. The number of bytes in the list is known to the compiler, but otherwise the array definition is just like saying:
1. reserve n bytes on the stack
2. create a variable unsigned char *buffer;
3. let buffer point to the first byte just reserved on the stack

Hence, the problem occurs when you say:
memcpy_s(&i_CountInds, sizeof(long), &buffer, sizeof(long));

The types of the desired parameters are:
pointer to bytes, int, pointer to bytes, int.

The types of the arguments you give it are:
pointer to bytes, int, pointer to pointer, int.

So, the first time you read something you are changing the address in 'buffer' to point to some other, random position in memory. The next time you try to access the buffer you get an access violation --meaning that you tried to access memory that doesn't belong to you.

Remember, buffer by itself is a pointer. You can dereference it with
*buffer
or
buffer[ 0 ]
but without the * or [] it is just another pointer variable.

So, to fix your code, use
memcpy_s(&i_CountInds, sizeof(long), buffer, sizeof(long));

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 11
Reputation: loushou is an unknown quantity at this point 
Solved Threads: 0
loushou loushou is offline Offline
Newbie Poster

Re: file read and buffer problems

 
0
  #3
Jan 24th, 2008
While your explaination made perfect sense, it did not solve the problem. I made what should have been the required change and still get:

  1. Unhandled exception at 0x7c9377d5 in saveverts.exe: 0xC0000005: Access violation writing location 0x00030ffc.

You are very sharp to catch this error... and I am slow for not. Though this was a problem... it is not THE problem. Further assistance would be appriciated.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: file read and buffer problems

 
0
  #4
Jan 25th, 2008
You changed every instance of &buffer to buffer in both of the routines posted above?

If you have, I don't know where the problem is. My quick glance says they look alright to me. Unless you are reading or writing too many bytes for your buffer or integer types somewhere...

Does your custom vertex structure have any pointers in it?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 11
Reputation: loushou is an unknown quantity at this point 
Solved Threads: 0
loushou loushou is offline Offline
Newbie Poster

Re: file read and buffer problems

 
0
  #5
Jan 25th, 2008
Yes i changed them all in both functions.

As far as writing and reading too many bytes, i am writing and reading the exact same amount because i structured the LoadVerts routine from the SaveVerts routine with an exact copy and slight modification. Additionally after each of my runs, i am checking the chris. raw file against the output i should have based on my knowledge of how raw data is stored in files. They come out identical.

My struct is as follows and contains no pointers:

  1. struct OURCUSTOMVERTEX
  2. {
  3. float x, y, z;
  4. unsigned long color;
  5. };

Can you see why this is frustrating me so?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: file read and buffer problems

 
0
  #6
Jan 25th, 2008
Maybe you are just paying the price for using ifstream.get()? Is there a byte with the value 10 in your data file?

(You should be using ifstream.read().)

Let me know if that fixes it... (ATM I don't think it will...)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 11
Reputation: loushou is an unknown quantity at this point 
Solved Threads: 0
loushou loushou is offline Offline
Newbie Poster

Re: file read and buffer problems

 
0
  #7
Jan 25th, 2008
I am an IDIOT!

Okay just to make myself look retarded.... i will explain what happened.

Review the original code for LoadVerts.

Look near the bottom at this line:

  1. memcpy(&p_Indices[temp2], buffer, sizeof(short));

The problem here is that the array that needs to be written to is p_temp_Indices (declared in this function) and then copied to the original array. Just goes to show that something small can cause a cog and stump a few.....

WOW! I should be more careful..... LOL!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: file read and buffer problems

 
0
  #8
Jan 25th, 2008
Heh heh heh... I do stuff like that all the time...

Please fix those get()s to read()s though...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC