reinterpret_cast可移植性/对齐问题 [英] reinterpret_cast portability/alignment issues

查看:92
本文介绍了reinterpret_cast可移植性/对齐问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,


我有一些代码可以从光盘中读取未格式化的数据并将其解释为无符号整数块。为了实现效率

(我的应用程序非常重要的是代码是
速度优化[*])我使用reinterpret_cast来为一个字符块读取别名
来自光盘的
作为整数块单词。我现有的代码(参见下面的
简化代码)似乎在平台上运行得很好

可供我使用,但我想实现最大的可移植性并且

非常感谢任何关于可能的可移植性(以及内存

对齐)问题的评论。我的方法可能会产生这些问题。


我明白某些意义上所有的赌注在使用

reinterpret_cast时都会关闭,因为细节将是实现定义但我看不到

为什么我的别名方法(带有合适的检查)应该导致任何问题。


关于内存对齐以便有效访问我的内存缓冲区,我不是没有b $ b确定全局运算符new如何处理内存对齐(I不应该认为

标准对此有什么要说的),所以我可能准备好(b)手动和b $ b。对齐我的内存块 - 实际上不确定怎么做

这个或者用这种方法可以实现多少便携性) - 或者

(ii)使用malloc / memalign,无论如何来自(或更确切地说是a)C库,

其中可以更精确地指定对齐行为(例如GNU C

库)。当然,这种方法会有可移植性问题。


从本质上讲,我的问题似乎涉及到

效率和可移植性之间的潜在权衡。再次,任何评论,建议,替代

接近欢迎。


以下简化代码。

[*]代码是愤怒中使用的是从统计应用程序中获取大量

二进制数据文件中的随机数...我已经确定

随机数操作作为时间瓶颈。


---开始代码----


// main.cpp


#include< ; iostream>

#include< fstream>

#include< string>


//从光盘读取未格式化的数据并解释为无符号整数


int main()

{

//单词类型(无符号整数类型 - 季节尝试)

typedef unsigned long word_t;


//每个字符的字符数

const size_t wordsize = sizeof(word_t );

std :: cout<< " wordsize =" <<单词大小<< ''\ n'';


//数据文件(我们用ios :: ate打开,因为我们想先获取文件大小)

const std :: string bitsfile =" bits.dat";

std :: ifstream bfs(bitsfile.c_str(),std :: ios :: in | std :: ios :: binary | std :: ios :: ate);

if(!bfs.is_open()){

std :: cerr<< 未能打开文件 << bitsfile<< ''\ n'';

返回1;

}


//获取文件大小

const size_t cfilesize = bfs.tellg();

std :: cout<< " filesize =" << cfilesize<< " chars \ n" ;;


//要读取的字节数(字符数) - 不得大于文件大小!

const size_t cblocksize = 128;

std :: cout<< 块大小(char)= << cblocksize<< ''\ n'';

if(cblocksize> cfilesize){

std :: cerr<< 块大小不能大于文件大小\ n;

返回1;

}


//数字要写的字必须可以被字大小整除!

const size_t wblocksize = cblocksize / wordsize;

std :: cout<< 块大小(单词)= << wblocksize<< ''\ n'';

if(wblocksize * wordsize!= cblocksize){

std :: cerr<< 块大小必须可以被字大小整除\ n;

返回1;

}


// char读取缓冲区

//评论? alignement问题?

char * const cbuf(new char [cblocksize]);


//用于将别名写入char缓冲区的字缓冲区

//评论?这有多便携......?

//评论? alignement问题?

const word_t * const wbuf(reinterpret_cast< word_t * const>(cbuf));


//寻找文件的开头和读取块chars

bfs.seekg(0);

bfs.read(cbuf,cblocksize);

bfs.close();


//写出单词

for(size_t i; i< wblocksize; ++ i)std :: cout<< word << i<< " = << wbuf [i]<< ''\ n'';


//清理

删除[] cbuf;


返回0;

}


---结束代码----


-

Lionel B

解决方案

您好


我没有查看您的所有代码但是一些快节点

我会把它作为一个''字''数组而不是''字符''数组只是为了使

确定它在右边边界。你可以将reinterpret_cast改为char *和

,因为它已被正确分配。


通常可以设置编译器选项。在visual studio中,您可以为您的项目设置

alignement,或者在不同的

位置添加pragma ...指令。


如果你从具有不同架构的系统中编写和读取

(大/小端)你的代码当然会失败


BTW getfilesize是否真的有用(不应该) 你把get ptr重新定位到

文件的结尾了吗?


问候,Ron AF Greve

< a rel =nofollowhref =http://moonlit.xs4all.nltarget =_ blank> http://moonlit.xs4all.nl


Lionel B < me@privacy.netwrote in message

news:KK ***************** @ newsfe5-win.ntli.net ...


问候,


我有一些代码可以从光盘读取未格式化的数据并解释

它作为无符号整数的块。为了实现效率

(我的应用程序非常重要的是代码是
速度优化[*])我使用reinterpret_cast来为一个字符块读取别名
来自光盘的
作为整数块单词。我现有的代码(参见下面的
简化代码)似乎在平台上运行得很好

可供我使用,但我想实现最大的可移植性并且

非常感谢任何关于可能的可移植性(以及内存

对齐)问题的评论。我的方法可能会产生这些问题。


我明白某些意义上所有的赌注在使用

reinterpret_cast时都会关闭,因为细节将是实现定义但我看不到

为什么我的别名方法(带有合适的检查)应该导致任何问题。


关于内存对齐以便有效访问我的内存缓冲区,我不是没有b $ b确定全局运算符new如何处理内存对齐(I不应该认为

标准对此有什么要说的),所以我可能准备好(b)手动和b $ b。对齐我的内存块 - 实际上不确定怎么做

这个或者用这种方法可以实现多少便携性) - 或者

(ii)使用malloc / memalign,无论如何来自(或更确切地说是a)C库,

其中可以更精确地指定对齐行为(例如GNU C

库)。当然,这种方法会有可移植性问题。


从本质上讲,我的问题似乎涉及到

效率和可移植性之间的潜在权衡。再次,任何评论,建议,替代

接近欢迎。


以下简化代码。
[*]愤怒中使用的代码是为统计应用程序从巨大的

二进制数据文件中获取随机数...我已经确定

随机数操作作为时间瓶颈。

---开始代码----


// main.cpp


#include< iostream>

#include< fstream>

#include< string>


//从光盘读取未格式化的数据并解释为无符号数据整数


int main()

{

//单词类型(无符号整数类型 - 品味季节)

typedef unsigned long word_t;


//每个字符的字符数

const size_t wordsize = sizeof(word_t);

std :: cout<< " wordsize =" <<单词大小<< ''\ n'';


//数据文件(我们用ios :: ate打开,因为我们想先获取文件大小)

const std :: string bitsfile =" bits.dat";

std :: ifstream

bfs(bitsfile.c_str(),std :: ios :: in | std :: ios :: binary | std :: ios :: ate);

if(!bfs.is_open()){

std :: cerr<< 未能打开文件 << bitsfile<< ''\ n'';

返回1;

}


//获取文件大小

const size_t cfilesize = bfs.tellg();

std :: cout<< " filesize =" << cfilesize<< " chars \ n" ;;


//要读取的字节数(字符数) - 不得大于文件大小!

const size_t cblocksize = 128;

std :: cout<< 块大小(char)= << cblocksize<< ''\ n'';

if(cblocksize> cfilesize){

std :: cerr<< 块大小不能大于文件大小\ n;

返回1;

}


//数字要写的字必须可以被字大小整除!

const size_t wblocksize = cblocksize / wordsize;

std :: cout<< 块大小(单词)= << wblocksize<< ''\ n'';

if(wblocksize * wordsize!= cblocksize){

std :: cerr<< 块大小必须可以被字大小整除\ n;

返回1;

}


// char读取缓冲区

//评论? alignement问题?

char * const cbuf(new char [cblocksize]);


//用于将别名写入char缓冲区的字缓冲区

//评论?这有多便携......?

//评论? alignement问题?

const word_t * const wbuf(reinterpret_cast< word_t * const>(cbuf));


//寻找文件的开头和读取块chars

bfs.seekg(0);

bfs.read(cbuf,cblocksize);

bfs.close();


//写出单词

for(size_t i; i< wblocksize; ++ i)std :: cout<< word << i<< " = <<

wbuf [i]<< ''\ n'';


//清理

删除[] cbuf;


返回0;

}


---结束代码----


-

Lionel B



Lionel B写道:


> ;

//用于阅读的char缓冲区

//评论? alignement问题?

char * const cbuf(new char [cblocksize]);


//用于将别名写入char缓冲区的字缓冲区

//评论?这有多便携......?

//评论? alignement问题?

const word_t * const wbuf(reinterpret_cast< word_t * const>(cbuf));



如果你正在施法从新的直接返回(如你在这里),它

应该没问题。新的char []需要返回一个普遍的&b
对齐的东西,只要你分配至少与你要对齐的

的东西。


当然,通用的保证是你可以随时将字符别名为b
。因此,不是分配一个/或
char的缓冲区,而是可以分配其他东西的缓冲区,然后将
转换为char来读取/复制数据。 />


哎呀不介意BTW错过了吃饭和评论:-(


问候,Ron AF Greve
http://moonlit.xs4all.nl


" Moonlit" news moonlit xs4all nlwrote in message

news:45 ********************* @ news.xs4all.nl ...





我没有查看你的所有代码,但有些快速节点

我会把它作为一个''字''数组而不是''字符''数组,只是为了使

确定它在正确的边界。您可以将reinterpret_cast重新设置为char *

,现在它已正确分配。


通常可以设置编译器选项。在visual studio中可以

为你的项目设置alignement或在不同的

地方添加pragma ...指令。


如果你写和读来自具有不同架构的系统

(大/小端)您的代码当然会失败


BTW getfilesize是否真的有效(不应该是你)将get ptr

重新定位到文件的末尾)?


问候,Ron AF Greve

http://moonlit.xs4all.nl


" Lionel B" ; < me@privacy.netwrote in message

news:KK ***************** @ newsfe5-win.ntli.net ...


>问候,

我有一些代码可以从光盘中读取未格式化的数据并将其解释为无符号整数块。为了实现效率
(对于我的应用来说,代码是速度优化的[*]是非常必要的)我使用reinterpret_cast来代替一组字符
读取
从盘中作为整数单词的块。我现有的代码(参见下面的简化代码)似乎在我可以使用的平台上运行良好,但我希望实现最大程度的可移植性,
将非常感谢任何关于可能由我的方法引起的可移植性(以及内存对齐)问题的评论。

我知道在某种意义上,当使用
reinterpret_cast时,所有的赌注都会被取消。是实现定义但我不能看到
为什么我的别名方法(合适的检查)会导致任何问题。

关于内存对齐,以便有效地访问我的内存缓冲区,我不确定全局运算符new如何处理内存对齐(我不应该认为标准对此有什么要说的),所以我可能准备
(i)手动调整我的内存块 - 实际上并不确定如何使用这种方法来实现这个或多少可移植性 - 或者
(ii)使用malloc / memalign,无论如何,来自(或者更确切地说是aC库,其中可以更精确地指定对齐行为(例如GNU C
库)。当然,这种方法会带来可移植性问题。

从本质上讲,我的问题似乎涉及到效率和可移植性之间的潜在折衷。同样,任何评论,建议,替代方法都欢迎。

以下简化代码。

[*]愤怒中使用的代码是抓取随机数来自用于统计应用程序的巨大的二进制数据文件...我已经确定随机数操作是一个时间瓶颈。

---开始代码----

// main.cpp

#include< iostream>
#include< fstream>
#include< string>
< br //>从光盘中读取未格式化的数据并将其解释为无符号整数

int main()
//单词类型(无符号整数类型 - 品味季节)
typedef unsigned long word_t;

//每个字符的字符数
const size_t wordsize = sizeof(word_t);
std :: cout<< " wordsize =" <<单词大小<< ''\ n'';

//数据文件(我们用ios :: ate打开,因为我们想先获取文件大小)
const std :: string bitsfile =" bits.dat";
std :: ifstream
bfs(bitsfile.c_str(),std :: ios :: in | std :: ios :: binar y | std :: ios :: ate) ;
if(!bfs.is_open()){
std :: cerr<< 未能打开文件 << bitsfile<< ''\ n'';
返回1;
}
//获取文件大小
const size_t cfilesize = bfs.tellg();
std :: cout<< " filesize =" << cfilesize<< " chars \ n" ;;

//要读取的字节数(字符数) - 不能大于文件大小!
const size_t cblocksize = 128;
std :: cout<< 块大小(char)= << cblocksize<< ''\ n'';
if(cblocksize> cfilesize){
std :: cerr<< 块大小不能大于文件大小\ n;
返回1;
}
//要写的字数 - 必须可以按字大小整除!
const size_t wblocksize = cblocksize / wordsize;
std :: cout<< 块大小(单词)= << wblocksize<< ''\ n'';
if(wblocksize * wordsize!= cblocksize){
std :: cerr<< 块大小必须可以被字大小整除\ n;
返回1;
}
//用于读取的字符缓冲区
//评论? alignement问题?
char * const cbuf(new char [cblocksize]);

//用于将别名写入char缓冲区的字缓冲区
//评论?这有多便携......?
//评论? alignement问题?
const word_t * const wbuf(reinterpret_cast< word_t * const>(cbuf));

//寻找文件的开头并读取字符块
bfs。 seekg(0);
bfs.read(cbuf,cblocksize);
bfs.close();

//写出单词
for(size_t i;我< wblocksize; ++ i)std :: cout<< word << i<< " = <<
wbuf [i]<< ''\ n'';

//清理
删除[] cbuf;

返回0;
}

---结束代码----
- Lionel B




Greetings,

I have some code that is to read unformatted data from disc and interpret
it as blocks of unsigned integers. In an attempt to achieve efficiency
(it is pretty essential for my application that the code be
speed optimized[*]) I use reinterpret_cast to alias a block of chars read
in from disc as a block of integer "words". My existing code (see
simplified code below) appears to work well enough on the platforms
available to me, but I would like to achieve maximum portability and would
really appreciate any commentary on possible portability (and also memory
alignment) issues that might arise from my approach.

I understand that in some sense all bets are off when using
reinterpret_cast as details will be implementation-defined but I can''t see
why my aliasing method (with suitable checks) should cause any problems.

As to memory alignment for efficient access to my memory buffer, I''m not
sure how global operator new handles memory alignment (I shouldn''t think
the standard has anything to say about this), so I might be prepared to
either (i) "manually" align my memory blocks - not actually sure how to do
this or how much portability might be achieved with this approach) - or
(ii) use malloc / memalign, whatever, from the (or rather "a") C library,
where alignment behaviour might be more precisely specified (eg the GNU C
library). Of course there will be portability issues with this approach.

In essence my problem seems to involve a potential trade-off between
efficiency and portability. Again, any comments, suggestions, alternative
approaches welcome.

Simplified code below.
[*] The code to be used in anger is to grab random numbers from huge
binary data files for a statistical application ... I have identified
random number manipulation as a time bottleneck.

--- BEGIN CODE ----

// main.cpp

#include <iostream>
#include <fstream>
#include <string>

// Read unformatted data from disc and interpret as unsigned integers

int main()
{
// the word type (an unsigned integer type - season to taste)
typedef unsigned long word_t;

// number of chars per word
const size_t wordsize = sizeof(word_t);
std::cout << "wordsize = " << wordsize << ''\n'';

// data file (we open with ios::ate since we want to get file size first)
const std::string bitsfile = "bits.dat";
std::ifstream bfs(bitsfile.c_str(),std::ios::in|std::ios::binary |std::ios::ate);
if (!bfs.is_open()) {
std::cerr << "failed to open file " << bitsfile << ''\n'';
return 1;
}

// get file size
const size_t cfilesize = bfs.tellg();
std::cout << "filesize = " << cfilesize << " chars\n";

// number of bytes (chars) to read - mustn''t be bigger than file size!
const size_t cblocksize = 128;
std::cout << "block size (char) = " << cblocksize << ''\n'';
if (cblocksize>cfilesize) {
std::cerr << "block size cannnot be larger than file size\n";
return 1;
}

// number of words to write - must be divisible by word size!
const size_t wblocksize = cblocksize/wordsize;
std::cout << "block size (word) = " << wblocksize << ''\n'';
if (wblocksize*wordsize != cblocksize) {
std::cerr << "block size must be divisible by word size\n";
return 1;
}

// char buffer for reading
// COMMENTS? alignement issues?
char* const cbuf(new char[cblocksize]);

// word buffer for writing aliased to char buffer
// COMMENTS? how portable is this...?
// COMMENTS? alignement issues?
const word_t* const wbuf(reinterpret_cast<word_t* const>(cbuf));

// seek to beginning of file and read block of chars
bfs.seekg(0);
bfs.read(cbuf,cblocksize);
bfs.close();

// write out words
for (size_t i;i<wblocksize;++i) std::cout << "word " << i << " = " << wbuf[i] << ''\n'';

// clean up
delete [] cbuf;

return 0;
}

--- END CODE ----

--
Lionel B

解决方案

Hi

I didn''t go through all your code but some quick nodes
I would new it as a ''word '' array not as a ''character'' array just to make
sure it is at the right boundary. You can the reinterpret_cast to char* and
now that it is correctly alligned.

There are usually compiler options you can set. In visual studio you can set
aliignement for your project or add pragma ... directives in different
places.

If you write and read from a systems with different architectures
(big/little endian) your code of course will fail

BTW does the getfilesize really work (shouldn''t you relocate the get ptr to
the end of file first)?

Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Lionel B" <me@privacy.netwrote in message
news:KK*****************@newsfe5-win.ntli.net...

Greetings,

I have some code that is to read unformatted data from disc and interpret
it as blocks of unsigned integers. In an attempt to achieve efficiency
(it is pretty essential for my application that the code be
speed optimized[*]) I use reinterpret_cast to alias a block of chars read
in from disc as a block of integer "words". My existing code (see
simplified code below) appears to work well enough on the platforms
available to me, but I would like to achieve maximum portability and would
really appreciate any commentary on possible portability (and also memory
alignment) issues that might arise from my approach.

I understand that in some sense all bets are off when using
reinterpret_cast as details will be implementation-defined but I can''t see
why my aliasing method (with suitable checks) should cause any problems.

As to memory alignment for efficient access to my memory buffer, I''m not
sure how global operator new handles memory alignment (I shouldn''t think
the standard has anything to say about this), so I might be prepared to
either (i) "manually" align my memory blocks - not actually sure how to do
this or how much portability might be achieved with this approach) - or
(ii) use malloc / memalign, whatever, from the (or rather "a") C library,
where alignment behaviour might be more precisely specified (eg the GNU C
library). Of course there will be portability issues with this approach.

In essence my problem seems to involve a potential trade-off between
efficiency and portability. Again, any comments, suggestions, alternative
approaches welcome.

Simplified code below.
[*] The code to be used in anger is to grab random numbers from huge
binary data files for a statistical application ... I have identified
random number manipulation as a time bottleneck.

--- BEGIN CODE ----

// main.cpp

#include <iostream>
#include <fstream>
#include <string>

// Read unformatted data from disc and interpret as unsigned integers

int main()
{
// the word type (an unsigned integer type - season to taste)
typedef unsigned long word_t;

// number of chars per word
const size_t wordsize = sizeof(word_t);
std::cout << "wordsize = " << wordsize << ''\n'';

// data file (we open with ios::ate since we want to get file size first)
const std::string bitsfile = "bits.dat";
std::ifstream
bfs(bitsfile.c_str(),std::ios::in|std::ios::binary |std::ios::ate);
if (!bfs.is_open()) {
std::cerr << "failed to open file " << bitsfile << ''\n'';
return 1;
}

// get file size
const size_t cfilesize = bfs.tellg();
std::cout << "filesize = " << cfilesize << " chars\n";

// number of bytes (chars) to read - mustn''t be bigger than file size!
const size_t cblocksize = 128;
std::cout << "block size (char) = " << cblocksize << ''\n'';
if (cblocksize>cfilesize) {
std::cerr << "block size cannnot be larger than file size\n";
return 1;
}

// number of words to write - must be divisible by word size!
const size_t wblocksize = cblocksize/wordsize;
std::cout << "block size (word) = " << wblocksize << ''\n'';
if (wblocksize*wordsize != cblocksize) {
std::cerr << "block size must be divisible by word size\n";
return 1;
}

// char buffer for reading
// COMMENTS? alignement issues?
char* const cbuf(new char[cblocksize]);

// word buffer for writing aliased to char buffer
// COMMENTS? how portable is this...?
// COMMENTS? alignement issues?
const word_t* const wbuf(reinterpret_cast<word_t* const>(cbuf));

// seek to beginning of file and read block of chars
bfs.seekg(0);
bfs.read(cbuf,cblocksize);
bfs.close();

// write out words
for (size_t i;i<wblocksize;++i) std::cout << "word " << i << " = " <<
wbuf[i] << ''\n'';

// clean up
delete [] cbuf;

return 0;
}

--- END CODE ----

--
Lionel B



Lionel B wrote:

>
// char buffer for reading
// COMMENTS? alignement issues?
char* const cbuf(new char[cblocksize]);

// word buffer for writing aliased to char buffer
// COMMENTS? how portable is this...?
// COMMENTS? alignement issues?
const word_t* const wbuf(reinterpret_cast<word_t* const>(cbuf));

If you''re casting the direct return from new (as you are here), it
should be OK. new char[] is required to return a "universally"
aligned thing as long as you allocate at least as much as the
thing you are aligning to.

Of course, the universal guarantee is you can always alias things
to chars and they work. So rather than allocating a buffer of
char, you can allocate a buffer of something else and then
cast it to char to read/copy-in the data.


Oops don''t mind the BTW missed the ate and the comment :-(

Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Moonlit" <news moonlit xs4all nlwrote in message
news:45*********************@news.xs4all.nl...

Hi

I didn''t go through all your code but some quick nodes
I would new it as a ''word '' array not as a ''character'' array just to make
sure it is at the right boundary. You can the reinterpret_cast to char*
and now that it is correctly alligned.

There are usually compiler options you can set. In visual studio you can
set aliignement for your project or add pragma ... directives in different
places.

If you write and read from a systems with different architectures
(big/little endian) your code of course will fail

BTW does the getfilesize really work (shouldn''t you relocate the get ptr
to the end of file first)?

Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Lionel B" <me@privacy.netwrote in message
news:KK*****************@newsfe5-win.ntli.net...

>Greetings,

I have some code that is to read unformatted data from disc and interpret
it as blocks of unsigned integers. In an attempt to achieve efficiency
(it is pretty essential for my application that the code be
speed optimized[*]) I use reinterpret_cast to alias a block of chars
read
in from disc as a block of integer "words". My existing code (see
simplified code below) appears to work well enough on the platforms
available to me, but I would like to achieve maximum portability and
would
really appreciate any commentary on possible portability (and also memory
alignment) issues that might arise from my approach.

I understand that in some sense all bets are off when using
reinterpret_cast as details will be implementation-defined but I can''t
see
why my aliasing method (with suitable checks) should cause any problems.

As to memory alignment for efficient access to my memory buffer, I''m not
sure how global operator new handles memory alignment (I shouldn''t think
the standard has anything to say about this), so I might be prepared to
either (i) "manually" align my memory blocks - not actually sure how to
do
this or how much portability might be achieved with this approach) - or
(ii) use malloc / memalign, whatever, from the (or rather "a") C library,
where alignment behaviour might be more precisely specified (eg the GNU C
library). Of course there will be portability issues with this approach.

In essence my problem seems to involve a potential trade-off between
efficiency and portability. Again, any comments, suggestions, alternative
approaches welcome.

Simplified code below.

[*] The code to be used in anger is to grab random numbers from huge
binary data files for a statistical application ... I have identified
random number manipulation as a time bottleneck.

--- BEGIN CODE ----

// main.cpp

#include <iostream>
#include <fstream>
#include <string>

// Read unformatted data from disc and interpret as unsigned integers

int main()
{
// the word type (an unsigned integer type - season to taste)
typedef unsigned long word_t;

// number of chars per word
const size_t wordsize = sizeof(word_t);
std::cout << "wordsize = " << wordsize << ''\n'';

// data file (we open with ios::ate since we want to get file size first)
const std::string bitsfile = "bits.dat";
std::ifstream
bfs(bitsfile.c_str(),std::ios::in|std::ios::binar y|std::ios::ate);
if (!bfs.is_open()) {
std::cerr << "failed to open file " << bitsfile << ''\n'';
return 1;
}

// get file size
const size_t cfilesize = bfs.tellg();
std::cout << "filesize = " << cfilesize << " chars\n";

// number of bytes (chars) to read - mustn''t be bigger than file size!
const size_t cblocksize = 128;
std::cout << "block size (char) = " << cblocksize << ''\n'';
if (cblocksize>cfilesize) {
std::cerr << "block size cannnot be larger than file size\n";
return 1;
}

// number of words to write - must be divisible by word size!
const size_t wblocksize = cblocksize/wordsize;
std::cout << "block size (word) = " << wblocksize << ''\n'';
if (wblocksize*wordsize != cblocksize) {
std::cerr << "block size must be divisible by word size\n";
return 1;
}

// char buffer for reading
// COMMENTS? alignement issues?
char* const cbuf(new char[cblocksize]);

// word buffer for writing aliased to char buffer
// COMMENTS? how portable is this...?
// COMMENTS? alignement issues?
const word_t* const wbuf(reinterpret_cast<word_t* const>(cbuf));

// seek to beginning of file and read block of chars
bfs.seekg(0);
bfs.read(cbuf,cblocksize);
bfs.close();

// write out words
for (size_t i;i<wblocksize;++i) std::cout << "word " << i << " = " <<
wbuf[i] << ''\n'';

// clean up
delete [] cbuf;

return 0;
}

--- END CODE ----

--
Lionel B




这篇关于reinterpret_cast可移植性/对齐问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆