2

For an embedded piece of code (avr-gcc) I am trying to reduce stack memory usage. So what I want to do is create a pointer, pass it to a function, and in the function, change the address the pointer points to, to the address of a heap allocated variable. This way, there would be no stack memory allocated inside main() for the testPointer.

I am trying it with the following code

#include <stdio.h>
char hello[18] = "Hello cruel world";
char* myfunc2() {
        return hello;
}
void myfunc(char *mypointer) {
        mypointer = myfunc2();
}
int main(){
        char *testPointer;
        printf("hello: %p\n", &hello);
        printf("test: %p\n", &testPointer);
        myfunc(testPointer);
        printf("test: %p\n", &testPointer);
        printf("test value: %s\n", testPointer);
        return 0;
}

but the testPointer address doesn't get reassigned. Of course in the real world use-case myfunc2 wouldn't be as simple, but it is returning a pointer to a heap allocated character array.

Output:

hello: 0x404030
test: 0x7ffe48724d38
test: 0x7ffe48724d38
test value: (null)
Dolf Andringa
  • 2,010
  • 1
  • 22
  • 36
  • You need to study the difference between static storage and heap. There should be no heap in your AVR program and there is no heap allocation in this source. Furthermore, doing what you are attempting as a way to reduce stack use is nonsense. Instead you should focus on what actually kills all the memory on the MCU, starting by throwing stdio.h into the garbage where it belongs. – Lundin Sep 25 '19 at 06:26

3 Answers3

5

You pass a pointer to the location you want to write to. Compare with:

#include <stdio.h>
char hello[18] = "Hello cruel world";
char* myfunc2() {
        return hello;
}
void myfunc(char **mypointer) {
        *mypointer = myfunc2();
}
int main(){
        char *testPointer;
        printf("hello: %p\n", &hello);
        printf("test: %p\n", &testPointer);
        myfunc(&testPointer);
        printf("test: %p\n", &testPointer);
        printf("test value: %s\n", testPointer);
        return 0;
}
Dan D.
  • 73,243
  • 15
  • 104
  • 123
2

You need to pass double pointer.

In general, if you want to change value of something in some other function, you need to pass address of that variable to the other function. Going by this, you are trying to change address stored in a variable, so you need to pass address of the pointer variable.

void myfunc(char **mypointer) { //Get address of a pointer variable
        if(NULL != mypointer ) //Check if variable is NULL
            *mypointer = myfunc2(); //Otherwise, update the pointer value
}

Call the function with address of the pointer variable:

myfunc(&testPointer);
MayurK
  • 1,925
  • 14
  • 27
1

I didn't really understand what you want to do but anyway, about your code there are 2 problems:

1) you pass function parameter by value. it should be passed by reference

2) you print the address of the variables by &hello: it means the address where the pointer is stored and not the address it points to (ie. the value of the variable hello).

You should do something like:

#include <stdio.h>
char hello[18] = "Hello cruel world";
char* myfunc2() {
        return hello;
}
void myfunc(char **mypointer) {
        *mypointer = myfunc2();
}
int main(){
        char *testPointer;
        printf("hello: %p\n", hello);
        printf("test: %p\n", testPointer);
        myfunc(&testPointer);
        printf("test: %p\n", testPointer);
        printf("test value: %s\n", testPointer);
        return 0;
}

and the output is:

hello: 0x601040                                                                                                             
test: (nil)                                                                                                                 
test: 0x601040                                                                                                              
test value: Hello cruel world

Note that the first time you print the value of the pointer testit is the null pointer. It is NOT the address where the variable testis stored.

But again, I don't see what this code is supposed to do...

Guillaume Petitjean
  • 2,408
  • 1
  • 21
  • 47