变量'x'周围的堆栈已损坏

时间:2015-07-02 10:52:06

标签: c stack

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

放回值的任务会导致错误:

  

运行时检查failiure - 变量'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;

3 个答案:

答案 0 :(得分:0)

int num = nalu->buf;

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

中的值
int num = *(nalu->buf);

反向执行

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

答案 1 :(得分:0)

你不能这样做:

height <- as.numeric(height)
mean(height)

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*** (可能只是成为newnum的编译器别名,因为你不修改它)只有本地范围!一旦你的功能退出,它应该不复存在。但是你将它的地址保存在一个来自外部的结构中。

答案 2 :(得分:0)

nalu-&gt; buf定义为byte * buf;

这意味着它是指向byte的指针。

您正在将nalu-&gt; buf中的地址复制到整数变量num。哪个错了。这是一个语义错误,但由于这个原因,你没有得到异常。

从那里继续你宣布一个名为&#39; x&#39;的变量。并将其地址复制到nalu-&gt; buf。这是错误的,因为当此函数退出时,变量x将不存在。这就是您收到运行时错误的原因。