Generating a char* from a linked list of linked lists

C

Chris Ritchey

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.
 
M

Mark A. Odell

(e-mail address removed) (Chris Ritchey) wrote in
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.
 
B

Ben Pfaff

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.
 
C

Chris Ritchey

Ben Pfaff said:
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
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
| |-...
|-...
 
B

Ben Pfaff

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.
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.
 
M

MiniDisc_2k2

--
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).
 
M

Mark A. Odell

(e-mail address removed) (Chris Ritchey) wrote in
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.
 
E

emerth

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top