2

I'm trying to read from a binary file, using fstream, some data I have previously written there.

The problem is that after getting to the end of the function the message in the subject is shown

The code is the following:

ifstream in("contrib.bin", ios::in | ios::binary );

char *nume, dim;
in.read((char*)&dim, sizeof(int));
nume = new char[dim + 1];
in.read(nume, dim);
nume[dim] = '\0';
double imp;
in.read((char*)&imp, sizeof(double));

delete [] nume;

Now, I've done my homework and looked for this issue, but the other people who faced it had arrays, whereas my variable is a simple char.

Can someone point me to the right direction, please?

hate-engine
  • 2,300
  • 18
  • 26
Cristina_eGold
  • 1,463
  • 2
  • 22
  • 41
  • 1
    Instead of raw arrays and `new` and `delete`, use a `std::vector` or a `std::string`, depending on how you intend to use the data. To pass a pointer to the first byte in a vector or a string `v`, use `&v[0]`. Note however that as opposed to the current code this is UB for size 0, so you need to check for size 0 (if that can occur). – Cheers and hth. - Alf Jan 11 '13 at 16:53

3 Answers3

13

The code

char dim;
in.read((char*)&dim, sizeof(int));

defines a 1 byte char then reads sizeof(int) bytes (which is likely to be greater that 1) into it. This is invalid and may corrupt your stack.

If you need to read sizeof(int) bytes, declare dim as int. Otherwise, change the number of bytes you read to 1. It'd be best if you also used sizeof(dim) to ensure that you only read as many bytes as you've provided storage for:

in.read((char*)&dim, sizeof(dim));
simonc
  • 41,632
  • 12
  • 85
  • 103
  • 4
    It *could* corrupt the stack. For all we know it could also send the computer flying across the room. ;-) – netcoder Jan 11 '13 at 16:30
  • 2
    "reads 4 bytes" is not necessarily true, since the sizeof(int) depends on the platform in more ways than one (size of a byte and amount of bits used for an int). – Agentlien Jan 11 '13 at 16:32
  • @netcoder I know this isn't strictly true in general but since the question mentioned a stack corruption error I thought it was a fair assumption here. I've updated my answer along the lines you were suggesting though. – simonc Jan 11 '13 at 16:33
  • Much better. :) You get a tasty +1 to munch on. – Agentlien Jan 11 '13 at 16:35
  • "(which will be greater that 1)" is not necessarily true. ordinarily it is true, and in the OP's case it is overwhelmingly probably true. but not as a general statement. – Cheers and hth. - Alf Jan 11 '13 at 16:50
  • @Cheersandhth.-Alf http://stackoverflow.com/a/589684/311966 implies that int will always be at least 2 bytes (assuming 8 bits per byte). Or am I misunderstanding? – simonc Jan 11 '13 at 16:57
  • 1
    @simonc You can't assume eight-bit bytes. They may be larger. If `CHAR_BIT >= 16`, `sizeof(int)` could be 1. – Daniel Fischer Jan 11 '13 at 17:04
7

in.read((char*)&dim, sizeof(int)); is not correct, dim only holds sizeof(char) which is one, but you're attempting to read sizeof(int) into it.

All gloves are off after this.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
2

Well you define a character then read in the size of an int. That would be the first issue

char *nume, dim;
in.read((char*)&dim, sizeof(char));
rerun
  • 25,014
  • 6
  • 48
  • 78