LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Passing pointers through multiple functions. (https://www.linuxquestions.org/questions/programming-9/passing-pointers-through-multiple-functions-4175592278/)

yaplej 10-26-2016 11:30 AM

Passing pointers through multiple functions.
 
Hello,

I'm trying to pass a pointer through multiple layers of functions and then write data to the original memory where the first pointer was allocated but it keeps crashing.

Full code is available here. It's not homework just struggling to figure out what I am doing wrong. :)
https://github.com/OpenNOP/opennop

Example:
Code:

struct processor {
    __u8 *buffer;
}
struct record{
        __u8 thing;
}

int me_2nd(__u8 *buffer){
    struct record *this_record = NULL;
    this_record = (struct record *)buffer; // It's a __u8 pointer but cast to a structure pointer.  I think that's what I am doing at least.
    this_record->thing = 0; // <- Crash here!
    return 0;
}

int me_1st(__u8 *buffer){
    me_2nd(buffer);
    return 0;
}

void *start(void *dummyPtr) {
    struct processor *me = NULL;

    me = (struct processor*) dummyPtr;
    me->buffer = calloc(1, BUFSIZE + 400);
   
    me_1st(me->buffer);
    return NULL;
}


rtmistler 10-26-2016 12:58 PM

I think you're trying to be too complicated. You can send the address of a pointer.
Code:

unsigned char **pointer_to_a_pointer_to_an_unsigned_char;
unsigned char *pointer_to_an_unsigned_char;

some_function(&pointer_to_an_unsigned_char); // this sends the ADDRESS of the pointer_to_an_unsigned_char to the function and allows some_function to modify that pointer's contents.

// Same result
pointer_to_a_pointer_to_an_unsigned_char = &pointer_to_an_unsigned_char;
some_function(pointer_to_a_pointer_to_an_unsigned_char);

// Note: Prototype for this function can be either of these two, however the second one is the better way to do this to ensure people understand the intentions of the function:
void some_function(unsigned char *);
void some_function(unsigned char **);

// See also information about (int argc, char **argv), same sort of conventions


yaplej 10-26-2016 01:32 PM

I wrote a sample program and tested it but everything worked. So I might have been focusing on the wrong place in my code to begin with.

Here was my sample program.
Code:

#include <stdio.h>
#include <stdlib.h>
#include <linux/types.h>

#define BUFSIZE 2048

struct processor {
    __u8 *buffer;
};
struct record{
        __u8 thing;
};

int me_2nd(__u8 *buffer){
    struct record *this_record = NULL;
    this_record = (struct record *)buffer; // It's a __u8 pointer but cast to a structure pointer.  I think that's what I am doing at least.
    this_record->thing = 0; // <- Crash here so I thought!
    return 0;
}

int me_1st(__u8 *buffer){
    me_2nd(buffer);
    return 0;
}

int main(){
    struct processor me;

    me.buffer = calloc(1, BUFSIZE + 400);

    me_1st(me.buffer);
    return 0;
}


NevemTeve 10-27-2016 12:09 AM

Off: nowadays we have <inttypes.h> that gives us int8_t and uint8_t, so we don't have to rely on linux-specific types.

yaplej 10-27-2016 09:35 AM

My particular application is pretty specific to Linux. It possible I could add support BSD by using divert but so far there have been zero requests for that. Thanks for the info seems like a good thing to know.

sundialsvcs 10-28-2016 10:08 AM

Your code would be much clearer if you consistently refer to e.g. struct record * instead of using void * or __u8 *. This would eliminate the need for typecasting.

For instance, in struct processor, *buffer is properly a pointer to struct record. If you then consistently referred to the structures in the parameter-lists of the various functions, "C" could detect inconsistencies for you. All bets are off when you use typecasting ("C" has no choice but to believe whatever you say is true).

yaplej 10-28-2016 06:12 PM

That makes sense. I should be more consistent in passing structures and try to limit passing arbitrary types as parameters. I assume it's probably ok to typecast inside the function but keep it isolated in the function.

Thanks. I will have a lot of clean to do. :(


All times are GMT -5. The time now is 12:34 PM.