![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
Reading a binary file into a dynamically allocated 2D arrayHi all,
I have a problem with this (see title). So, I have a binary file which contains a matrix. The first 4 bytes of the file are the dimensions of the matrix. The following bytes are the elements of it. Here is the code: CPP / C++ / C Code:
The method works with fixed-size 2D array, so I tried this with a dynamically allocated array. However, the program crashes at runtime. Anybody knows how to perform this..? Many thanks before! Cheers, Arthur Last edited by LuciWiz : 13-May-2009 at 06:22.
Reason: Please insert your C++ code between [cpp] & [/cpp] tags
|
|||
|
#2
|
|||
|
|||
Re: Reading a binary file into a dynamically allocated 2D arrayNot suprisinlgly this is a common question. (I need to review it too!)
Searching the site can be VERY helpful. Here is one example of allocating and working with a 2d array: gidforums.com/t-18393.html It should help you see what you need to change in your function. You might also want to include a sample main() for us to try out your function with like: CPP / C++ / C Code:
|
|
#3
|
|||
|
|||
Re: Reading a binary file into a dynamically allocated 2D arrayQuote:
1. Give us the file specification. Exactly. 2. Give us the exact specification of the function that is going to read the data. (What are the function parameters: inputs, outputs?) 3. Show us the code of the function that works with a 2-D array. 4. If you can't show us the entire program, then at least show us the declarations in the calling function (main() or whatever) and show us how it calls the function that reads the data from the file. 5. Show us how you are going to use whatever data structure that holds the data that you read from the file. (Do you expect to have three 2-D arrays and access each one by "row" and "column" or what?) Regards, Dave |
|
#4
|
|||
|
|||
Re: Reading a binary file into a dynamically allocated 2D arrayHere is the complete code:
CPP / C++ / C Code:
The purpose is to read an image from a binary file, which is created for this purpose only. The first 4 bytes (2 shorts) contain the dimension of the image, then the following bytes are the image data. The program crashes at runtime. However, regarding the ifstream::read function, if I use 2D-array with fixed size, e.g. CPP / C++ / C Code:
the program works perfectly. Cheers, Arthur Last edited by admin : 14-May-2009 at 20:04.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|
#5
|
|||
|
|||
Re: Reading a binary file into a dynamically allocated 2D arrayQuote:
The following statement tries to store 262,144 chars from the file into a contiguous area in memory, starting at the address pointed to by imgR CPP / C++ / C Code:
However, assuming that the values of imgW and imgH are 512, I see that your program has 512 different allocation statements for 512 different chunks of memory imgR. So the memory blocks that hold the rows or rgbR data are not contiguous. See Footnote. Successive blocks of allocated memory are not guaranteed to be contiguous, and, in fact, I can just about guarantee that blocks of memory separately allocated by new (or malloc(), or whatever...) are not contiguous. However, your 512 blocks for the 512 rows of imgR data aren't even allocated successively. The blocks for each row of rgbG and rgbB follow the blocks for the rgbR rows. Here's the deal: If you allocate memory a row at a time, you can't read multiple rows at a time. Period. Full stop. In particular: If you allocate contiguous storage a 512 bytes at a time, you can't read more than 512 bytes with a single read() statement. If you want to read a block of 262,144 bytes into contiguous storage with a single read() statement, you have to allocate 262,144 bytes at a time. I'm guessing that this is not what you need to do, since accessing the data as a 2-D array by row and column index is another matter, and won't work the way that you seem to want it to. Regards, Dave Footnote: The statement unsigned char imgR[2][512]; allocates 1024 bytes of contiguous memory in row-major order. That is, it is absolutely guaranteed that imgR[1][0] is stored immediately after imgR[0][511] Assuming that imgR is a pointer-to-pointer to unsigned char, and that memory for two or more pointers has been allocated and the starting point assigned to imgR, the following two statements allocate two separate blocks of 512 bytes each. CPP / C++ / C Code:
I hate to repeat myself, but: Each block consists of 512 chars of contiguous storage. The two blocks are not guaranteed to be contiguous. That is, it is not guaranteed that imgR[1][0] immediately follows imgR[0][511] (And in all compilers that you are likely to run across on your workstation these days they are definitely not contiguous.) Last edited by davekw7x : 14-May-2009 at 00:07.
|
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Power Calibration Error In Nero Fix (hopefully) | matt3678 | Computer Hardware Forum | 60 | 20-Aug-2009 06:04 |
| contents of .txt file into 2D array | anirudhroxrulz | C Programming Language | 4 | 10-Apr-2008 23:45 |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 08:44 |
| Double linked List & File System | NatsoumiMaya | C Programming Language | 1 | 10-Feb-2008 09:23 |
| After execution - Error cannot locate /Skin File? | WSCH | C++ Forum | 1 | 05-Mar-2005 21:03 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The