LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-26-2016, 11:30 AM   #1
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu, openSuSE
Posts: 165
Blog Entries: 1

Rep: Reputation: 22
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;
}
 
Old 10-26-2016, 12:58 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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
 
Old 10-26-2016, 01:32 PM   #3
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu, openSuSE
Posts: 165

Original Poster
Blog Entries: 1

Rep: Reputation: 22
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;
}

Last edited by yaplej; 10-26-2016 at 01:56 PM.
 
Old 10-27-2016, 12:09 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
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.
 
Old 10-27-2016, 09:35 AM   #5
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu, openSuSE
Posts: 165

Original Poster
Blog Entries: 1

Rep: Reputation: 22
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.
 
Old 10-28-2016, 10:08 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,668
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
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).
 
Old 10-28-2016, 06:12 PM   #7
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu, openSuSE
Posts: 165

Original Poster
Blog Entries: 1

Rep: Reputation: 22
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python: passing functions or unevaluated code with multiple lines or expressions stateless Programming 1 11-30-2013 10:38 PM
Pointers/Passing arguments to functions question (C) smoothdogg00 Programming 7 03-17-2006 01:46 PM
C Programming: Help with Pointers to Functions devinWhalen Programming 2 03-04-2005 01:00 PM
question on pointers to functions h/w Programming 3 10-06-2003 04:51 PM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration