变量周围的堆栈损坏 [英] stack corruption around the variable

查看:401
本文介绍了变量周围的堆栈损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我这里有一个小问题.

我想做的是在串行端口上发送数据.我已经写了必要的代码,但是出现了调试错误.

错误文本是这个..

运行-时间检查失败#2,变量"a"周围的堆栈已损坏.

我写的代码如下:

Hey everyone.

I have a little problem here.

What I''m trying to do is sending data on serial port. I''ve written the necessary codes, but there appears a debug error.

The error text is this..

Run - Time Check Failure #2, Stack around the variable "a" was corrupted.

The Code that I wrote is below:

    char a = 1;

    printf("Press a key: \n");

    scanf("%d", &a) ;

    switch(a)
    {
        case 1:
            {
                char data1[8] = "___id:";
                int size1 = 8;
                ser.write(data1,size1);
                break;
            }
        case 2:
            {
                char data2[8] = "___sr:";
                int size2 = 8;
                ser.write(data2,size2);
                break;
            }

        case 3:
            {
                char data3[8] = "___np:";
                int size3 = 8;
                ser.write(data3,size3);
                break;
            }
        case 4:
            {
                char data4[8] = "_cocn:";
                int size4 = 8;
                ser.write(data4,size4);
                break;
            }
}




通过串口写的代码如下:




The Code for write through serial port is given below:

int SerPort::write(const char *data, unsigned int size)
{
    unsigned long ret;

    if (myPort != INVALID_HANDLE_VALUE && myStatus == STATUS_OPEN)
    {
        if (!WriteFile(myPort, data, size, &ret, NULL))
        {
            printf("SerPort::write: Error on writing.\n");
            return -1;
        }
        return ret;
    }

    printf("SerPort::write: Connection invalid.\n");
    return -1;
}



有谁有主意吗?
我最好的问候...



Does anyone has an idea?
My best Regards...

推荐答案

您应该使用
You should use
    int a = 1;
 
    printf("Press a key: \n");
 
    scanf("%d", &a) ;
//...


C pallini正确的问题是char只是一个字节.

一个int的长度可以是2 4甚至有时是8个字节,这就是堆栈损坏的原因. 您试图将2/4/8个字节放入单个字节的地址空间.

然后,这将进一步覆盖堆栈地址空间中的数据.
C pallini is correct the problem that you are having is that a char is only a single byte.

an int can be 2 4 or even sometimes 8 bytes in the length this is why the stack is corrupting.
You are trying to put 2/4/8 bytes into the address space of a single byte.

This then overwrites the data futher down the stack address space.


这可能不是解决堆栈损坏的方法,但是该代码中还有另一个问题.

您在串行线路上发送的所有字符串都声明为8个字符,并且您传输的是8个字符,但是这些字符串仅为6个字符(如果在末尾计算为空,则为7个字符).

您的分配是可以的,但是接收者"可能不喜欢NULL字符,也不喜欢前7个字符之后的第8个字符为垃圾邮件(堆栈先前使用的剩余字符).

在此代码中:
This may not be the solution to your stack corruption but there is another problem in that code.

All of your strings you are sending over the serial line are declared to be 8 characters and you transmit 8 characters but the strings are only 6 characters (7 if you count the null at the end).

You allocations are OK but the "receiver" may not like either the NULL character or the fact that after the first 7 characters, the 8th character is junk (leftover from the stack''s previous use).

in this code:
char data1[8] = "___id:";
int size1 = 8;
ser.write(data1,size1);


数据[0] =``_'';
数据[1] =``_'';
数据[2] =``_'';
数据[3] =``i'';
数据[4] =``d'';
data [5] ='';'';
数据[6] =``\ 0''; (空)
data [7] =(统一/剩余的垃圾字符)

然后您盲目地从串行端口发送8个字符.我无法告诉您正确的做法是什么或如何重新编写代码,因为我不知道在串行线上读取数据的其他程序/设备"期望什么,但我愿意打赌它不希望垃圾".


data[0] = ''_'';
data[1] = ''_'';
data[2] = ''_'';
data[3] = ''i'';
data[4] = ''d'';
data[5] = '';'';
data[6] = ''\0''; (null)
data[7] = (unitialized / leftover junk character)

and you blindly send 8 characters out the serial port. I can''t tell you what the right thing to do is or how to re-write your code because I do not know what the other "program / device" reading the data on the serial line is expecting but I''m willing to bet that it''s not expecting "junk".


这篇关于变量周围的堆栈损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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