cout causing functions to inexplicably fail

I'm trying to make a simple program to read back a large number of files and parse the data. To help me I've created a few functions that I can call to speed up the process. Unfortunately, if I use cout anywhere near where the function is called, or in the function itself, it causes my program to crash. Here's the code:
1
2
3
4
5
6
7
size_t read_32bitBE(int off, fstream &infile)//reads a 32 bit value at offset and converts it from BE to LE
{
    uint8_t buf[4];
    cout<<"here";
    read_streamfile(buf,off,4,infile);
    return (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | (buf[3]);//puts buf into BE
}


This causes read_streamfile() to crash after about 3 runs, but if I remove "cout<<"here";" it works fine.

Here's read_streamfile():

1
2
3
4
5
6
7
8
9
10
11
int read_streamfile(uint8_t *buf,int off,int siz,fstream &infile)//reads a certain amount of data from a certain offset and returs the amount read
{
    char* buf2;
    infile.seekg(off);
    //crashes here, when calling infile.read()
    infile.read(buf2,siz);
    memcpy(buf,buf2,siz);
    if(buf2) return siz;
    return 0;
}


I've also found that if I call the function too many times it will eventually crash if there are any couts in main() as well... Anyways, If anyone could help me out I'd greatly appreciate it.

Thanks,
-Alex
Last edited on
cout has nothing to do with it.

You're corrupting the heap in read_streamfile:

1
2
3
4
5
6
    char* buf2;  // buf2 is a pointer, but you don't say where it points to

    infile.read(buf2,siz);  // so this is just reading data and putting it in random areas in memory

    if(buf2) return siz;  // and I don't know what you expect this to do, but you never set buf2 to
         // anything, so this is meaningless. 


Another question here is why are you using buf2 at all? All you do is copy it to buf. Why not just read to buf directly?
I use buf2 because I can't read() directly into a uint8_t. If I try to cast it as a char, I get an error about losing precision. Also, read() only accepts a pointer. I'm reading from infile into buf2, so shouldn't it allocate memory automatically? Or should I malloc some space? Also, the last part is just making sure that buf2 exists so I don't tell the program it copied successfully when it didn't. Anyways, I know cout is messing something up since it works fine without it... for about 10 calls. :(
Last edited on
I use buf2 because I can't read() directly into a uint8_t. If I try to cast it as a char, I get an error about losing precision.


At worst you'd get a warning. You wouldn't get an error unless you're casting improperly.

This will work just fine, and is what I recommend:

 
infile.read( reinterpret_cast<char*>(buf), siz );


Also, read() only accepts a pointer.


Right, but the pointer you give it has to actually point to something.

I'm reading from infile into buf2, so shouldn't it allocate memory automatically?


Nope.

Or should I malloc some space?


Don't use malloc in C++. Use new[] if you have to.

That's one option, but again it's pointless here because you could just read to buf directly.


Pointers and buffers are two different things. A buffer is actual allocated space in memory that can hold data. A pointer is just an address. Often times, a pointer points to the first byte of a buffer:

1
2
3
uint8_t abuffer[100];  // a buffer of 100 uint8_t's

uint8_t* ptr = abuffer; // a pointer that points to the first uint8_t of that buffer 


Functions like read() expect the pointer you give them to be pointing to a buffer. That way the data they read goes into that buffer.

You're not giving it any buffer. You're just giving it some random address because you never initialized your pointer. So read() is just taking the data and putting it wherever, which is very dangerous (and is what was causing the program to crash)

Also, the last part is just making sure that buf2 exists so I don't tell the program it copied successfully when it didn't.


That doesn't really make sense though. You're checking to see if buf2 is null, but you never set buf2 to anything. That'd be like doing this:

1
2
3
4
5
6
int apples;

if(apples == 5)
{
  cout << "we have 5 apples";
}


Will that if() statement be true? Who knows! You never say how many apples there are, so how can you expect the if() statement to have any significance?



EDIT:

Anyways, I know cout is messing something up since it works fine without it... for about 10 calls. :(


NO, cout has absolutely nothing to do with it

Trust me. It's heap corruption. Heap corruption makes your program do all sorts of crazy, unpredictable things.
Last edited on
Ah, thank you very much! I'd never heard of reinterpret_cast before. Now that I'm copying directly into buf it's working great! I also set buf to zero ahead of time now, and then check it against that instead. Again, thanks a lot, not only for the cast solution, but for the quick tutorial on pointers as well!

-Alex
Last edited on
Topic archived. No new replies allowed.