首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

帮小弟我看看这段代码哪里有有关问题,运行时出现段异常

2013-08-10 
帮我看看这段代码哪里有问题,运行时出现段错误/// @Synopsisresize /*{{{*//// @Param _from_data/// @Par

帮我看看这段代码哪里有问题,运行时出现段错误

/// @Synopsis  resize /*{{{*/
/// @Param _from_data
/// @Param _from_width
/// @Param _from_height
/// @Param _to_data
/// @Param _to_width
/// @Param 
void resize(
    Bitmap24PixelRgb* _from_data, unsigned int _from_width, unsigned int _from_height,
    Bitmap24PixelRgb* _to_data, unsigned int _to_width, unsigned int _to_height
    ){

    unsigned int row = _to_height;
    unsigned int column = _to_width;
    unsigned int cell_size = sizeof( Bitmap24PixelRgb );

    void** matrix;
    matrix = (void**) malloc( sizeof(void*) * row + cell_size * row * column) ;
    if( matrix != NULL){
memset( matrix , 0 , sizeof(void*) * row + cell_size * row * column );

void* head;
head = (void*)matrix + sizeof( void* ) * row;
while( row-- ){
    matrix[row] = head + cell_size * row * column;
}
    }


    Bitmap24PixelRgb** data = (Bitmap24PixelRgb**) matrix;
    unsigned int x;
    unsigned int y;
    for( y=0; y < row -1 ; y++){
for( x=0; x< column -1; x++){
    fprintf( stdout, "%d", data[y][x].red );
}
    }

    free( matrix);
}/*}}}*/

这段代码动态分配了一个二维数组。并将所有数组元素清0
在使用下标便利的时候出现“段错误”
gdb调试信息:
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Program received signal SIGSEGV, Segmentation fault.
0x080494d0 in resize (_from_data=0xb7d93008, _from_width=800, _from_height=500, _to_data=0xb7f85008, _to_width=400, _to_height=300) at transform.c:111
111    fprintf( stdout, "%d", data[y][x].red );

Missing separate debuginfos, use: debuginfo-install glibc-2.14.90-24.fc16.7.i686 libX11-1.4.3-1.fc16.i686 libXau-1.0.6-2.fc15.i686 libgcc-4.6.2-1.fc16.i686 libxcb-1.7-3.fc16.i686

错误是这么明显,我却不能发现哪里造成的下标访问越界。
帮看看。。。。。
[解决办法]


    void** matrix; //  这里你定义的是二级指针哦!所以,你下面要分配两次内存的!
                 //不然相关matrix[i]来说,它就没有内存空间的!
    matrix = (void**) malloc( sizeof(void*) * row + cell_size * row * column) ;
    if( matrix != NULL)
    {   
        memset( matrix , 0 , sizeof(void*) * row + cell_size * row * column );

        void* head;
        head = (void*)matrix + sizeof( void* ) * row;
        while( row-- )
        {   
            matrix[row] = head + cell_size * row * column;
            //matrix[row]这个,你没有分配内存空间给它,赋值,内存访问出错了!
        }   
    }   

[解决办法]
作resize的话,根本不需要在函数内部分配空间,只需要插入或者是删除Bitmap24PixelRgb像素点就好。

引用:
/// @Synopsis  resize /*{{{*/
/// @Param _from_data
/// @Param _from_width
/// @Param _from_height
/// @Param _to_data
/// @Param _to_width
/// @Param 
void resize(
    Bitmap24PixelRgb* _from_data, unsigned int _from_width, unsigned int _from_height,
    Bitmap24PixelRgb* _to_data, unsigned int _to_width, unsigned int _to_height


    ){

    unsigned int row = _to_height;
    unsigned int column = _to_width;
    unsigned int cell_size = sizeof( Bitmap24PixelRgb );

    void** matrix;
    matrix = (void**) malloc( sizeof(void*) * row + cell_size * row * column) ;
    if( matrix != NULL){
memset( matrix , 0 , sizeof(void*) * row + cell_size * row * column );

void* head;
head = (void*)matrix + sizeof( void* ) * row;
while( row-- ){
    matrix[row] = head + cell_size * row * column;
}
    }


    Bitmap24PixelRgb** data = (Bitmap24PixelRgb**) matrix;
    unsigned int x;
    unsigned int y;
    for( y=0; y < row -1 ; y++){
for( x=0; x< column -1; x++){
    fprintf( stdout, "%d", data[y][x].red );
}
    }

    free( matrix);
}/*}}}*/


这段代码动态分配了一个二维数组。并将所有数组元素清0
在使用下标便利的时候出现“段错误”
gdb调试信息:
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Program received signal SIGSEGV, Segmentation fault.
0x080494d0 in resize (_from_data=0xb7d93008, _from_width=800, _from_height=500, _to_data=0xb7f85008, _to_width=400, _to_height=300) at transform.c:111
111    fprintf( stdout, "%d", data[y][x].red );Missing separate debuginfos, use: debuginfo-install glibc-2.14.90-24.fc16.7.i686 libX11-1.4.3-1.fc16.i686 libXau-1.0.6-2.fc15.i686 libgcc-4.6.2-1.fc16.i686 libxcb-1.7-3.fc16.i686

错误是这么明显,我却不能发现哪里造成的下标访问越界。
帮看看。。。。。

[解决办法]
引用:
作resize的话,根本不需要在函数内部分配空间,只需要插入或者是删除Bitmap24PixelRgb像素点就好。

Quote: 引用:

/// @Synopsis  resize /*{{{*/
/// @Param _from_data
/// @Param _from_width
/// @Param _from_height
/// @Param _to_data
/// @Param _to_width
/// @Param 
void resize(
    Bitmap24PixelRgb* _from_data, unsigned int _from_width, unsigned int _from_height,
    Bitmap24PixelRgb* _to_data, unsigned int _to_width, unsigned int _to_height
    ){

    unsigned int row = _to_height;
    unsigned int column = _to_width;
    unsigned int cell_size = sizeof( Bitmap24PixelRgb );

    void** matrix;
    matrix = (void**) malloc( sizeof(void*) * row + cell_size * row * column) ;
    if( matrix != NULL){
memset( matrix , 0 , sizeof(void*) * row + cell_size * row * column );

void* head;
head = (void*)matrix + sizeof( void* ) * row;
while( row-- ){
    matrix[row] = head + cell_size * row * column;
}
    }


    Bitmap24PixelRgb** data = (Bitmap24PixelRgb**) matrix;
    unsigned int x;
    unsigned int y;
    for( y=0; y < row -1 ; y++){
for( x=0; x< column -1; x++){
    fprintf( stdout, "%d", data[y][x].red );
}
    }

    free( matrix);
}/*}}}*/

这段代码动态分配了一个二维数组。并将所有数组元素清0
在使用下标便利的时候出现“段错误”
gdb调试信息:
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Program received signal SIGSEGV, Segmentation fault.
0x080494d0 in resize (_from_data=0xb7d93008, _from_width=800, _from_height=500, _to_data=0xb7f85008, _to_width=400, _to_height=300) at transform.c:111
111    fprintf( stdout, "%d", data[y][x].red );

Missing separate debuginfos, use: debuginfo-install glibc-2.14.90-24.fc16.7.i686 libX11-1.4.3-1.fc16.i686 libXau-1.0.6-2.fc15.i686 libgcc-4.6.2-1.fc16.i686 libxcb-1.7-3.fc16.i686

错误是这么明显,我却不能发现哪里造成的下标访问越界。
帮看看。。。。。



问题是他定义了二级指针在里面,而且作了赋值操作的哦?如果不在分配内存的话,
直接赋值内存肯定出错吧?
[解决办法]
仅供参考
//在堆中开辟一个4×5的二维int数组
#include <stdio.h>
#include <malloc.h>
int **p;
int i,j;
void main() {
    p=(int **)malloc(4*sizeof(int *));
    if (NULL==p) return;
    for (i=0;i<4;i++) {
        p[i]=(int *)malloc(5*sizeof(int));
        if (NULL==p[i]) return;
    }
    for (i=0;i<4;i++) {
        for (j=0;j<5;j++) {
            p[i][j]=i*5+j;
        }
    }
    for (i=0;i<4;i++) {
        for (j=0;j<5;j++) {
            printf(" %2d",p[i][j]);
        }
        printf("\n");
    }
    for (i=0;i<4;i++) {
        free(p[i]);
    }
    free(p);
}
//  0  1  2  3  4
//  5  6  7  8  9
// 10 11 12 13 14
// 15 16 17 18 19

[解决办法]
直接赋值给_to_data是不会有问题,函数调用前需保证可以存放resize之后的像素数据。

引用:
Quote: 引用:

作resize的话,根本不需要在函数内部分配空间,只需要插入或者是删除Bitmap24PixelRgb像素点就好。

Quote: 引用:

/// @Synopsis  resize /*{{{*/
/// @Param _from_data
/// @Param _from_width
/// @Param _from_height
/// @Param _to_data
/// @Param _to_width
/// @Param 
void resize(
    Bitmap24PixelRgb* _from_data, unsigned int _from_width, unsigned int _from_height,


    Bitmap24PixelRgb* _to_data, unsigned int _to_width, unsigned int _to_height
    ){

    unsigned int row = _to_height;
    unsigned int column = _to_width;
    unsigned int cell_size = sizeof( Bitmap24PixelRgb );

    void** matrix;
    matrix = (void**) malloc( sizeof(void*) * row + cell_size * row * column) ;
    if( matrix != NULL){
memset( matrix , 0 , sizeof(void*) * row + cell_size * row * column );

void* head;
head = (void*)matrix + sizeof( void* ) * row;
while( row-- ){
    matrix[row] = head + cell_size * row * column;
}
    }


    Bitmap24PixelRgb** data = (Bitmap24PixelRgb**) matrix;
    unsigned int x;
    unsigned int y;
    for( y=0; y < row -1 ; y++){
for( x=0; x< column -1; x++){
    fprintf( stdout, "%d", data[y][x].red );
}
    }

    free( matrix);
}/*}}}*/


这段代码动态分配了一个二维数组。并将所有数组元素清0
在使用下标便利的时候出现“段错误”
gdb调试信息:
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Program received signal SIGSEGV, Segmentation fault.
0x080494d0 in resize (_from_data=0xb7d93008, _from_width=800, _from_height=500, _to_data=0xb7f85008, _to_width=400, _to_height=300) at transform.c:111
111    fprintf( stdout, "%d", data[y][x].red );Missing separate debuginfos, use: debuginfo-install glibc-2.14.90-24.fc16.7.i686 libX11-1.4.3-1.fc16.i686 libXau-1.0.6-2.fc15.i686 libgcc-4.6.2-1.fc16.i686 libxcb-1.7-3.fc16.i686

错误是这么明显,我却不能发现哪里造成的下标访问越界。
帮看看。。。。。


问题是他定义了二级指针在里面,而且作了赋值操作的哦?如果不在分配内存的话,
直接赋值内存肯定出错吧?

热点排行