Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify the contents of the memory address of the return of a function

Tags:

c

return-value

Is it possible to modify the contents of the memory address of the return value of a function? functions return the value of a locally defined variable.

In the following example, compiled for my machine (x86-64) without warnings:

#include <stdio.h>

int get_val1()
{
  int ret = 1;
  return ret;
}

int get_val2()
{
  int ret = 2;
  return ret;
}

int get_val3()
{
  int ret = 3;
  return ret;
}

void redefine_ints(int *val1, int *val2, int *val3) {
  *val1 = 10;
  *val2 = 11;
  *val3 = 12;
}

void print_and_redefine_ints(int val1, int val2, int val3) {
  printf("val1 %d val2 %d val3 %d\n", val1, val2, val3);
  redefine_ints(&val1, &val2, &val3);
  printf("rval1 %d rval2 %d rval3 %d\n", val1, val2, val3);
}

int main()
{
  print_and_redefine_ints(get_val1(), get_val2(), get_val3());
  return 0;
}

I get the next output:

val1 1 val2 2 val3 3
rval1 10 rval2 11 rval3 12

This is the expected output, but how is it possible? Where are these variables stored?

like image 250
HenryGiraldo Avatar asked Jan 25 '19 14:01

HenryGiraldo


People also ask

How to access the memory address of a variable in C?

In order to access the memory address of a variable, val, prepend it with & sign. For example, &val returns the memory address of val. This memory address is assigned to a pointer and can be shared among various functions. For example, int *p = &val will assign the memory address of val to pointer p.

How to get the memory address of an object in Python?

In Python, everything is an object, from variables to lists and dictionaries everything is treated as objects. In this article, we are going to get the memory address of an object in Python. We can get an address using the id () function. id () function gives the address of the particular object.

How do you assign a memory address to a pointer?

This memory address is assigned to a pointer and can be shared among various functions. For example, int *p = &val will assign the memory address of val to pointer p. To access the content of the memory to which the pointer points, prepend it with a *.

How to get memory addresses in hexadecimal format?

It is also possible to get memory addresses in hexadecimal format. Here we will call the hex (address) function, to convert the memory address to hexadecimal representation.


1 Answers

A draw may explain more than some text. I'll use only 1 get_val1() in that example.

print_and_redefine_ints(get_val1());
                           |
                           |
                         [CALL]
                           |
                           |
                           V
                    int get_val1()
                    {
                        int ret = 1;<----------------------------------------------------+
                        return ret;                                                      |
                    }      |                                                             |
                           |                                                             |
                    [COPY OF VALUE]                                                      |
                           |                                                             |
                           |                                                             |
                           +---+                                                         |
                               |                                                         |
                               |                                                         |
                               V                                                         |
void print_and_redefine_ints(int val1) {                                                 |
    printf("val1 %d\n");        ^                                                        |
    redefine_ints(&val1);       |                                                        |
                    |           +--------------------------------------------+           |
                    |                                                        |           |
          [POINTER AKA REFERENCE]                                            |           |
                    |                                                        |           |
                     |                                                       |           |
                      V                                                      |           |
void redefine_ints(int *val1) {                                              |           |
    *val1 = 10; //<---- the value is changed, then its referenced value (this one, NOT THIS ONE) is changed too
}                     |
                      |
                      +---+
                          |
                    [VALUE CHANGED]
                          |
                          |
                          V
    printf("rval1 %d\n", val1);
    printf("original val1 %d\n", get_val1()); //if you add this line, you'll notice the returned value of get_val1() is still 1
}
like image 113
Cid Avatar answered Sep 22 '22 12:09

Cid