问题
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
回答1:
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] = ...
}
来源:https://stackoverflow.com/questions/20818238/c-access-violation-when-reading-array