:: Re: [DNG] Making sense of C pointer…
Página Inicial
Delete this message
Reply to this message
Autor: Emiliano Marini
Data:  
Para: Edward Bartolo
CC: dng
Assunto: Re: [DNG] Making sense of C pointer syntax.
Exactly, to modify a pointer you need a pointer to that pointer. Remember
in C, function parameters are "read only" because they're always copies of
the calling variable's values.

This is wrong:

function abcd(void *p) {
p = malloc(sizeof(int)); // Memory leaking
*p = 1;
}

main () {
int *p = malloc(sizeof(int)); // [1]
*p = 0;
abcd(p); // Sends a copy of the value of p

// p is still pointing to memory reserved by [1], so *p equals 0

}

This is OK:

function abcd(void **p) {
*p = malloc(sizeof(int));
**p = 1;
}

main() {
int i = 0;
int *p = &i; // p has the address where i is located

// Here *p is 0

efgh(&p); // Sends a copy of the address where p is stored

// Now p is pointing to some address in the heap segment, and *p is 1

}

IMHO you need to be careful when allocating memory inside functions. In
some cases it would be a good practice checking the value of the pointer
before allocating (!null) to avoid memory leaking. However, if you look at
this last example, p is pointing to local memory, and you can overwrite
it's value safely (so checking if p is not null before allocating wouldn't
be any help).

But, if you call this function twice, you will leak memory:

efgh(&p); // [1]
efgh(&p);
// Now you can't free the memory reserved at [1]

Cheers,
Emiliano.


On Mon, Mar 28, 2016 at 4:55 PM, Edward Bartolo <edbarx@???> wrote:

> Hi,
>
> Thanks for dedicating some of your time to answer me. I used:
>
> void change_value(void** ptr)
>
> Because I wanted to enable myself to allocate memory for the pointer
> inside the function, therefore I needed a pointer to a pointer of type
> void. Void allows such a function to handle different data types in
> which case an ordinal typed parameter may be used to allow a switch
> statement within the function to handle the different data types. I
> used such a construct in Delphi Pascal in the past, and consider it a
> powerful feature that can have its uses. This is why I am anxious to
> comprehend the why behind pointer to pointer use.
>
> This is a program employing the use of a pointer to change the value
> of a parameter inside a function.
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int afunc(double* dd) {
> *dd *= 2;
> }
>
> int main() {
> double* mm;
> mm = malloc(sizeof(double));
> printf("value of mm after creation, unassigned: %f\n", *mm);
> *mm = 1.0;
>
>   int j;
>   for (j = 1; j <= 20; j++) {
>     afunc(mm);
>     printf("value of mm after function call %d: %f\n", j, *mm);
>   }

>
> free(mm);
>
> return 0;
> }
>
> I will post tomorrow a reply illustrating the allocation of memory to
> a pointer within a function. This means the function must be able to
> modify the pointer not only its data. For that I will probably need a
> pointer to a pointer or use typecasting with a standard data type
> having the same number of bytes as a pointer. The reason for this is
> the fact that a pointer is a number.
>
> I found using the return value of a function makes code much more
> readable and probably more reliable. Multiple return values can be
> encapsulated inside a structure which would be returned by a function.
> I used this construct in simple-netaid-lightweight which avoids the
> use of GtkBuilder.
>
> Edward
> _______________________________________________
> Dng mailing list
> Dng@???
> https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng
>