473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Generating a char* from a linked list of linked lists

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.
Jul 19 '05 #1
7 4789
re*****@yahoo.com (Chris Ritchey) wrote in
news:48**************************@posting.google.c om:
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 ->
--
Jul 19 '05 #2
re*****@yahoo.com (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;et c.

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
Jul 19 '05 #3
Ben Pfaff <bl*@cs.stanford.edu> wrote in message news:<87************@pfaff.Stanford.EDU>...
re*****@yahoo.com (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
| |-...
|-...
Jul 19 '05 #4
re*****@yahoo.com (Chris Ritchey) writes:
Ben Pfaff <bl*@cs.stanford.edu> wrote in message news:<87************@pfaff.Stanford.EDU>...
re*****@yahoo.com (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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\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[i]\
);}return 0;}
Jul 19 '05 #5
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\ \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[i]\ );}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).
Jul 19 '05 #6
re*****@yahoo.com (Chris Ritchey) wrote in
news:48*************************@posting.google.co m:
> 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 ->
--
Jul 19 '05 #7
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" <Ma******@nospam.com> wrote in message news:<pFZOa.7$Ez2.3@lakeread02>...
"Chris Ritchey" <re*****@yahoo.com> wrote in message
news:48**************************@posting.google.c om...
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)

Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Chris Ritchey | last post by:
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...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
1
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
4
by: MJ | last post by:
Hi I have written a prog for reversing a linked list I have used globle pointer Can any one tell me how I can modify this prog so that I dont have to use extra pointer Head1. When I reverse a LL...
33
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
12
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
19
by: Dongsheng Ruan | last post by:
with a cell class like this: #!/usr/bin/python import sys class Cell: def __init__( self, data, next=None ): self.data = data
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
1
by: ziroi | last post by:
Hello! I am new here to the forums and still learning C. I understand your policies on coursework, but I have an error that I've run around and around and can't figure out where my problem is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.