简体   繁体   English

C / C ++中的C样式无符号字符解析和处理-分段错误

[英]C-Style unsigned char parsing and manipulation in C/C++ - segmentation fault

Note that I'm using a C++ compiler ( hence, the cast on the calloc function calls) to do this, but the code is essentially C. 请注意,我正在使用C ++编译器(因此,对calloc函数进行calloc )执行此操作,但是代码本质上是C。

Basically, I have a typedef to an unsigned char known as viByte , which I'm using to create a string buffer to parse a file from binary (a TGA file, to be exact - but, that's irrelevant). 基本上,我对unsigned char有一个typedef ,称为viByte ,我用它来创建一个字符串缓冲区以从二进制文件(确切地说是TGA文件-但这是无关紧要的)中解析一个文件。

I'm writing basic functions for it right now; 我现在正在为此编写基本功能; append, prepend, new, etc. 追加,添加,添加等

The problem is that, on the first iteration of the first loop in viByteBuf_Prepend , I get a segmentation fault. 问题是,在viByteBuf_Prepend中的第一个循环的第一次迭代中,出现了分段错误。 I need to know why, exactly, as this is something which could keep me up all night without some pointers (pun intended). 我确切地知道为什么,因为这可以使我整夜不使用任何指针(双关语)。

I also would like to know if my algorithms are correct in terms of how the buffer is pre-pending the viByte string. 我还想知道我的算法在缓冲区如何在viByte字符串之前添加是否viByte For example, I have a feeling that using memset too much might be a bad idea, and whether or not my printf format for the unsigned char is correct (I have a feeling it isn't, as nothing is getting output to my console). 例如,我觉得过多使用memset可能不是一个好主意,并且我的unsigned char的printf格式是否正确(我觉得不正确,因为没有任何内容输出到控制台) 。

Compiling on GCC, Linux. 在GCC,Linux上编译。

Ze Code 泽码

#ifdef VI_BYTEBUF_DEBUG
void viByteBuf_TestPrepend( void )
{
    viByteBuf* buf = viByteBuf_New( 4 );

    buf->str = ( viByte* ) 0x1;

    printf(" Before viByteBuf_Prepend => %uc ", buf->str);

    viByteBuf_Prepend( buf, 3, ( viByte* ) 0x2 );

    printf(" After viByteBuf_Prepend => %uc ", buf->str);
}
#endif

viByteBuf* viByteBuf_New( unsigned int len )
{
    viByteBuf* buf = ( viByteBuf* ) calloc( sizeof( viByteBuf ), 1 );

    const int buflen = len + 1;

    buf->str = ( viByte* ) calloc( sizeof( viByte ), buflen );
    buf->len = buflen;

    buf->str[ buflen ] = '\0';

    return buf;
}

void viByteBuf_Prepend( viByteBuf* buf, unsigned int len, viByte* str )
{
    unsigned int pos, i;
    const unsigned int totallen = buf->len + len;
    viByteBuf* tmp = viByteBuf_New( totallen );
    viByte* strpos = buf->str;


    memset( tmp->str, 0, tmp->len );

    int index;

    for( i = 0; i < buf->len; ++i )
    {

       index = ( buf->len - i ) - 1;

       *strpos = buf->str[ 0 ];
       ++strpos;
    }

    memset( buf->str, 0, buf->len );

    printf( "%uc\n", buf->str );

    i = totallen;

    for ( pos = 0; pos < len; ++pos )
    {
        tmp->str[ pos ] = str[ pos ];
        tmp->str[ i ]   = buf->str[ i ];

        --i;
    }

    memset( buf->str, 0, buf->len );

    buf->len = tmp->len;

    memcpy( buf->str, tmp->str, tmp->len );

    viByteBuf_Free( tmp );

    //memset(  )
    //realloc( ( viByteBuf* ) buf, sizeof( viByteBuf ) * tmp->len );
}

Many thank yous. 非常感谢。

Update 更新资料

Sorry, I should have explicitly posted the code where the segmentation fault lies. 抱歉,我应该明确地将代码发布到细分错误所在的位置。 It is right here: 就在这里:

for( i = 0; i < buf->len; ++i )
{

   index = ( buf->len - i ) - 1;

   *strpos = buf->str[ 0 ]; //<--segmentation fault.
   ++strpos;
}

On your code you have buf->str[ buflen ] = '\\0'; 在您的代码上,您有buf->str[ buflen ] = '\\0'; , but you only allocate space for buflen . ,但您只能为buflen分配空间。 I think you meant buf->str[ len ] = '\\0'; 我认为您的意思是buf->str[ len ] = '\\0'; .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM