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

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

问题描述

我正在尝试读取数据数组并收到访问冲突.我可以使用分配数组的函数从数组中读取数据:

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.

如何在不同的函数中访问数组 AllCurrentData 中的数据?谢谢!

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

额外信息:

typedef struct current{

    char **data;

}CurrentData;

AllCurrentData 在 main 中声明:

AllCurrentData is declared in main:

CurrentData *AllCurrentData = '';

函数调用

getCurrentData(current, AllCurrentData);

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

推荐答案

CurrentData *AllCurrentData = '';

这声明了一个指针.该指针是一个变量,其中包含一个被解释为地址的数字.您将指针初始化为 ''(空).

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

getCurrentData(current, AllCurrentData);

在这里,您将此指针作为参数传递给函数 getCurrentData.由于指针是一个变量,因此该变量是按值传递的,这意味着函数接收该值的副本(表示地址的数字).

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).

如果在函数内部写

AllCurrentData = malloc...

您修改了指针的副本,因此在函数 AllCurrentData 之外仍将是 ''.您需要传递一个指向该指针的指针(我知道是 Inception).

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

getCurrentData(current, &AllCurrentData);

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

<小时>

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

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.*/
}

正确的做法是:

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] = ...
}

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

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