0

I want to have a void* as a function parameter and then inside a function modify that pointer (change it to NULL).

Code below doesn't change memory1 to null after function call. How can I change that.

#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>

void myFree(void**freeMemoryPointer){
    /*some code*/
    *freeMemoryPointer=NULL;
}


int main(){
    void *memory1 = mmap(NULL, getpagesize() , PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);//example
    printf("\nCurrent pointer: %p\n", memory1);
    myFree(memory1);
    printf("\nShould show null: %p\n", memory1);
}

I've also tried this and it works:

myFree((void*)&memory1);

However I need to set void* as a function parameter and not (void*)&void*.

George
  • 21
  • 3
  • 4
    myFree(&memory1) should work – Fredrik Dec 30 '22 at 13:16
  • 1
    also main() is not a valid signature for main: https://stackoverflow.com/a/2108208/5878272 – Fredrik Dec 30 '22 at 13:17
  • C does not have built-in reference types; `(void*)&void*` would not be a valid parameter type. Are you compiling as C++ or C? Write the code using `void myFree(void**freeMemoryPointer)` as the function declaration and `myFree(&memory1);` as the call. If that does not work, make a [mre] showing it and edit it into the question. – Eric Postpischil Dec 30 '22 at 13:19
  • @Fredrik: `int main() {…}` is a valid definition of `main`. C 2018 5.1.2.2.1 1 says `main` shall be declared as `int main(void)` or `int main(int argc, char *argv[])` “or equivalent,” and `int main() { … }` is equivalent. In a function declaration that is not a declaration, `()` does not declare parameters or their types (including not declaring there are no parameters). However, in the grammar for a function definition, in C 2018 6.9.1, `int main() { … }` defines the function to take no parameters, making it equivalent to `int main(void) { … }`. – Eric Postpischil Dec 30 '22 at 13:22
  • @EricPostpischil isn't the form of an empty parameter list deprected though? – Fredrik Dec 30 '22 at 13:23
  • @Fredrik: No, it is not deprecated, and that would be a separate question from whether it is valid. It is obsolescent, which means it is currently a feature but could be considered for withdrawal in the future. – Eric Postpischil Dec 30 '22 at 13:24
  • @EricPostpischil here is a respone from you on the matter https://stackoverflow.com/a/18167747/5878272 – Fredrik Dec 30 '22 at 13:25
  • @Fredrik: Yes, so? That answer and my comments above are correct. The feature is obsolescent and valid, is not deprecated, and should not be used in new code. – Eric Postpischil Dec 30 '22 at 13:28
  • @EricPostpischil fair enough, I always thought it was deprecated but I was wrong :) – Fredrik Dec 30 '22 at 13:30
  • cant reproduce https://godbolt.org/z/fPxfY1YEP https://godbolt.org/z/8razoT9MT – 0___________ Dec 30 '22 at 13:39

2 Answers2

2

As the question is stated, this is not possible in C.

Passing the value of the pointer memory1 passes a copy of the pointer. The function may use that to access whatever memory1 is pointing at, but it has no way of changing memory1 itself. To do that, the function will need a pointer to memory1, i.e. &memory1.

nielsen
  • 5,641
  • 10
  • 27
0

You could pass the pointer to the pointer to modify it.

void myFree(void**freeMemoryPointer){
    /*some code*/
    *freeMemoryPointer=NULL;
}


int main(void){
    void *memory1 = mmap(NULL, getpagesize() , PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);//example
    printf("\nCurrent pointer: %p\n", memory1);
    myFree(&memory1);
    printf("\nShould show null: %p\n", memory1);
}

Compiles without warning and executes correctly in C & C++ ( https://godbolt.org/z/fPxfY1YEP https://godbolt.org/z/8razoT9MT)

koen
  • 5,383
  • 7
  • 50
  • 89
0___________
  • 60,014
  • 4
  • 34
  • 74