Problem Writing to Disk ( > 4kB )

Hello,

I am trying to save an image from a framegrabber to a file on my hard
disk. I am using the fwrite() command, however, when the size gets larger
than 4000 bytes, I get a memory fault. Does anyone know if this is due to
a memory shortage, or parameter that I have to set?

Thanks,
Mathew.

Mathew Asselin <m2asselin@yahoo.com> wrote:

Hello,

I am trying to save an image from a framegrabber to a file on my hard
disk. I am using the fwrite() command, however, when the size gets larger
than 4000 bytes, I get a memory fault. Does anyone know if this is due to
a memory shortage, or parameter that I have to set?

Would need more detail on the code to know why you get the memory fault –
but it is unlikely to be a problem with either file size, or with the
fwrite() command. It is most likely to be an error in the size allocated
for buffering/handling the memory in your code.

BTW, for handling large amounts of data you may find that using open()
and write() rather than fopen() and fwrite() will be more efficient and
give you much better throughput. (Generally, FILE * i/o is useful if
you need local buffering of many small writes accumulated into a large
one for efficiency, but if you’re dealing with largish data chunks
(definitely > 1000 bytes, likely anything > 500 bytes) then the extra
overhead of the file streams will be bad for performance.)

-David

Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com

Hi David,

the framegrabber stores an image from a camera and points to the location
in memory by CaptInfo_.lin_mem. I then read each pixel, and attempt to
write them all to a file, individually, using fwrite(). Bytes_Per_Pixel
is set to 1.

ArvBYTE CaptBuffer[2][2];
for( j=0; j<captInfo.h; j++ )
{
CaptBuffer[0] = (ArvBYTE
)captInfo.lin_mem + j *
captInfo.pitch;
for( k=0; k<captInfo.w; k++ )
{
fwrite( CaptBuffer[0], Bytes_Per_Pixel, 1, NEW_FILE );
CaptBuffer[0]++;
}
}

Thanks, Matt.


David Gibbs wrote:_

_Mathew Asselin <> m2asselin@yahoo.com> > wrote:
Hello,

I am trying to save an image from a framegrabber to a file on my hard
disk. I am using the fwrite() command, however, when the size gets larger
than 4000 bytes, I get a memory fault. Does anyone know if this is due to
a memory shortage, or parameter that I have to set?

Would need more detail on the code to know why you get the memory fault –
but it is unlikely to be a problem with either file size, or with the
fwrite() command. It is most likely to be an error in the size allocated
for buffering/handling the memory in your code.

BTW, for handling large amounts of data you may find that using open()
and write() rather than fopen() and fwrite() will be more efficient and
give you much better throughput. (Generally, FILE * i/o is useful if
you need local buffering of many small writes accumulated into a large
one for efficiency, but if you’re dealing with largish data chunks
(definitely > 1000 bytes, likely anything > 500 bytes) then the extra
overhead of the file streams will be bad for performance.)

-David

Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com_

Mathew Asselin <m2asselin@yahoo.com> wrote:

Hi David,

the framegrabber stores an image from a camera and points to the location
in memory by CaptInfo> .lin_mem. I then read each pixel, and attempt to
write them all to a file, individually, using fwrite(). Bytes_Per_Pixel
is set to 1.

ArvBYTE CaptBuffer[2][2];
for( j=0; j .h; j++ )
{
CaptBuffer> [0] = (ArvBYTE
)captInfo> .lin_mem + j *
captInfo> .pitch;
for( k=0; k .w; k++ )
{
fwrite( CaptBuffer> [0], Bytes_Per_Pixel, 1, NEW_FILE );
CaptBuffer> [0]++;
}
}

Again, I don’t know what your data structures look like – but the
most likely problem is that part way through the loop CaptBuffer[0]
becomes a point to invalid memory. (Assuming NEW_FILE is someting
reasonable of course.) (I also assume that ArvBYTE has a size of
one byte.)

Also, it strikes me that:

for( k=0; k .w; k++ )
{
fwrite( CaptBuffer[i][0], Bytes_Per_Pixel, 1, NEW_FILE );
CaptBuffer[i][0]++;
}

_Could be far more efficiently written as:
fwrite( CaptBuffer[i][0], Bytes_Per_Pixel, captInfo[i].w, NEW_FILE );
CaptBuffer[i][0] += captInfo[i].w; /* if the pointer side effect needed */

-David

Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com_

David, why do you find it necessary to assume ArvBYTE is one byte?
Wny could it not be 3, or 4? You might think have to fix your code
to say “CaptBuffer_[0] += capInfo.w * Bytes_Per_Pixel;” then too,
of course, but actually you could just delete that CaptBuffer[0]
update line entirely.

Anyway, I find the whole CaptBuffer declaration strange. The whole
example would have been equivalent to this:

for (j=0; j<captInfo.w; k++) {
ArvBYTE *CaptLine = (ArvByte *)captInfo.lin_mem +
j * captInfo.pitch;
fwrite (CaptLine, Bytes_Per_Pixel, captInfo, NEW_FILE);
}

where obviously you could even remove my declaration and just
substitute in the expression that is its rhs.

dB
\


David Gibbs wrote ~ 28 Jul 2004 17:52:51 GMT:_

_Mathew Asselin <> m2asselin@yahoo.com> > wrote:
Hi David,

the framegrabber stores an image from a camera and points to the location
in memory by CaptInfo> .lin_mem. I then read each pixel, and attempt to
write them all to a file, individually, using fwrite(). Bytes_Per_Pixel
is set to 1.

ArvBYTE CaptBuffer[2][2];
for( j=0; j .h; j++ )
{
CaptBuffer> [0] = (ArvBYTE
)captInfo[i].lin_mem + j *
captInfo[i].pitch;
for( k=0; k<captInfo[i].w; k++ )
{
fwrite( CaptBuffer[i][0], Bytes_Per_Pixel, 1, NEW_FILE );
CaptBuffer[i][0]++;
}
}

Again, I don’t know what your data structures look like – but the
most likely problem is that part way through the loop CaptBuffer[i][0]
becomes a point to invalid memory. (Assuming NEW_FILE is someting
reasonable of course.) (I also assume that ArvBYTE has a size of
one byte.)

Also, it strikes me that:
for( k=0; k<captInfo[i].w; k++ )
{
fwrite( CaptBuffer[i][0], Bytes_Per_Pixel, 1, NEW_FILE );
CaptBuffer[i][0]++;
}

Could be far more efficiently written as:
fwrite( CaptBuffer[i][0], Bytes_Per_Pixel, captInfo[i].w, NEW_FILE );
CaptBuffer[i][0] += captInfo[i].w; /* if the pointer side effect needed */

-David

Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com_

Make that

fwrite (CaptLine, Bytes_Per_Pixel, captInfo_.w, NEW_FILE);

(I left out the .w) -

dB
\


David Bacon wrote ~ 28 Jul 2004 18:39:33 GMT:_

_David, why do you find it necessary to assume ArvBYTE is one byte?
Wny could it not be 3, or 4? You might think have to fix your code
to say “CaptBuffer> [0] += capInfo> .w * Bytes_Per_Pixel;” then too,
of course, but actually you could just delete that CaptBuffer> [0]
update line entirely.

Anyway, I find the whole CaptBuffer declaration strange. The whole
example would have been equivalent to this:

for (j=0; j .w; k++) {
ArvBYTE *CaptLine = (ArvByte *)captInfo> .lin_mem +
j * captInfo> .pitch;
fwrite (CaptLine, Bytes_Per_Pixel, captInfo> , NEW_FILE);
}

where obviously you could even remove my declaration and just
substitute in the expression that is its rhs.

dB
\


David Gibbs wrote ~ 28 Jul 2004 17:52:51 GMT:
Mathew Asselin <> m2asselin@yahoo.com> > wrote:
Hi David,

the framegrabber stores an image from a camera and points to the location
in memory by CaptInfo> .lin_mem. I then read each pixel, and attempt to
write them all to a file, individually, using fwrite(). Bytes_Per_Pixel
is set to 1.

ArvBYTE CaptBuffer[2][2];
for( j=0; j .h; j++ )
{
CaptBuffer[i][0] = (ArvBYTE
)captInfo[i].lin_mem + j *
captInfo[i].pitch;
for( k=0; k<captInfo[i].w; k++ )
{
fwrite( CaptBuffer[i][0], Bytes_Per_Pixel, 1, NEW_FILE );
CaptBuffer[i][0]++;
}
}

Again, I don’t know what your data structures look like – but the
most likely problem is that part way through the loop CaptBuffer[i][0]
becomes a point to invalid memory. (Assuming NEW_FILE is someting
reasonable of course.) (I also assume that ArvBYTE has a size of
one byte.)

Also, it strikes me that:
for( k=0; k<captInfo[i].w; k++ )
{
fwrite( CaptBuffer[i][0], Bytes_Per_Pixel, 1, NEW_FILE );
CaptBuffer[i][0]++;
}

Could be far more efficiently written as:
fwrite( CaptBuffer[i][0], Bytes_Per_Pixel, captInfo[i].w, NEW_FILE );
CaptBuffer[i][0] += captInfo[i].w; /* if the pointer side effect needed */

-David

Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com_

Not doing too well, am I? Here’s what I really meant:

for (j=0; j<captInfo_.h; k++) {
ArvBYTE *CaptLine = (ArvByte *)captInfo.lin_mem +
j * captInfo.pitch;
fwrite (CaptLine, Bytes_Per_Pixel, captInfo.w, NEW_FILE);
}

dB_

The data strucutre is that of an image: the framegrabber stores the data
for each image as a string of bytes ( 1 byte per pixel ), where the string
of bytes begins at CaptInfo.lin_mem.

I am therefore trying to copy this linear vector of bytes into a file,
which can be read off-line for image processing. The problem arises when
I have a picture that is larger than 4000 pixels (=4kB).

The code works fine for pictures smaller than 4kB (although as you have
both put it, and now that I look at it again, it is rather inefficient
code), however the second I increase the picture size, the memory fault
occurs.

Do you know if there are any limitations with respect to the size of a
file when using fwrite()? Does write() have less limitations?

Thanks again, this is of much help to me.
Mathew.


David Gibbs wrote:

Mathew Asselin <> m2asselin@yahoo.com> > wrote:
Hi David,

the framegrabber stores an image from a camera and points to the location
in memory by CaptInfo> .lin_mem. I then read each pixel, and attempt to
write them all to a file, individually, using fwrite(). Bytes_Per_Pixel
is set to 1.

ArvBYTE CaptBuffer[2][2];
for( j=0; j .h; j++ )
{
CaptBuffer> [0] = (ArvBYTE
)captInfo> .lin_mem + j *
captInfo> .pitch;
for( k=0; k .w; k++ )
{
fwrite( CaptBuffer> [0], Bytes_Per_Pixel, 1, NEW_FILE );
CaptBuffer> [0]++;
}
}

Again, I don’t know what your data structures look like – but the
most likely problem is that part way through the loop CaptBuffer> [0]
becomes a point to invalid memory. (Assuming NEW_FILE is someting
reasonable of course.) (I also assume that ArvBYTE has a size of
one byte.)

Also, it strikes me that:
for( k=0; k .w; k++ )
{
fwrite( CaptBuffer[i][0], Bytes_Per_Pixel, 1, NEW_FILE );
CaptBuffer[i][0]++;
}

Could be far more efficiently written as:
fwrite( CaptBuffer[i][0], Bytes_Per_Pixel, captInfo[i].w, NEW_FILE );
CaptBuffer[i][0] += captInfo[i].w; /* if the pointer side effect needed
*/




_-David

Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com_

How is the memory at captInfo_.lin_mem allocated?

By the way (and this is mostly to David Gibbs), the short fwrites
in the original example actually would have made sense if the
frame grabber had stored, say, 4 bytes per pixel but only 3
(or Bytes_Per_Pixel) bytes for each pixel were wanted in the file.

There are no limits on write() or fwrite() that would be applicable
here.

dB
\


m2asselin@yahoo.com (Mathew Asselin) wrote ~ 28 Jul 2004 19:12:40 GMT:_

_The data strucutre is that of an image: the framegrabber stores the data
for each image as a string of bytes ( 1 byte per pixel ), where the string
of bytes begins at CaptInfo.lin_mem.

I am therefore trying to copy this linear vector of bytes into a file,
which can be read off-line for image processing. The problem arises when
I have a picture that is larger than 4000 pixels (=4kB).

The code works fine for pictures smaller than 4kB (although as you have
both put it, and now that I look at it again, it is rather inefficient
code), however the second I increase the picture size, the memory fault
occurs.

Do you know if there are any limitations with respect to the size of a
file when using fwrite()? Does write() have less limitations?

Thanks again, this is of much help to me.
Mathew.


David Gibbs wrote:

Mathew Asselin <> m2asselin@yahoo.com> > wrote:
Hi David,

the framegrabber stores an image from a camera and points to the location
in memory by CaptInfo> .lin_mem. I then read each pixel, and attempt to
write them all to a file, individually, using fwrite(). Bytes_Per_Pixel
is set to 1.

ArvBYTE CaptBuffer[2][2];
for( j=0; j .h; j++ )
{
CaptBuffer> [0] = (ArvBYTE
)captInfo> .lin_mem + j *
captInfo> .pitch;
for( k=0; k .w; k++ )
{
fwrite( CaptBuffer> [0], Bytes_Per_Pixel, 1, NEW_FILE );
CaptBuffer> [0]++;
}
}

Again, I don’t know what your data structures look like – but the
most likely problem is that part way through the loop CaptBuffer> [0]
becomes a point to invalid memory. (Assuming NEW_FILE is someting
reasonable of course.) (I also assume that ArvBYTE has a size of
one byte.)

Also, it strikes me that:
for( k=0; k<captInfo[i].w; k++ )
{
fwrite( CaptBuffer[i][0], Bytes_Per_Pixel, 1, NEW_FILE );
CaptBuffer[i][0]++;
}

Could be far more efficiently written as:
fwrite( CaptBuffer[i][0], Bytes_Per_Pixel, captInfo[i].w, NEW_FILE );
CaptBuffer[i][0] += captInfo[i].w; /* if the pointer side effect needed
*/

-David

Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com_
\

I am not sure how the memory is allocated at captInfo.lin_mem, this is
done by the manufacturer’s code on the frame grabber. I am however
capable of reading, byte by byte, starting at captInfo.lin_mem and
printing out the value of each pixel on the screen. The values are
correct, telling me that I am reading the values properly. However, when
I go to save these values in a file, after 4kB I get a memory fault.
Thanks again, Mathew.


David Bacon wrote:

How is the memory at captInfo> _.lin_mem allocated?

By the way (and this is mostly to David Gibbs), the short fwrites
in the original example actually would have made sense if the
frame grabber had stored, say, 4 bytes per pixel but only 3
(or Bytes_Per_Pixel) bytes for each pixel were wanted in the file.

There are no limits on write() or fwrite() that would be applicable
here.

dB
\


m2asselin@yahoo.com > (Mathew Asselin) wrote ~ 28 Jul 2004 19:12:40 GMT:
The data strucutre is that of an image: the framegrabber stores the data
for each image as a string of bytes ( 1 byte per pixel ), where the string
of bytes begins at CaptInfo.lin_mem.

I am therefore trying to copy this linear vector of bytes into a file,
which can be read off-line for image processing. The problem arises when
I have a picture that is larger than 4000 pixels (=4kB).

The code works fine for pictures smaller than 4kB (although as you have
both put it, and now that I look at it again, it is rather inefficient
code), however the second I increase the picture size, the memory fault
occurs.

Do you know if there are any limitations with respect to the size of a
file when using fwrite()? Does write() have less limitations?

Thanks again, this is of much help to me.
Mathew.


David Gibbs wrote:

Mathew Asselin <> m2asselin@yahoo.com> > wrote:
Hi David,

the framegrabber stores an image from a camera and points to the
location
in memory by CaptInfo> .lin_mem. I then read each pixel, and attempt
to
write them all to a file, individually, using fwrite().
Bytes_Per_Pixel
is set to 1.

ArvBYTE CaptBuffer[2][2];
for( j=0; j .h; j++ )
{
CaptBuffer> [0] = (ArvBYTE
)captInfo> .lin_mem + j *
captInfo> .pitch;
for( k=0; k .w; k++ )
{
fwrite( CaptBuffer> [0], Bytes_Per_Pixel, 1, NEW_FILE );
CaptBuffer> [0]++;
}
}

Again, I don’t know what your data structures look like – but the
most likely problem is that part way through the loop CaptBuffer> [0]
becomes a point to invalid memory. (Assuming NEW_FILE is someting
reasonable of course.) (I also assume that ArvBYTE has a size of
one byte.)

Also, it strikes me that:
for( k=0; k<captInfo[i].w; k++ )
{
fwrite( CaptBuffer[i][0], Bytes_Per_Pixel, 1, NEW_FILE );
CaptBuffer[i][0]++;
}

Could be far more efficiently written as:
fwrite( CaptBuffer[i][0], Bytes_Per_Pixel, captInfo[i].w, NEW_FILE
);
CaptBuffer[i][0] += captInfo[i].w; /* if the pointer side effect
needed
*/

-David

Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com_
\

So the next logical question is, how is NEW_FILE defined? (How is
it declared and how initialized?)

dB


m2asselin@yahoo.com (Mathew Asselin) wrote ~ 28 Jul 2004 19:34:19 GMT:

I am not sure how the memory is allocated at captInfo.lin_mem, this is
done by the manufacturer’s code on the frame grabber. I am however
capable of reading, byte by byte, starting at captInfo.lin_mem and
printing out the value of each pixel on the screen. The values are
correct, telling me that I am reading the values properly. However, when
I go to save these values in a file, after 4kB I get a memory fault.
Thanks again, Mathew.


David Bacon wrote:

How is the memory at captInfo> _.lin_mem allocated?

By the way (and this is mostly to David Gibbs), the short fwrites
in the original example actually would have made sense if the
frame grabber had stored, say, 4 bytes per pixel but only 3
(or Bytes_Per_Pixel) bytes for each pixel were wanted in the file.

There are no limits on write() or fwrite() that would be applicable
here.

dB
\


m2asselin@yahoo.com > (Mathew Asselin) wrote ~ 28 Jul 2004 19:12:40 GMT:
The data strucutre is that of an image: the framegrabber stores the data
for each image as a string of bytes ( 1 byte per pixel ), where the string
of bytes begins at CaptInfo.lin_mem.

I am therefore trying to copy this linear vector of bytes into a file,
which can be read off-line for image processing. The problem arises when
I have a picture that is larger than 4000 pixels (=4kB).

The code works fine for pictures smaller than 4kB (although as you have
both put it, and now that I look at it again, it is rather inefficient
code), however the second I increase the picture size, the memory fault
occurs.

Do you know if there are any limitations with respect to the size of a
file when using fwrite()? Does write() have less limitations?

Thanks again, this is of much help to me.
Mathew.


David Gibbs wrote:

Mathew Asselin <> m2asselin@yahoo.com> > wrote:
Hi David,

the framegrabber stores an image from a camera and points to the
location
in memory by CaptInfo> .lin_mem. I then read each pixel, and attempt
to
write them all to a file, individually, using fwrite().
Bytes_Per_Pixel
is set to 1.

ArvBYTE CaptBuffer[2][2];
for( j=0; j .h; j++ )
{
CaptBuffer> [0] = (ArvBYTE
)captInfo> .lin_mem + j *
captInfo> .pitch;
for( k=0; k .w; k++ )
{
fwrite( CaptBuffer> [0], Bytes_Per_Pixel, 1, NEW_FILE );
CaptBuffer> [0]++;
}
}

Again, I don’t know what your data structures look like – but the
most likely problem is that part way through the loop CaptBuffer> [0]
becomes a point to invalid memory. (Assuming NEW_FILE is someting
reasonable of course.) (I also assume that ArvBYTE has a size of
one byte.)

Also, it strikes me that:
for( k=0; k<captInfo[i].w; k++ )
{
fwrite( CaptBuffer[i][0], Bytes_Per_Pixel, 1, NEW_FILE );
CaptBuffer[i][0]++;
}

Could be far more efficiently written as:
fwrite( CaptBuffer[i][0], Bytes_Per_Pixel, captInfo[i].w, NEW_FILE
);
CaptBuffer[i][0] += captInfo[i].w; /* if the pointer side effect
needed
*/

-David

Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com_




\

David Bacon <dbacon@qnx.com> wrote:

David, why do you find it necessary to assume ArvBYTE is one byte?

Cause, if not you get into some messy issues.

One: pointer arithmetic – is it coming out right?
Two: efficiency, my “better” fwrite wouldn’t work.
Three: byte order (BE vs LE – which byte of the captured data are you
really getting?)


Wny could it not be 3, or 4? You might think have to fix your code
to say "CaptBuffer> [0] += capInfo> .w * Bytes_Per_Pixel;" then too,
of course, but actually you could just delete that CaptBuffer> [0]
update line entirely.

Anyway, I find the whole CaptBuffer declaration strange. The whole
example would have been equivalent to this:

for (j=0; j .w; k++) {
ArvBYTE *CaptLine = (ArvByte *)captInfo> .lin_mem +
j * captInfo> .pitch;
fwrite (CaptLine, Bytes_Per_Pixel, captInfo> , NEW_FILE);

_If there are no “extra” non-captured pixels at the end of the capture
buffer. Sometimes there are.

-David

Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com_

I am simply opening NEW_FILE as follows:

FILE *NEW_FILE;
NEW_FILE = fopen( “picture.raw”, “w”);

Mathew.

David Bacon wrote:

So the next logical question is, how is NEW_FILE defined? (How is
it declared and how initialized?)

dB


m2asselin@yahoo.com > (Mathew Asselin) wrote ~ 28 Jul 2004 19:34:19 GMT:
I am not sure how the memory is allocated at captInfo.lin_mem, this is
done by the manufacturer’s code on the frame grabber. I am however
capable of reading, byte by byte, starting at captInfo.lin_mem and
printing out the value of each pixel on the screen. The values are
correct, telling me that I am reading the values properly. However, when
I go to save these values in a file, after 4kB I get a memory fault.
Thanks again, Mathew.


David Bacon wrote:

How is the memory at captInfo> _.lin_mem allocated?

By the way (and this is mostly to David Gibbs), the short fwrites
in the original example actually would have made sense if the
frame grabber had stored, say, 4 bytes per pixel but only 3
(or Bytes_Per_Pixel) bytes for each pixel were wanted in the file.

There are no limits on write() or fwrite() that would be applicable
here.

dB
\


m2asselin@yahoo.com > (Mathew Asselin) wrote ~ 28 Jul 2004 19:12:40 GMT:
The data strucutre is that of an image: the framegrabber stores the
data
for each image as a string of bytes ( 1 byte per pixel ), where the
string
of bytes begins at CaptInfo.lin_mem.

I am therefore trying to copy this linear vector of bytes into a file,
which can be read off-line for image processing. The problem arises
when
I have a picture that is larger than 4000 pixels (=4kB).

The code works fine for pictures smaller than 4kB (although as you have
both put it, and now that I look at it again, it is rather inefficient
code), however the second I increase the picture size, the memory fault
occurs.

Do you know if there are any limitations with respect to the size of a
file when using fwrite()? Does write() have less limitations?

Thanks again, this is of much help to me.
Mathew.


David Gibbs wrote:

Mathew Asselin <> m2asselin@yahoo.com> > wrote:
Hi David,

the framegrabber stores an image from a camera and points to the
location
in memory by CaptInfo> .lin_mem. I then read each pixel, and
attempt
to
write them all to a file, individually, using fwrite().
Bytes_Per_Pixel
is set to 1.

ArvBYTE CaptBuffer[2][2];
for( j=0; j .h; j++ )
{
CaptBuffer> [0] = (ArvBYTE
)captInfo> .lin_mem + j *
captInfo> .pitch;
for( k=0; k .w; k++ )
{
fwrite( CaptBuffer> [0], Bytes_Per_Pixel, 1, NEW_FILE );
CaptBuffer> [0]++;
}
}

Again, I don’t know what your data structures look like – but the
most likely problem is that part way through the loop
CaptBuffer> [0]
becomes a point to invalid memory. (Assuming NEW_FILE is someting
reasonable of course.) (I also assume that ArvBYTE has a size of
one byte.)

Also, it strikes me that:
for( k=0; k<captInfo[i].w; k++ )
{
fwrite( CaptBuffer[i][0], Bytes_Per_Pixel, 1, NEW_FILE );
CaptBuffer[i][0]++;
}

Could be far more efficiently written as:
fwrite( CaptBuffer[i][0], Bytes_Per_Pixel, captInfo[i].w,
NEW_FILE
);
CaptBuffer[i][0] += captInfo[i].w; /* if the pointer side effect
needed
*/

-David

Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com_




\

Mathew Asselin <m2asselin@yahoo.com> wrote:

Do you know if there are any limitations with respect to the size of a
file when using fwrite()? Does write() have less limitations?

No, there aren’t any such restrictions. Well, not quite true, there
is a restriction on file size at the 2G mark. And, these things do
take size_t parameters that are signed integers, again, a 2G limitation.

There is nothing at the 4K-8K range.

For example:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define TEST_SIZE 8192

int main(int argc, char *argv[])
{
char *ptr, *ptr1;
FILE *outfile;
int i;

ptr = malloc( TEST_SIZE );
if( !ptr )
{
perror(“malloc”);
exit(1);
}
memset(ptr, ‘a’, TEST_SIZE );

outfile = fopen( “/tmp/blah”, “w” );

if( !outfile )
{
perror(“fopen”);
exit(1);
}
ptr1 = ptr;
for( i=0; i<TEST_SIZE; i++ )
{
fwrite( ptr1, 1, 1, outfile );
ptr1++;
}
fclose( outfile );
free(ptr);
}

Uses the exact same structure around fwrite – one byte at a time,
writing out 8K of single bytes, and it works happily – generating
an 8K output file.

So, I repeat, the error is in the pointers, or your manipulation
of them.

In fact, are you sure the crash is even in this block of code?
Have you tried getting a core dump and loading it into a
debugger to see where it actually died?

-David

Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com

David Gibbs <dagibbs@qnx.com> wrote:

Mathew Asselin <> m2asselin@yahoo.com> > wrote:
Do you know if there are any limitations with respect to the size of a
file when using fwrite()? Does write() have less limitations?

No, there aren’t any such restrictions. Well, not quite true, there
is a restriction on file size at the 2G mark. And, these things do
take size_t parameters that are signed integers, again, a 2G limitation.

As a side issue, if you do need files > 2GB, I have put together a “big
file extensions” driver that does this for you…

http://www.parse.com/samples/manpages/bfe.html

Cheers,
-RK


[If replying via email, you’ll need to click on the URL that’s emailed to you
afterwards to forward the email to me – spam filters and all that]
Robert Krten, PDP minicomputer collector http://www.parse.com/~pdp8/

Mathew Asselin <m2asselin@yahoo.com> wrote:
MA > I am simply opening NEW_FILE as follows:

MA > FILE *NEW_FILE;
MA > NEW_FILE = fopen( “picture.raw”, “w”);

MA > Mathew.

MA > David Bacon wrote:

So the next logical question is, how is NEW_FILE defined? (How is
it declared and how initialized?)

dB

I wonder is you just ran out of stack space.

Can you try increasing the stack size?

How do I increase the stack size?
Matt.


Bill Caroselli wrote:

Mathew Asselin <> m2asselin@yahoo.com> > wrote:
MA > I am simply opening NEW_FILE as follows:

MA > FILE *NEW_FILE;
MA > NEW_FILE = fopen( “picture.raw”, “w”);

MA > Mathew.

MA > David Bacon wrote:

So the next logical question is, how is NEW_FILE defined? (How is
it declared and how initialized?)

dB

I wonder is you just ran out of stack space.

Can you try increasing the stack size?

Mathew Asselin <m2asselin@yahoo.com> wrote:
MA > How do I increase the stack size?
MA > Matt.


Use the ‘-N stack_size’ option to gcc/qcc.

Sorry, I don’t use the IDE so I’m not sure where you put it there.

Bill Caroselli <qtps@earthlink.net> wrote:

Mathew Asselin <> m2asselin@yahoo.com> > wrote:
MA > How do I increase the stack size?
MA > Matt.



Use the ‘-N stack_size’ option to gcc/qcc.

Default is 512K.

Sorry, I don’t use the IDE so I’m not sure where you put it there.

Right click on the project, properties, QNX C/C++ Project properties
if it is a QNX project.

-David

Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com

On 28 Jul 2004 14:28:07 GMT, Mathew Asselin <m2asselin@yahoo.com> wrote:

Hello,

I am trying to save an image from a framegrabber to a file on my hard
disk. I am using the fwrite() command, however, when the size gets
larger
than 4000 bytes, I get a memory fault. Does anyone know if this is due
to
a memory shortage, or parameter that I have to set?

4k sounds like a page
I’ll bet the problem is in reading from memory, not writing.
How is the FG’s memory mapped (mmap_* call).


Using Opera’s revolutionary e-mail client: http://www.opera.com/m2/