简体   繁体   中英

Why doesn't this pointer passing work?

EDIT: An initial "float** floats;" was a typo, corrected. Also, "*floats[0] = 1.0;" does work, but "*floats[1] = 1.0;" segfauls at that point. The array is malloced. Compilation has no warnings on pedantic c99.

This worked:

(pseudo:)

void func(int*);
int i;
func(&i);
void func(int* i) {
 func2(i);
}
void func2(int* i) {
 *i = 2;
}

initial value changed to 2.

but this didn't work:

void func(float**);
float* floats;
func(&floats);
void func(float** floats) {
 func2(floats);
}
void func2(float** floats) {
 *floats[1] = 1.0;
}

segfault on assignment;

because 'floats' is a bad pointer. Sure, you declared the pointer, but you haven't allocated any memory, so wherever it happens to point will be invalid.

Also, the address of a float** is a float***, which is not what your function calls for.

Why are you passing a pointer to an array? Have you tried

func(float *floats)
{
  func2(floats);
}

func2(float *floats)
{
  floats[0] = 2;
}

Say you have these 2 funcs, like you posted:

void func2(float** floats) {
 *floats[1] = 1.0;
}
void func(float** floats) {
 func2(floats);
}

Then you have the following code:

float* floats;
func(&floats);

What happens? First, floats is a float * , and since it's not assigned, it points to a random memory location, say 0xBADD . If you write to here, you're liable to screw something up.

The address of floats , &floats is another memory location pointing to where it is on the stack, say 0x1000 . This is passed to func , which is then verbatim passed to func2 .

What does func2 do? It tries to write something in the location *floats[1] . I'm not sure what happens first, the bracket or the star, so let's take both cases:

*(floats[1]) : First you find what floats[1] is. Since floats is the memory location of the initial floats variable, this is 0x1000 . floats[1] would be 0x1004 , since pointers are 4 bytes wide assuming a 32-bit system. Then you try to dereference this pointer, which is the value of whatever is in memory location 0x1004 . This could be anything, like 0xBAD2 , so trying to write a float value there most likely causes a segfault.

(*floats)[1] . Here you first dereference floats , which gets you 0xBADD , of type float * . You then try to write a value into 0xBADD + 4 , or 0xBAE1 . This is memory that hasn't been allocated for you, so writing there will likely cause a segfault.

Either way, you're messing with memory that isn't yours, thus leading to a segfault.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM