Helpful ReplyHot!Reading data to dynamic allocated table by FATFS

Author
adi1525
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2017/01/15 08:52:24
  • Location: 0
  • Status: offline
2017/12/07 05:30:31 (permalink)
0

Reading data to dynamic allocated table by FATFS

Hello, I want to write bitmap by the F_read to the table with dynamic allocated array but it's dosen't work. The program stop in function f_read(&file,&bitmapImage,Bi.biSizeImage,&size) and don't go to ahead.
In Bi.biSizeImage is 192.

 

int readBitMap(char *FileName)
{
    FRESULT result;
    FIL file;
    UINT size;
    uint8_t *bitmapImage;
    int a=0,j=0;
    if((result = f_open(&file,FileName,FA_READ))==FR_OK)
    {
        /*Reading the BitMap*/
        
        printf("File opened ! \n");
        f_read(&file,&Bf,sizeof(Bf),&size);
        f_read(&file,&Bi,sizeof(Bi),&size);
        
        bitmapImage = malloc(Bi.biSizeImage);
        printf("size image: %d\n",Bi.biSizeImage);
        f_lseek(&file,138);
        f_read(&file,&bitmapImage,Bi.biSizeImage,&size);
        f_close(&file);
        return 1;
    }else
        printf("Error in 'Open': %d\n",result);
    return 0;
}

 
When I was writing data to the static table, it was working.
#1
NKurzman
A Guy on the Net
  • Total Posts : 15127
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: Reading data to dynamic allocated table by FATFS 2017/12/07 08:11:01 (permalink)
+1 (1)
What does “doesn’t work” mean? You are not checking the return from malloc()
#2
RISC
Super Member
  • Total Posts : 4645
  • Reward points : 0
  • Status: offline
Re: Reading data to dynamic allocated table by FATFS 2017/12/07 10:35:40 (permalink)
0
Hi,
Which processor ?
Which version of XC32 ?
Do you use Harmony ? which version ?
Regards
 
#3
davekw7x
Entropy++
  • Total Posts : 1264
  • Reward points : 0
  • Joined: 2012/01/16 12:01:07
  • Location: Left Coast, USA
  • Status: offline
Re: Reading data to dynamic allocated table by FATFS 2017/12/07 10:57:32 (permalink) ☄ Helpfulby adi1525 2017/12/07 12:42:03
+2 (2)
adi1525
 

    uint8_t *bitmapImage;
.
.
        bitmapImage = malloc(Bi.biSizeImage);
.
.
        f_read(&file,&bitmapImage,Bi.biSizeImage,&size);




Since I don't have your entire source program, I don't know whether anything else is wrong in the code that you show (or, especially, in the code that you don't show), but I think there is a Very Big Problem here:
 
The second parameter of f_read() is a pointer to unsigned char, right?  Its value will be the address of a region in memory where the information will be stored.  [Edit](Maybe the function is defined with that parameter as a void pointer, but the function will use its value as a pointer to char when it copies the stuff.)[/Edit]
 
Anyhow, the second argument should be bitmapImage, not &bitmapImage.
 
See?  the value of bitmapImage is the address of the first element in your "dynamically allocated array," but &bitmapImage is the address of the variable bitmapImage, not the address of the beginning of the allocated region in memory.
 
Now...
The reason you might 'get away with it' if you use similar code for a declared array is that if you have something like the following.
 
Suppose you have
  uint8_t x[80];

 
Then...
x used by itself in an expression (without [] brackets), is a pointer to uint8_t, and its value is the address of the first element of the array.  The second argument in the call to f_read() should be x, not &x.

However...
In fact, since &x is the pointer to an array, it happens that the value of &x is the  address of the first element of the array.  Since &x has the same numerical value as x, this particular function "works."  I think it's a Very Bad Thing to get into the habit of improper usage even though it apparently "works" in the case of arrays.  Some compilers give a warning if you use the address of an array of char where a pointer to char should be used.  If, however, the function's definition of the parameter was a void pointer, the compiler might not bail you out.  You have to do it right.
 
And, of course, as NKurzman pointed out, you should always check the return value of malloc() and its siblings to make sure it was successful (although I don't know what you should/can do about it if it fails).
 
That's why I try to avoid dynamic allocation where possible.  I mean, if you are going to allocate this once and never free it, why not just use a declared array?  And if you are going to free it, then, for goodness sake, return the value that malloc gave you so that you can deallocate it!
 
And, by the way, since you return after reading, but never do anything with the stored image and nothing outside that function can possibly know where it is, what's the point?
 
Finally...
I have noted, from time to time, that source code in various Microchip distributions (as well as many examples of code from other sources) uses &x for array arguments where x should have been used.  The code "works," but serves as a Very Bad Example that people should not copy.
 
 
Regards,
 
Dave
post edited by davekw7x - 2017/12/07 11:34:12

Sometimes I just can't help myself...
#4
adi1525
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2017/01/15 08:52:24
  • Location: 0
  • Status: offline
Re: Reading data to dynamic allocated table by FATFS 2017/12/07 12:41:12 (permalink)
0
Thanks everyone for interest !
For first I check returned from malloc and I found out that I must increase Heap size.
Next that like said davekw7x second argument must be bitmapImage without "&". I must make dynamic array because I want to load photo with different resolution. 
I will add that I can't use perror because I get:
 
liblega-c.a(perror.o): In function `perror':
perror.c:(.text.perror+0xb4): undefined reference to `writev'

Yes, I have defined #include <stdio.h> 
 
Instead of this, I used:
printf("%s\n",strerror(errno)); from errno.h library
 
Very thanks for response ;)
#5
davekw7x
Entropy++
  • Total Posts : 1264
  • Reward points : 0
  • Joined: 2012/01/16 12:01:07
  • Location: Left Coast, USA
  • Status: offline
Re: Reading data to dynamic allocated table by FATFS 2017/12/07 14:01:27 (permalink)
0
adi1525
... I can't use perror ...



If malloc() fails, it returns a null pointer, so you can check that.  (Rather than depending on the global errno variable.)
 
 
Regards,
 
Dave
post edited by davekw7x - 2017/12/07 14:10:03

Sometimes I just can't help myself...
#6
adi1525
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2017/01/15 08:52:24
  • Location: 0
  • Status: offline
Re: Reading data to dynamic allocated table by FATFS 2017/12/07 14:12:46 (permalink)
0
Aaa ok I understandLoL: LoL
But I think this program dosen't work, I reading all bitmap to the buffer and next sent it to the LCD. When my file will by larger than 58kB (free memory), then it's dosen't work.
So I must read some block of bitmap sending it to the LCD, read again memory and send again ??
#7
aschen0866
Super Member
  • Total Posts : 4184
  • Reward points : 0
  • Joined: 2006/01/08 22:18:32
  • Location: San Diego
  • Status: offline
Re: Reading data to dynamic allocated table by FATFS 2017/12/07 19:49:41 (permalink)
+1 (1)
 
Don't you need to free() what malloc() allocates? I don't see that in the code you posted.
#8
adi1525
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2017/01/15 08:52:24
  • Location: 0
  • Status: offline
Re: Reading data to dynamic allocated table by FATFS 2017/12/08 02:13:01 (permalink)
0
Yes I need, but I must change program because I can't read large photo, so I change program to read 512 byte, send to LCD and again read.
#9
Jump to:
© 2017 APG vNext Commercial Version 4.5