【问题标题】:Stack around variable 'x' was corrupted围绕变量“x”的堆栈已损坏
【发布时间】:2015-09-19 20:44:24
【问题描述】:

我正在操作一个 C 代码,该代码需要我提取一个值、执行计算并将该值放回原始变量中。

放回值的任务导致错误:

运行时检查失败 - 围绕变量“x”的堆栈已损坏。

代码如下:

int RBSPtoNALU (unsigned char *rbsp, NALU_t *nalu, int rbsp_size, int nal_unit_type, int nal_reference_idc, int UseAnnexbLongStartcode)
{
  int len;

  int num = nalu->buf;  ***//nalu->buf is the information i am extracting***

 int r = 0,newnum=0;

  while (num > 0)       ***// Doing a simple reverse operation***

  {
      r = num % 10;
      newnum = newnum * 10 + r;

      num =(num/10);
  }


  byte x = newnum;
  printf("x is in c %c \n", x);
  printf("x is in d %d \n \n \n", x);  getchar();

  nalu->buf = &x;  ***// My efforts end here***


  assert (nalu != NULL);
  assert (nal_reference_idc <=3 && nal_reference_idc >=0);
#if (MVC_EXTENSION_ENABLE)
  assert (nal_unit_type > 0 && nal_unit_type <= NALU_TYPE_SLC_EXT);
#else
  assert (nal_unit_type > 0 && nal_unit_type <= NALU_TYPE_FILL);
#endif
  assert (rbsp_size < MAXRBSPSIZE);

  nalu->startcodeprefix_len = UseAnnexbLongStartcode ? 4 : 3;
  nalu->forbidden_bit       = 0;  
  nalu->nal_reference_idc   = (NalRefIdc) nal_reference_idc;
  nalu->nal_unit_type       = (NaluType) nal_unit_type;

#if (MVC_EXTENSION_ENABLE)
  if(nal_unit_type==NALU_TYPE_PREFIX || nal_unit_type==NALU_TYPE_SLC_EXT)
  {
    nalu->svc_extension_flag = 0;
    //nalu->non_idr_flag       = (nal_reference_idc==NALU_PRIORITY_HIGHEST) ? 0:1;
    nalu->reserved_one_bit   = 1;

  }
  else
    nalu->svc_extension_flag = 0;
endif


  len = RBSPtoEBSP (nalu->buf, rbsp, rbsp_size);
  nalu->len = len;

  printf("length len is %d", len); printf("\n \n");

  return len;
} ***//end of code***

nalu 是结构 NALU_t 的一个实例,其定义如下:

typedef struct nalu_t
{
  int       startcodeprefix_len;   //!< 4 for parameter sets and first slice in picture, 3 for everything else (suggested)
  unsigned  len;                   //!< Length of the NAL unit (Excluding the start code, which does not belong to the NALU)
  unsigned  max_size;              //!< NAL Unit Buffer size
  int       forbidden_bit;         //!< should be always FALSE
  NaluType  nal_unit_type;         //!< NALU_TYPE_xxxx
  NalRefIdc nal_reference_idc;     //!< NALU_PRIORITY_xxxx  
  ***byte     *buf;***                   //!< contains the first byte followed by the EBSP
  uint16    lost_packets;          //!< true, if packet loss is detected
#if (MVC_EXTENSION_ENABLE)
  int       svc_extension_flag;    //!< should be always 0, for MVC
  int       non_idr_flag;          //!< 0 = current is IDR
  int       priority_id;           //!< a lower value of priority_id specifies a higher priority
  int       view_id;               //!< view identifier for the NAL unit
  int       temporal_id;           //!< temporal identifier for the NAL unit
  int       anchor_pic_flag;       //!< anchor access unit
  int       inter_view_flag;       //!< inter-view prediction enable
  int       reserved_one_bit;      //!< shall be equal to 1
#endif
} NALU_t;

buf 是一个指向 byte 的指针引用,它是一个定义如下的结构体:

typedef unsigned char byte;


【问题讨论】:

  • 其他降价在代码降价中不起作用,**this is not bold**。不要迷惑​​人

标签: c stack


【解决方案1】:
int num = nalu->buf;

nalu-&gt;buf 给出指针的值(地址不是存储在该位置的值)。您需要取消引用它以获取存储在位置中的值

int num = *(nalu->buf);

反过来

*(nalu->buf) = newnum;/* or  *(nalu->buf) = x */

【讨论】:

  • 还是不行。现在它给出了访问冲突。你能建议一种方法,我可以提取 nalu-buf 的值,然后将其存储回其中而不会出现错误。
  • 做到了。帮我把修改后的值放回nalu->buf @gopi
  • 程序正在运行,谢谢,我最后一次麻烦你解决这个问题:我在代码中写了以下几行(检查它是否工作正常) newnum = 12345; *(nalu->buf) = newnum; printf("nalu->buf里面的值是%d \n", *(nalu->buf));但是我得到的输出是: nalu->buf 中的值是 57 出了什么问题?提前感谢@gopi
  • @chickenmomo 好。如果这有助于将其标记为已回答正确的方式,请在 SO 上表示感谢
  • 程序正在运行,谢谢,我最后一次麻烦你解决这个问题:我在代码中写了以下几行(检查它是否工作正常) newnum = 12345; *(nalu->buf) = newnum; printf("nalu->buf里面的值是%d \n", *(nalu->buf));但是我得到的输出是: nalu->buf 中的值是 57 出了什么问题?提前感谢@gopi
【解决方案2】:

你不能这样做:

  byte x = newnum;
  printf("x is in c %c \n", x);
  printf("x is in d %d \n \n \n", x);  getchar();

  nalu->buf = &x;  ***// My efforts end here***

x(它可能只是成为 newnum 的编译器别名,因为你不修改它)只有本地范围!一旦您的函数退出,它应该不再存在。但是您将其地址保存在来自外部的结构中。

【讨论】:

    【解决方案3】:

    nalu->buf定义为字节*buf;

    这意味着它是一个指向字节的指针。

    您正在将 nalu->buf 中的地址复制到整数变量 num。这是错误的。这是一个语义错误,但您不会因此而收到异常。

    从那里进一步声明一个名为“x”的变量并将其地址复制到 nalu->buf 中。这是错误的,因为该函数退出时变量 x 将不存在。这就是您收到运行时错误的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多