Ç - 阅读时访问冲突数组 [英] c - Access violation when reading array

查看:197
本文介绍了Ç - 阅读时访问冲突数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取数据的数组,并接受访问冲突。该阵列是使用分配的I可以从函数内的阵列读取数据

  AllCurrentData [newLineCount]。数据[tabCount] =的malloc(长+ 1);
的strcpy(AllCurrentData [newLineCount]。数据[tabCount],缓冲区);的printf(%S,AllCurrentData [newLineCount]。数据[tabCount]);

但功能之外无法读取。这是我得到的访问冲突,看起来它试图读取一个空位置。

我如何可以访问不同的功能数组 AllCurrentData 中的数据?谢谢!

额外的信息:

  typedef结构{电流    焦炭**数据;} CURRENTDATA;

AllCurrentData 主声明:

  CURRENTDATA * AllCurrentData ='\\ 0';

函数调用

  getCurrentData(电流,AllCurrentData);的printf(%S,AllCurrentData [0]。数据[0]); //< -----这里的错误


解决方案

  CURRENTDATA * AllCurrentData ='\\ 0';

这声明了一个指针。这个指针是保存一个数字,PTED作为地址间$ P $一个变量。初始化指针'\\ 0'(空)。

  getCurrentData(电流,AllCurrentData);

在这里,你通过这个指针作为参数传递给函数 getCurrentData 。作为一个指针是一个变量,这个变量是按值传递的,这意味着该函数接收值的副本(即重新presents地址数)。

在里面的功能,如果你写

  AllCurrentData = malloc的...

您修改指针的副本,因此函数外AllCurrentData将仍然是'\\ 0'。
您需要将指针传递给该指针(成立之初,我知道)。

  getCurrentData(电流,&安培; AllCurrentData);getCurrentData(..,CURRENTDATA ** p_AllCurrentData){
* p_AllCurrentData =的malloc(...);
}


让我解释一个简单的例子这一概念:

 为int * V = NULL; //这里v是一个指​​针。该指针值为0(无效地址)
V = malloc的(10 *的sizeof(int)的); //这里的指针被分配一个有效的地址,可以说0x41A0
F(V);
无效F(INT * X){
  //这里的指针X收到指针诉值的副本,所以x是,像V,0x41A0
  X [0] = 4;
  / *当你在地址0x41A0修改内存,这是好的,
  因此本变形例是从功能外可见,为v指向相同的地址。* /  X =的malloc(...);
  / *这也不行,为x接收到新的地址,可以说0xCC12。
  但由于x具有诉值的副本,V仍然会修改。* /
}

所以,如果你想分配一个函数内部的指针,这是不正常:

 为int * V = NULL; //这里v是一个指​​针。该指针值为0(无效地址)F(V);无效F(INT * X){
  //这里的指针X收到指针诉值的副本,所以x是,像V,NULL
  X =的malloc(...);
  / *这也不行,为x接收到新的地址,可以说0xCC12。
  但由于x具有诉值的副本,V仍然会修改,
  它仍然具有NULL * /
}

做正确的做法是:

 为int * V = NULL; //这里v是一个指​​针。该指针值为0(无效地址)
//为v是一个变量(类型的指针为int),V有一个地址,让我们说加上0xAAAA。
F(&放大器; 5);
无效F(INT ** p_x){
  / *这里的p_x是一个指针(int的指针),
  所以该指针接收数值p的地址的拷贝,所以p_x是加上0xAAAA。* /
  * p_x =的malloc(...);
  / *可以说malloc的返回地址0xBBBB。这将是我们向量的地址。
  * p_x =说,我们在地址p_x修改数值。因此,价值发现在ADRESS加上0xAAAA
  将0XBBBB。但由于加上0xAAAA为v的地址,我们有效地修饰V的值
  到0xBBBB。所以现在V有我们的出发向量的地址。* /
  //如果要修改这个载体需要里面的值:
  (* p_x)[0] = ...
}

I am trying to read an array of data and receiving an access violation. I can read the data from the array within the function that the array was allocated using:

AllCurrentData[newLineCount].data[tabCount] = malloc(length + 1);
strcpy( AllCurrentData[newLineCount].data[tabCount], buffer );

printf("%s", AllCurrentData[newLineCount].data[tabCount]);

But can't read it outside of the function. This is where I get the access violation, looks like it is trying to read a null location.

How can I access the data in the array AllCurrentData in a different function? thanks!

Extra info:

typedef struct current{

    char **data;

}CurrentData;

AllCurrentData is declared in main:

CurrentData *AllCurrentData = '\0';

Function call

getCurrentData(current, AllCurrentData);

printf("%s", AllCurrentData[0].data[0]);   //<----- error here

解决方案

CurrentData *AllCurrentData = '\0';

This declares a pointer. This pointer is a variables that holds a number that is interpreted as an address. You initialize the pointer to '\0' (null).

getCurrentData(current, AllCurrentData);

Here you pass this pointer as parameter to function getCurrentData. As a pointer is a variable, this variable is passed by value, meaning that the function receives a copy of that value (the number that represents an address).

When inside the function if you write

AllCurrentData = malloc...

you modify that copy of the pointer, so outside the function AllCurrentData will still be '\0'. You need to pass a pointer to that pointer (Inception, I know).

getCurrentData(current, &AllCurrentData);

getCurrentData(.., CurrentData **p_AllCurrentData) {
*p_AllCurrentData = malloc(...);
}


Let me explain this concept on a simpler example:

int *v = NULL; // here v is a pointer. This pointer has value 0 (invalid address)
v = malloc(10 * sizeof(int)); // here the pointer is assigned a valid address, lets say 0x41A0
f(v);


void f(int *x) {
  // here the pointer x receives a copy of the value of the pointer v, so x will be, like v, 0x41A0
  x[0] = 4; 
  /*this is ok as you modify the memory at the address 0x41A0,
  so this modification is seen from outside the function, as v points to the same address.*/

  x = malloc(...);
  /* this is NOT ok, as x receives a new address, lets say 0xCC12.
  But because x has a copy of the value from v, v will still be unmodified.*/
}

so if you want to allocate a pointer inside a function this is not ok:

int *v = NULL; // here v is a pointer. This pointer has value 0 (invalid address)

f(v);    

void f(int *x) {
  // here the pointer x receives a copy of the value of the pointer v, so x will be, like v, NULL


  x = malloc(...); 
  /*this is NOT ok, as x receives a new address, lets say 0xCC12.
  But because x has a copy of the value from v, v will still be unmodified,
  it will still have NULL.*/
}

The right way to do is:

int *v = NULL; // here v is a pointer. This pointer has value 0 (invalid address)
// as v is a variable (of type pointer to int), v has an address, lets say 0xAAAA.
f(&v);


void f(int **p_x) {
  /* here the p_x is a pointer to (a pointer of int),
  so this pointer receives a copy of the value the address of p, so p_x is 0xAAAA.*/


  *p_x = malloc(...);
  /* lets say malloc returns the address 0xBBBB. This will be the address of our vector.
  *p_x= says we modify the value at the address p_x. So the value found at the adress 0xAAAA
  will be 0XBBBB. But because 0xAAAA is the address of v, we effectively modified the value of v
  to 0xBBBB. So now v has the address of our starting vector.*/
  // if you want to modify values inside this vector you need:
  (*p_x)[0] = ...
}

这篇关于Ç - 阅读时访问冲突数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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