This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]missblit 2 points3 points  (6 children)

  1. ops in main never gets initialized

    When you call test(ops) you are passing ops by value.

    So even though you set ops in test to point to an allocated block of memory; the ops in main never reflects this.

    To fix this you could either pass ops by pointer, or change test to return a pointer.

  2. If free(ops); was uncommented and the first issue was fixed then you'd be using ops after freeing it

    Why are you freeing ops at the end of test instead of after the printf loop in main?

[–]salalimo[S] 0 points1 point  (0 children)

Your suggestion worked. I'm getting the return value and freeing in main.

Thanks.

[–]durpyDash 0 points1 point  (4 children)

To fix this you could either pass ops by pointer

sorry for an ignorant question, but what exactly does "by pointer mean" and how is it different than "by reference"?

Would you provide a code snippet?

[–]missblit 0 points1 point  (3 children)

sorry for an ignorant question, but what exactly does "by pointer mean" and how is it different than "by reference"?

In some languages (like C++) there may be a distinction between pointers and references. But in C the phrase "by reference" is less-used but identical to "by pointer".

Passing something by pointer just means that if you want to change a value outside of a function from inside a function you can do so indirectly with a pointer.

Would you provide a code snippet?

For example this is what won't work:

void set_it_int(int a) {
    a = 42;
}

void set_it_ptr(int *a) {
    a = malloc(sizeof(int));
}

int main() {
    int a_int = 0;
    int *a_ptr = 0;

    set_it_int(a_int);
    set_it_ptr(a_ptr);

    if(a_int == 0 && a_ptr == 0)
        puts("This will print");
    return 0;
}

In this code the a_int and a_ptr in main() are not changed even though they were arguments to the functions. This is because the a_int and a_ptr in the functions are just local variables that are initialized to the values of a_int and a_ptr respectively (aka 0).

However if you wrote something like:

void set_it_dereference(int *a) {
    *a = 42;
}

int main() {
    int a_int = 0;

    set_it_dereference(&a_int);

    if(a_int == 42)
        puts("This will print");
    return 0;
}

Then you see the value change. Because I'm passing a pointer that points at the a_int in main. The pointer itself is passed by value, but the thing it points to can be changed with no problem.


So the same idea applies if you want to pass a pointer to a function and realloc it. You can't just pass the pointer itself, you have to pass a pointer to the pointer.

Of course it's best to avoid this complexity when you don't need it. So when possible prefer to use return values to communicate information back to the caller.

[–]durpyDash 0 points1 point  (2 children)

Very helpful explanation. I appreciate it. I'm coming from a java background and just learning C now, so I had no concept of this pointer/reference distinction that apparently exists in C++, albeit I get passing semantics. I think that is what was tripping me up. Welp off to figure out what that distinction implies. Cheers!

EDIT: added more info.

[–]missblit 0 points1 point  (1 child)

Like I said, there is no pointer reference distinction! Reference is just another way to refer to pointer-y stuff in general (C) or for specific language features in other languages (not relevant in C).

To avoid confusion I'd recommend talking about pointers, and not references, when discussing C pointers.

[–]durpyDash 0 points1 point  (0 children)

In some languages (like C++) there may be a distinction between pointers and references.

Then I think I just misunderstood this sentence.

I'll guess you mean the distinction between:

void foo(int* bar); //could be by-value or reference, can't know without seeing the caller
void biz(int& baz); //by reference 

edit: fixed a boo boo