Thread: Segfaulting/memory access for array (Linux)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    OK I will try to explain why I think the 3 pointers are justified.

    My threads have to communicate with each other and share common variables. Without using global variables or pipes between certain threads, I have used pointers to certain variables in a struct. I pass this struct to each thread upon creation(4th arg of pthread_create) and the thread can then read from the shared variables and edit/write to them as they wish. As a string of arrays is a double pointer already, unfortunately a pointer to this will be a tripple pointer.

    If i just used the double pointer as you said then I would not be able to change that array of strings in one thread, and have another thread read the changes etc as the string array would be local to the thread only.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Blasz View Post
    As a string of arrays is a double pointer already, unfortunately a pointer to this will be a tripple pointer.
    That's your mistake. A double pointer is a pointer. What does it point to? An array of strings. Does it have to always point to the same array? NO! You can reassign it to the value of some other double pointer and vice versa. You do not need any further "pointer to the pointer". This is why I suggested this:

    Code:
    typedef struct {
        char **array1;  // these are pointers to an array of strings!
        char **array2;
    } info;
        char **x = malloc(sizeof(char*)*10);  // an array of strings!
        a.array1 = x;
    Right now it points to the same thing as x (because a.array1 == x) but it can be reassigned to any other equivalent structure later (in fact, you could just malloc a.array1 and not bother with x and y -- malloc is an assignment, so you could still reassign it to something else afterward.)

    Now, if later you want to switch which array of strings a.array1 points to, eg, let's reverse them:
    Code:
    char **tmp;
    tmp = a.array1;
    a.array1 = a.array2;
    a.array2 = tmp;
    Presto! The double pointer does is not permanently attached to any particular array of strings (that would be something like "char array[10][16]", which is not a double pointer). It can be reassigned. So you still do not have a justification for the ***ptptp. The only way that could be justified is if at some point you want to use a.array1 as a pointer to an array of an array of strings:
    Code:
    char ***ptptp = malloc(sizeof(char**)*10);  // 10 arrays of strings;
    But if it is always just going to be an array of strings, you will never need that added level of indirection. All it is going to do is make your code more awkward, as this seg fault demonstrates. It will also make it less efficient, because this:
    Code:
    (*p)[5];
    Means loading the address in p, dereferencing it to the value it points to (so loading another address) whereas this:
    Code:
    p[5];
    Simply means loading one address "directly". Make sense?
    Last edited by MK27; 06-06-2010 at 12:37 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Blasz View Post
    If i just used the double pointer as you said then I would not be able to change that array of strings in one thread, and have another thread read the changes etc as the string array would be local to the thread only.
    Ah -- now I see what you are worried about. But you are passing the struct, not the double pointer:
    Code:
           pthread_create(&tid,NULL,thread, (void *)&a);
    Changes you make to members of "a" are not local to the thread/function, they will be global, because you are reassigning a member of the struct and not the pointer that was passed in. Reassigning "a" itself would be. Also this:
    Code:
        info b;
        b = *(info *)arg;
    Eek, should not even be allowed by the compiler. You do not need a whole new struct. You just need to do this:
    Code:
    info *b = (info*)arg;
    Trust me on this one.
    Last edited by MK27; 06-06-2010 at 12:49 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    OK I see now, thanks for explaining.

    I'm probably going to leave the code as it is though, because i have limited time, lots more to do and don't want to stuff up anything.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Blasz View Post
    I'm probably going to leave the code as it is though, because i have limited time, lots more to do and don't want to stuff up anything.
    Up to you, although those changes are not hard to make (making them sooner rather than later will be easier, obviously) and will probably make everything after that easier too. If this is for an assignment, your prof will not fail to spot that indiscretion either. And if anyone else has to read or work with your code, it will be confusing and irritating. Anyway, enjoy pthreads -- it's a whole other kettle of fish/pit of snakes.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows-Linux Comparison
    By Yarin in forum General Discussions
    Replies: 80
    Last Post: 02-14-2010, 05:10 PM
  2. Linux Version reccomendation
    By Pobega in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 10-05-2006, 06:48 PM
  3. Why Linux, for the average user?
    By Hunter2 in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 07-07-2006, 02:36 PM
  4. Which distribution of Linux should I get?
    By joshdick in forum A Brief History of Cprogramming.com
    Replies: 50
    Last Post: 01-19-2003, 09:26 AM
  5. Linux for Windows!
    By Strut in forum Linux Programming
    Replies: 2
    Last Post: 12-25-2002, 11:36 AM