Like Tree1Likes

looping struct

This is a discussion on looping struct within the C Programming forums, part of the General Programming Boards category; Originally Posted by chihwahli Its a way to write an looping buffer. Going to use it in a program with ...

  1. #16
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Quote Originally Posted by chihwahli View Post
    Its a way to write an looping buffer. Going to use it in a program with 2 threads that share this buffer. One creates numbers and the other gets them off the buffer.
    THREADS?!?
    Do you realise that threading is basically the hardest topic that there is?!
    You have how many years of C programming experience???
    AndiPersti likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  2. #17
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    Quote Originally Posted by AndiPersti View Post
    Why don't you use an array instead of misusing a struct?

    The data structure you want to use is called a queue.

    Bye, Andreas
    yes, you are right. Structs are used to keep hold a set of data that belongs together, like name, address data.

    Will use array. This thread was informative very usefull nevertheless.

    Edit:
    @imalc: Many years experience not. I am picking up my old C program that I was creating, it partially works but something is wrong with the semaphores, and mutex. Time to rap things up and finish things. If I were thinking like you said, that it's the hardest thing, well, where should I go in the future? Just stay in bed and think everything is difficult? God gave us a brain to use. So I intend to.
    It might be a good way to say positive and encouraging things, instead of saying: no it's too difficult, etc.
    Personalities are shaped with what you say to others. Every word has a positive or negavtive charge to it.

    Don't you agree? Now , let us write code iMalc!
    Last edited by chihwahli; 03-22-2013 at 02:44 AM.

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    5,830
    Quote Originally Posted by chihwahli View Post
    Will use array. This thread was informative very usefull nevertheless.
    Depends on your definition of informative. Until AndiPersti suggested using an array, the thread was full of misinformation - or information that is not always true.

    A basic problem with the advice given is that layout of structs is implementation dependent. One consequence of this is that incrementing a pointer to one member is not guaranteed to give the address of the next.
    Code:
    #include <stdio.h>
    struct X
    {
        int a1;
        int a2;
    };
    
    int main()
    {
         struct X x;
    
         int *p1 = &(x.a1);
         int  *p2 = p1 + 1; 
    
         if (p2 == &(x.a2))
             printf("Worked\n");
         else
             printf("Did not work\n");
         return 0;
    }
    While it is true that struct members will appear in order, there is nothing preventing a compiler from inserting padding between them. So, the output of this code is 100% compiler dependent. It can vary between compilers from different vendors. It can change when you update your compiler.
    Right 98% of the time, and don't care about the other 3%.

  4. #19
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    Thanks Grumpy, you mean this:
    what is structure padding - C / C++

    There are ways to calculate it if I recall it right. Thanks for bringing it up. I might have missed it and guess what's wrong...

  5. #20
    Registered User
    Join Date
    Jun 2005
    Posts
    5,830
    Sure, there are ways to calculate what padding is between struct members. But doesn't having to do that work against your wish to easily iterate over members? Arrays have none of that sort of problem and, unlike structs, are meant to be iterated over.
    Right 98% of the time, and don't care about the other 3%.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  2. 2 problems: Struct array and pointer + struct
    By v1n1c1u5 in forum C Programming
    Replies: 0
    Last Post: 12-13-2009, 04:38 PM
  3. Looping struct info
    By kokoro in forum C Programming
    Replies: 4
    Last Post: 11-26-2009, 09:36 AM
  4. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  5. Replies: 1
    Last Post: 05-05-2004, 06:58 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21