Generating a char* from a linked list of linked lists

Discussion in 'C Programming' started by Chris Ritchey, Jul 9, 2003.

  1. Hmmm I might scare people away from this one just by the title, or
    draw people in with a chalange :)

    I'm writting this program in c++, however I'm using char* instead of
    the string class, I am ordered by my instructor and she does have her
    reasons so I have to use char*. So there is alot of c in the code as
    well

    Anyways, I have a linked list of linked lists of a class we defined, I
    need to make all this into a char*, I know that I need to allocate
    them into one continuous chunk of data, ie remove all the pointers.
    The way that I'm currently doing this is by manually placing the data
    into the cahr* at appropriate placed using sizeof on the objects I'm
    placing into the the char*.
    I tried looking up people that were working with dynamic memory and
    char*'s so see if they were using tokens in their char* then searching
    for the tokens instead of just accessing the memory locations directly
    based on the sizeof operator. I am experiencing some mind boggling
    problems with the way I have it implemented, so I was wondering how
    other people are implementing a similiar situation. Would it be better
    to use tokens to seperate the data fields inside the char* or just
    leave it to pointer arithmatic...

    This is my first time converting anytype of data structure to a char*,
    infact I didn't know you could do just cast another pointerto a char*
    before I started working on this... sooo I'm basically looking for
    any advice on teh above. Thanks in advacne for any suggestions that
    are posted.
     
    Chris Ritchey, Jul 9, 2003
    #1
    1. Advertising

  2. (Chris Ritchey) wrote in
    news::

    > Hmmm I might scare people away from this one just by the title, or
    > draw people in with a chalange :)
    >
    > I'm writting this program in c++, however I'm using char* instead of
    > the string class, I am ordered by my instructor and she does have her
    > reasons so I have to use char*. So there is alot of c in the code as
    > well


    Doesn't really matter. It's got classes so it's not C. There is nothing
    illegal about using char * in C++ so it is topical there.

    --
    - Mark ->
    --
     
    Mark A. Odell, Jul 9, 2003
    #2
    1. Advertising

  3. Chris Ritchey

    Ben Pfaff Guest

    (Chris Ritchey) writes:

    > I'm writting this program in c++, however I'm using char* instead of
    > the string class, I am ordered by my instructor and she does have her
    > reasons so I have to use char*. So there is alot of c in the code as
    > well


    Well, so far you've managed to keep away from C++-specific
    issues. As long as you can do that, it's fine to talk about it
    in comp.lang.c. But if the discussion strays into C++ land,
    please drop comp.lang.c.

    > Anyways, I have a linked list of linked lists of a class we defined, I
    > need to make all this into a char*, I know that I need to allocate
    > them into one continuous chunk of data, ie remove all the pointers.
    > The way that I'm currently doing this is by manually placing the data
    > into the cahr* at appropriate placed using sizeof on the objects I'm
    > placing into the the char*.


    It's difficult to really give any concrete suggestions, because
    you haven't explained what you're trying to do. You have a
    linked list of linked lists and you want to get a char * out of
    it is all we really know. You haven't mentioned what kind of
    data the linked lists contain or what format you want the output
    to be in. You've implied that the output is a string, but that's
    all we know.

    Here's the kind of info that would be helpful:

    I have a linked list of data structures. Each of the lower
    level data structures consists of exactly one "key" string
    and zero or more "value" strings. I want to transform
    this linked list into a string that looks like this:

    key1=value1,value2;key2=value1;key3;key4=value1;etc.

    If you provide more information, maybe we can help.
    --
    "Welcome to the wonderful world of undefined behavior, where the demons
    are nasal and the DeathStation users are nervous." --Daniel Fox
     
    Ben Pfaff, Jul 9, 2003
    #3
  4. Ben Pfaff <> wrote in message news:<>...
    > (Chris Ritchey) writes:
    >
    > > I'm writting this program in c++, however I'm using char* instead of
    > > the string class, I am ordered by my instructor and she does have her
    > > reasons so I have to use char*. So there is alot of c in the code as
    > > well

    >
    > Well, so far you've managed to keep away from C++-specific
    > issues. As long as you can do that, it's fine to talk about it
    > in comp.lang.c. But if the discussion strays into C++ land,
    > please drop comp.lang.c.


    I love it when I get two posts one right after the other both telling
    me not to in that news group... Where else should I post? Actually
    this is more for Mark whom told me not to post in comp.lang.c than it
    is for Ben since he actually seams to want to help, thank you Ben!
    more info below

    > > Anyways, I have a linked list of linked lists of a class we defined, I
    > > need to make all this into a char*, I know that I need to allocate
    > > them into one continuous chunk of data, ie remove all the pointers.
    > > The way that I'm currently doing this is by manually placing the data
    > > into the cahr* at appropriate placed using sizeof on the objects I'm
    > > placing into the the char*.

    >
    > It's difficult to really give any concrete suggestions, because
    > you haven't explained what you're trying to do. You have a
    > linked list of linked lists and you want to get a char * out of
    > it is all we really know. You haven't mentioned what kind of
    > data the linked lists contain or what format you want the output
    > to be in. You've implied that the output is a string, but that's
    > all we know.


    I was trying not to be confusing in my original post but I guess I
    amde it too general, heres more specifics.

    The Data Structure I'm converting into a char* is called
    CommunicationGroup it contains 2 integers and a linked list. The
    linked list inside of CommunicationGroup is a linekd list of
    ObjectGroups which contains 3, well 1 int and two enums, integers and
    a linked lists of Objects. Objects consist of 3 integers. I put a
    "diagram" at the bottom of the post.

    As for the format of the string I do need to clarify, my bad. I'm
    actually not using the char* for output to the screen, it's used to
    send through a socket. The program we are writting is a network
    protocol so we need a way to put these data types into a char*. The
    same class will accept a char* to its constructor to fill in it's data
    membes.

    I hope thats enoughn formation, and qht aI was wondering about was,
    would it be better to insert a delimeter between the values in the
    char*(or char[], not sure how I should phrase that) or to leave them
    out and just access through pointer arithmatic only. for exaple how I
    have it now the layout of the string would look like this:
    <int><int><ObjectGroup><ObjectGroup>... Where <int> is an integer in
    the char. be better to use some character, say | to seperate them,
    thus the char* would become :
    <int>|<int>|<ObjectGroup>|<ObjectGroup>|...
    So, which way would be better? Performance is an issue and not so much
    memory. Is there another way that would be good instead?




    Diagram #1:
    a * indicates a pointer to that object in the linked list
    Communicationgroup:
    |-int
    |-int
    |-int
    |-*ObjectGroup:
    | |-int
    | |-int
    | |-int
    | |-*Object:
    | | |-int
    | | |-int
    | | |-int
    | |-*Object:
    | | |-int
    | | |-int
    | | |-int
    | |-...
    | *ObjectGroup*:
    | |-int
    | |-int
    | |-int
    | |-*Object:
    | | |-int
    | | |-int
    | | |-int
    | |-*Object:
    | | |-int
    | | |-int
    | | |-int
    | |-...
    |-...
     
    Chris Ritchey, Jul 10, 2003
    #4
  5. Chris Ritchey

    Ben Pfaff Guest

    (Chris Ritchey) writes:

    > Ben Pfaff <> wrote in message news:<>...
    > > (Chris Ritchey) writes:
    > >
    > > > I'm writting this program in c++, however I'm using char* instead of
    > > > the string class, I am ordered by my instructor and she does have her
    > > > reasons so I have to use char*. So there is alot of c in the code as
    > > > well

    > >
    > > Well, so far you've managed to keep away from C++-specific
    > > issues. As long as you can do that, it's fine to talk about it
    > > in comp.lang.c. But if the discussion strays into C++ land,
    > > please drop comp.lang.c.

    >
    > I love it when I get two posts one right after the other both telling
    > me not to in that news group... Where else should I post?


    It's pretty dangerous in general to post to both comp.lang.c and
    comp.lang.c++. C and C++ are related, but the preferred way to
    solve many kinds of problems in each is so different that it's
    really better to choose just one. Choosing which one is
    generally easy, because most programs are either C or C++, not
    both.

    > > > Anyways, I have a linked list of linked lists of a class we defined, I
    > > > need to make all this into a char*, I know that I need to allocate
    > > > them into one continuous chunk of data, ie remove all the pointers.
    > > > The way that I'm currently doing this is by manually placing the data
    > > > into the cahr* at appropriate placed using sizeof on the objects I'm
    > > > placing into the the char*.


    > The Data Structure I'm converting into a char* is called
    > CommunicationGroup it contains 2 integers and a linked list. The
    > linked list inside of CommunicationGroup is a linekd list of
    > ObjectGroups which contains 3, well 1 int and two enums, integers and
    > a linked lists of Objects. Objects consist of 3 integers. I put a
    > "diagram" at the bottom of the post.
    >
    > As for the format of the string I do need to clarify, my bad. I'm
    > actually not using the char* for output to the screen, it's used to
    > send through a socket. The program we are writting is a network
    > protocol so we need a way to put these data types into a char*. The
    > same class will accept a char* to its constructor to fill in it's data
    > membes.
    >
    > I hope thats enoughn formation, and qht aI was wondering about was,
    > would it be better to insert a delimeter between the values in the
    > char*(or char[], not sure how I should phrase that) or to leave them
    > out and just access through pointer arithmatic only. for exaple how I
    > have it now the layout of the string would look like this:
    > <int><int><ObjectGroup><ObjectGroup>... Where <int> is an integer in
    > the char. be better to use some character, say | to seperate them,
    > thus the char* would become :
    > <int>|<int>|<ObjectGroup>|<ObjectGroup>|...
    > So, which way would be better? Performance is an issue and not so much
    > memory. Is there another way that would be good instead?


    You have two choices here. I think you realize what they are,
    but I'll recap anyway. You can use a binary format, where you
    encode your data into bytes that efficiently represent it, but
    which is difficult for humans to read and often difficult to
    share among heterogeneous systems. Or you can use a text-based
    format, which may take more space and more time to encode and
    decode, but which is easy to read by eye and easily portable.
    Personally, I'd prefer the latter. In that case, you can use the
    C string functions (or a C++ string class) to construct a
    readable string. Parsing is a little harder, and you'd probably
    be best off writing a tokenization function to deal with lexical
    analysis and a parser function to deal with higher-level stuff.

    I'd suggest that you should worry about performance when it turns
    out to actually be too slow. Networks are generally a lot slower
    than CPUs, so it seems unlikely to be a real problem.
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
     
    Ben Pfaff, Jul 10, 2003
    #5
  6. Chris Ritchey

    MiniDisc_2k2 Guest


    > --
    > int main(void){char

    p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    > \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int

    putchar(\
    > );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof

    p-1;putchar(p\
    > );}return 0;}


    I tried to compile your signature. I got Ju and then got an access
    violation. Is it really supposed to work? (I know, I'm bored).
     
    MiniDisc_2k2, Jul 10, 2003
    #6
  7. (Chris Ritchey) wrote in
    news::

    >> > I'm writting this program in c++, however I'm using char* instead of
    >> > the string class, I am ordered by my instructor and she does have her
    >> > reasons so I have to use char*. So there is alot of c in the code as
    >> > well

    >>
    >> Well, so far you've managed to keep away from C++-specific
    >> issues. As long as you can do that, it's fine to talk about it
    >> in comp.lang.c. But if the discussion strays into C++ land,
    >> please drop comp.lang.c.

    >
    > I love it when I get two posts one right after the other both telling
    > me not to in that news group... Where else should I post? Actually
    > this is more for Mark whom told me not to post in comp.lang.c than it
    > is for Ben since he actually seams to want to help, thank you Ben!
    > more info below


    I don't mean to be unhelpful but it gets tiring asking people to lurk,
    read the topicality of the newsgroup, the C-FAQ, and then post. Don't be
    offended by simply being told the rules.

    --
    - Mark ->
    --
     
    Mark A. Odell, Jul 10, 2003
    #7
  8. Chris Ritchey

    emerth Guest

    As he said above in the thread, the data structures get
    sent through a socket (to some other process). send(...)
    requires a char * to the data to be sent.

    That is not an area I have enormous experience in, just
    moderately moderate experience...

    Nevertheless, here goes:

    If the data structure (top level & lower level) exists
    entirely contiguous in memory then I think you could
    cast the [DataStructure] pointer to a char * and start
    send()'ing. But any internal pointers in the data structure
    will be invalid at the recieving end - the recieving
    end will not have placed the data at the same location in
    memory as the sending end had it. If the data structure is
    just arrays then I think it could work, but you run risks
    if the sending and recieving machines are not the same
    architecture and OS.

    I think you want to marshal the data before sending it - not
    try to send actual data structures. Then use the data to instantiate
    new objects at the recieving end. Unless you are trying to
    send "live objects" through the wire?

    OK. I decided to answer this because I _think_ the original
    underlying question has more to do with sending data structures
    across sockets. Which is a technique that can be done well or
    poorly.

    If anyone with deeper experience in socket stream communication
    programming would care to get involved, this thread might be
    really cool.

    - Eric M




    "MiniDisc_2k2" <> wrote in message news:<pFZOa.7$Ez2.3@lakeread02>...
    > "Chris Ritchey" <> wrote in message
    > news:...
    > > Hmmm I might scare people away from this one just by the title, or
    > > draw people in with a chalange :)
    > >
    > > I'm writting this program in c++, however I'm using char* instead of
    > > the string class, I am ordered by my instructor and she does have her
    > > reasons so I have to use char*. So there is alot of c in the code as
    > > well
    > >
    > > Anyways, I have a linked list of linked lists of a class we defined, I
    > > need to make all this into a char*, I know that I need to allocate
    > > them into one continuous chunk of data, ie remove all the pointers.
    > > The way that I'm currently doing this is by manually placing the data
    > > into the cahr* at appropriate placed using sizeof on the objects I'm
    > > placing into the the char*.
    > > I tried looking up people that were working with dynamic memory and
    > > char*'s so see if they were using tokens in their char* then searching
    > > for the tokens instead of just accessing the memory locations directly
    > > based on the sizeof operator. I am experiencing some mind boggling
    > > problems with the way I have it implemented, so I was wondering how
    > > other people are implementing a similiar situation. Would it be better
    > > to use tokens to seperate the data fields inside the char* or just
    > > leave it to pointer arithmatic...
    > >
    > > This is my first time converting anytype of data structure to a char*,
    > > infact I didn't know you could do just cast another pointerto a char*
    > > before I started working on this... sooo I'm basically looking for
    > > any advice on teh above. Thanks in advacne for any suggestions that
    > > are posted.

    >
    > Would you mind telling us how this linked list relates to a char*? If it has
    > no relation:
    >
    > (assume that ll is the pointer to the first element in the outside set of
    > the linked list)
    >
    > C Version:
    > void* buffer = ll;
    > char* charll = buffer;
    >
    > C++ Version:
    > char* charll = reinterpret_cast<char*>(ll);
    >
    > If it does have some relationship to characters (for example, these linked
    > lists store single characters, and you're trying to bring them into an
    > entire string), it would be helpful to know what exactly the linked list
    > stores. Perhaps you could post your code for the definition of the class of
    > linked lists?
    >
    > BTW: Classes are not C, so I don't know how you're compiling it under a C
    > compiler (if you are)
     
    emerth, Jul 10, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    514
    emerth
    Jul 10, 2003
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    451
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,159
    Ian Collins
    May 9, 2006
  4. fool
    Replies:
    14
    Views:
    549
    Barry Schwarz
    Jul 3, 2006
  5. jawdoc
    Replies:
    9
    Views:
    811
    Chris Thomasson
    Mar 10, 2008
Loading...

Share This Page