内存值未经许可即被更改 [英] Value of memory changed without permission

查看:57
本文介绍了内存值未经许可即被更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维数组,第一次打印数组数据时,日期正确打印,而其他时候,数组[last] [i]的数据从i = 0到最后- 1.

I have an array with 2 dimensions and when I print the data of the array the first time, the date is printed correctly, but the other times the data of array[last][i] from i = 0 to last - 1.

显然是一个逻辑错误,但我不明白原因,因为我复制并粘贴了for语句.那么...¿C更改数据了吗?

Apparently is a logic error, but I don't understand the reason, because I copy and paste the for statement. So... ¿C change the data?

我使用gcc -std=c99,但在此之前,我尝试使用C ++和cout语句.

I use gcc -std=c99 but before that I try with C++ and cout statements.

这是输出屏幕截图

#include <stdio.h>

int main(int argc, char *argv[])
{
  unsigned int numero_jugaderes = 11;
  unsigned int numero = numero_jugaderes - 1;

  unsigned int p_a[numero];

  float p_aya[numero][numero];

  for (unsigned int i = 0; i <= numero; i++) {
    p_a[i] = i;
  }

  for (unsigned int i = 0; i <= numero; i++) {
    for (unsigned int j = 0; j <= numero; j++) {
      p_aya[i][j] = (float) (p_a[i] * p_a[j]) / 100;
      printf("%f\t", p_aya[i][j]);
    }
    puts("");
  }

  puts("\n");

  for (unsigned int i = 0; i <= numero; i++) {
    for (unsigned int j = 0; j <= numero; j++) {
      printf("%f\t", p_aya[i][j]);
    }
    puts("");
  }

  return 0;
}

推荐答案

我所看到的问题是,您正在像进行条件检查那样遍历

The problem as I see it is, you're looping over with a condition check like

 for (unsigned int i = 0; i <= numero; i++) 

用于定义为

unsigned int p_a[numero];

,您将一步一步地.这实际上是无效的内存访问,会调用未定义的行为.

and you're going off-by-one. This is essentially invalid memory access which invokes undefined behavior.

C数组具有从0开始的索引,因此有效限制为

C arrays have 0-based indexing, so the valid limit would be

for (unsigned int i = 0; i < numero; i++)

这篇关于内存值未经许可即被更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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