3

I am looking for some ways to advance pointers to the beginning of files in compressed archives.

I have a character pointer to the beginning of the file that has been read into memory. The archive directory contains the offsets of each file. Is it legal/recommended to say:

char* beginning; //Imagine this is assigned to the beginning of the file in memory
int file1OffsetBytes = 1000; // Imagine the first file is 1000 bytes into the file

char* file1 = beginning + file1OffsetBytes;

Is this a bad idea? What is another way to do this?

Satchmo Brown
  • 1,409
  • 2
  • 20
  • 47
  • the (compressed) file is already 100% in memory? what kind of compression is used? – Najzero Jan 22 '13 at 06:58
  • 3
    It is legal or not depends on the content at which the pointer points after the advancing. If it points to something invalid.It will result in UB. Why don't you use stream classes? [istream::seekg](http://www.cplusplus.com/reference/istream/istream/seekg/) – Alok Save Jan 22 '13 at 07:00
  • @AlokSave +1, You should turn that comment about istream into an answer. – us2012 Jan 22 '13 at 07:05
  • @us2012: Using stream classes is the obvious & right way to do this.But I am not sure what the OP is trying to achieve here.To be honest the question seems vague & incomplete to me. – Alok Save Jan 22 '13 at 07:08
  • This doesn't seem to be about files or c++. – Peter Wood Jan 22 '13 at 08:36

2 Answers2

4

that is quite Ok. You only have to take care about out of bounds jumps... and one more thing: here is an size_t or ssize_t type usually used for memory buffers offset.

zaufi
  • 6,811
  • 26
  • 34
2

Adding to a pointer (or subtracting from it) is legal as long as the resulting pointer still points to an element in the array or to the non-existent element right after the last existing one. Needless to say, you can only dereference a pointer pointing to an existing element and the element has to have been initialized if you're reading it through the pointer.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180