|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On November 17 2010 01:54 KaiserJohan wrote:Show nested quote +On November 16 2010 22:46 mcc wrote:On November 16 2010 21:50 KaiserJohan wrote: Got a C#/general question:
Consider I have a custom class,
public class test { private int a,b; private string c;
<get / set functions, and also serialize/deserialize methods so I can stick it the class object into a byte[] and send it over a socket> }
The problem is whenever I receive data on the other side, an event is triggered executes a receieve function which reads the data from a buffer and deserializes it back into my custom class test -- however, sometimes when I read from the buffer, number of bytes equals to 1½ or 2 classes might've been in the buffer already so when I deserialize, it deserializes into 1 complete class object, but throws the rest of the data away, so I dont get complete information.
The solution would be to know how big the class is, so I can only read from the buffer up to a certain index.... but does the size of the class object vary with the size of its variables? For example, would string c = "kekeke" generate a smaller object than c= "kekekeekkeekeke"? In that case, how do I know how much to read form the buffer?
Ahh, I didn't notice your last question String c = "kekeke" generates object of the same size as c= "kekekeekkeekeke", but that is because the actual "string" is not stored within the object, rather that object contains pointer to the memory storing the "string". But as I said in serialization/deserialization size of the object is not the only thing you need, you need also sizes of referenced objects and size of memory blocks referenced by those, and that is not fixed. So basically if you do not send size of deserialized object along with the data, you cannot know beforehand how big a chunk of the buffer you need. You would need to parse the buffer yourself to determine where one deserialized object ends and another begins. Sorry if the formulation is not clear, if this is so, hopefully someone else will put it more clearly. This case is one reason I think all programmers should know C/C++ (or some other language with pointers). Because C#, Java, PHP, .... shelters you from pointers, but they are actually there all the time, and many things make so much more sense when you know that. /end of rant  @ mcc No, the formulation is crystal clear.  I've done a fair bit of C programming during my studies, such as constructing a small OS in MIPS assembler / C, so I know a fair bit about pointers and memory management, although I prefer high-level languages such as C#. While I have done some C I havn't done any c++ programming actually, the windows API scares me with all the gazillion function/initialization and stuff; it's not the actual problems concerning memory and pointers (and wierd debugging messages) but I guess I got to get around learning that someday.. I can imagine something like this might've been easier to do in C++ though funny enough, when it comes to serialization; I mean wouldn't it just be to point at the begining of the allocated memory for that object and send it over the socket? I guess it could be done in C# too, with the Marshall class (I think it allows unmanaged code like pointers) @morfildur: Out of all those 3, the first solution seems pretty sweet, I'm gonna have a go at it. Maybe second would work, but how do I know the serializer wont possibly add that chunk of 4 bytes? I can feel your pain, debugging with my server app(since it's a client-server based chat, everything gets sent to server which redirects it to the other clients) is a pain with all the possible asynchronous calls for all the clients. I use TCP connection for this, but I'm thinking of adding a UDP socket for a networkstream for voice communication -- still wondering how that would work with encoding and decoding though, but maybe the provided decoder library would handle things such as end-markers for decoding for me... But yes network programming is pretty fun to do atleast, I'm just making stuff for fun / practice, and a client-server chat/VOIP like mIRC+ventrilo combined is a good goal. (wouldn't have any ideas about another field/type of program besides network programmign that could be a good practice? especially a c++ based app) I just hope you didn't take my rant at the end personally, it was a separate rant to point out how hard it is sometimes to explain things without assuming the other person knows what pointer is, I was not saying that you don't, there was no way to know. If I knew I could have cut most of my post out  I don't it would be much easier in C++, I think it is probably the same, but yes Marshal allows you to work with kind of pointers 
Sending the bytecount is easiest and I do not think the cons apply to your case, so.
|
On November 17 2010 01:54 KaiserJohan wrote: But yes network programming is pretty fun to do atleast, I'm just making stuff for fun / practice, and a client-server chat/VOIP like mIRC+ventrilo combined is a good goal. (wouldn't have any ideas about another field/type of program besides network programmign that could be a good practice? especially a c++ based app)
Game programming (OpenGL, SDL, XNA) Mobile programming (Android and iPhone programming are sooooo easy to learn) Web programming (Python, Django, PHP)
Edit:
Once you know OO, you dont need to stick with only one OO language, when learning a new OO language all you need is just to get a grip on the syntax of that language.
The same is worth for the rest of the programming paradigms.
|
On November 17 2010 03:34 fabiano wrote:Show nested quote +On November 17 2010 01:54 KaiserJohan wrote: But yes network programming is pretty fun to do atleast, I'm just making stuff for fun / practice, and a client-server chat/VOIP like mIRC+ventrilo combined is a good goal. (wouldn't have any ideas about another field/type of program besides network programmign that could be a good practice? especially a c++ based app)
Game programming (OpenGL, SDL, XNA) Mobile programming (Android and iPhone programming are sooooo easy to learn) Web programming (Python, Django, PHP) Edit: Once you know OO, you dont need to stick with only one OO language, when learning a new OO language all you need is just to get a grip on the syntax of that language. The same is worth for the rest of the programming paradigms.
Just out of curiosity, is the API for smartphones good? I mean I could very well imagine touch-based controlls to be tricky to program. I don't have a smartphone unfortunately so I can't try it.
|
Its very easy actually.
All you need to do is to implement a callback that will be called when some event (the touch) occurs. Who identifies when and what event occurred is the OS, and the programmer only has to worry about the implementation of the behaviour triggered by the event.
I dont own a smartphone either, I use the simulators the IDEs provide.
What could be tricky are the controls for games, but I didnt learn how to program games for iPhone or Android yet 
If you want to try out mobile programming, I advice you to take a look at Android, since its free and you can program in Windows, Linux or Mac, unlike iPhone where you need a goddamn Mac.
|
Might be a dumb question and perhaps the wrong place to ask, but if I have an app in VS2010 with a target framework of 3.5, and I change it to 4.0, will it automatically use WPF version 4.0 as well?
Could not find this anywhere (or I'm just wording my search wrong). I mean, I can't find it explicitly stated anywhere, but whenever there's something about .Net 4.0 and WPF, it's always about WPF 4.0, so it seems like the .Net framework dictates which WPF version you're using, but I just want to be sure.
Hm I find this "All future releases of WPF will be part of the .NET Framework." which seems to imply that the .Net version dictates which WPF version you're using. Wouldn't mind hearing another opinion still.
edit: Stackoverflow tells me yes, so I'm gonna go with that for now.
|
That would be my guess aswell but I'm not certain.
Btw anyone knows when .net 4 will become a standard or available on windows updates? It sucks Windows 7 was shipped with 3.5 (AFAIK, or 3) and they couldnt press out .net 4 in time
|
Ah too bad. Thanks for the input anyways. Off to stack overflow I go.
|
hey guys, i ask for your help again, i asked for help on the algorithm in how to insert data at the beginning, middle and end of the list. Our prof asked us to code it and when I did, it crashed.
i believe the problem is here. + Show Spoiler +while(i < n) { printf("Enter the element:"); scanf("%s", str); list[i] = str; i++; } for (i=0; i<n; i++) { printf("%d. %s\n", i+1, list[i]); }
i tried inputting the list first and displaying it, but when it reaches the bolded area, it crashes. Is there something wrong on my input of the list?
|
On November 17 2010 07:56 icystorage wrote:hey guys, i ask for your help again, i asked for help on the algorithm in how to insert data at the beginning, middle and end of the list. Our prof asked us to code it and when I did, it crashed. i believe the problem is here. + Show Spoiler +while(i < n) { printf("Enter the element:"); scanf("%s", str); list[i] = str; i++; } for (i=0; i<n; i++) { printf("%d. %s\n", i+1, list[i]); }
i tried inputting the list first and displaying it, but when it reaches the bolded area, it crashes. Is there something wrong on my input of the list?
Assuming the "list" you're talking about is some sort of implementation of linked list, adding a new element isn't that easy. What you're doing in your code is assigning a new values to elemenst of array. List is dynamic structure where each element contains a pointer to the next element, so to insert a new one you have to update the pointers of previous and next elements. And if you have no idea what I'm talking about google for "C linked list tutorial" or something like that :p.
edit: after rereading your post it's posssible you just need to add new elements to an array? If so, your program crashes because there isn't enough memory allocated for the array. Look into malloc and realloc functions.
|
On November 17 2010 07:56 icystorage wrote:hey guys, i ask for your help again, i asked for help on the algorithm in how to insert data at the beginning, middle and end of the list. Our prof asked us to code it and when I did, it crashed. i believe the problem is here. + Show Spoiler +while(i < n) { printf("Enter the element:"); scanf("%s", str); list[i] = str; i++; } for (i=0; i<n; i++) { printf("%d. %s\n", i+1, list[i]); }
i tried inputting the list first and displaying it, but when it reaches the bolded area, it crashes. Is there something wrong on my input of the list?
Could you post a bigger chunk of your code ?
|
On November 17 2010 07:56 icystorage wrote:hey guys, i ask for your help again, i asked for help on the algorithm in how to insert data at the beginning, middle and end of the list. Our prof asked us to code it and when I did, it crashed. i believe the problem is here. + Show Spoiler +while(i < n) { printf("Enter the element:"); scanf("%s", str); list[i] = str; i++; } for (i=0; i<n; i++) { printf("%d. %s\n", i+1, list[i]); }
i tried inputting the list first and displaying it, but when it reaches the bolded area, it crashes. Is there something wrong on my input of the list?
Uh, this is C right? I believe you need a terminating null character, or else printf is going to try to print as many characters as it can from that array till it hits a null character, which would most certainly be outside that array.
I could be wrong, because I don't remember the behavior of printf, and that scanf looks sketchy too, but I don't remember how that works either >_>.
|
On November 17 2010 08:09 IMlemon wrote:Show nested quote +On November 17 2010 07:56 icystorage wrote:hey guys, i ask for your help again, i asked for help on the algorithm in how to insert data at the beginning, middle and end of the list. Our prof asked us to code it and when I did, it crashed. i believe the problem is here. + Show Spoiler +while(i < n) { printf("Enter the element:"); scanf("%s", str); list[i] = str; i++; } for (i=0; i<n; i++) { printf("%d. %s\n", i+1, list[i]); }
i tried inputting the list first and displaying it, but when it reaches the bolded area, it crashes. Is there something wrong on my input of the list? Assuming the "list" you're talking about is some sort of implementation of linked list, adding a new element isn't that easy. What you're doing in your code is assigning a new values to elemenst of array. List is dynamic structure where each element contains a pointer to the next element, so to insert a new one you have to update the pointers of previous and next elements. And if you have no idea what I'm talking about google for "C linked list tutorial" or something like that :p. edit: after rereading your post it's posssible you just need to add new elements to an array? If so, your program crashes because there isn't enough memory allocated for the array. Look into malloc and realloc functions.
uh, i dont think its a linked list so i have to make my own list first.
On November 17 2010 08:27 mcc wrote:Show nested quote +On November 17 2010 07:56 icystorage wrote:hey guys, i ask for your help again, i asked for help on the algorithm in how to insert data at the beginning, middle and end of the list. Our prof asked us to code it and when I did, it crashed. i believe the problem is here. + Show Spoiler +while(i < n) { printf("Enter the element:"); scanf("%s", str); list[i] = str; i++; } for (i=0; i<n; i++) { printf("%d. %s\n", i+1, list[i]); }
i tried inputting the list first and displaying it, but when it reaches the bolded area, it crashes. Is there something wrong on my input of the list? Could you post a bigger chunk of your code ?
okay, here it is + Show Spoiler + #include <stdio.h>
void insert(int n,char new_data,int loc, char list[]) { while(n>loc) { list[n+1] = list[n]; n = n-1; } list[loc] = new_data; }
void insert_start(int n, char new_data, char list[]) { while(n>=0) { list[n+1] = list[n]; n = n-1; } list[0] = new_data; }
void insert_end (int n, char new_data, char list[]) { list[n+1] = new_data; }
void del(int n,int loc, char list[]) { int x = 0; x = n; list[loc] = NULL; while(loc>n) { list[n-1] = list[n]; n = n-1; } list[x] = NULL; }
void del_end (int n, char list[]) { list[n] = NULL; }
void del_start(int n, char list[]) { list[0] = NULL; while(n>=0) { list[n-1] = list[n]; n = n-1; } }
int main() { int n = 0, loc = 0, choice = 0, i = 0, add_choice = 0; char new_data, str[20]; printf("Enter the number of elements on the list:"); scanf("%d/n", &n); char list[n]; while(i < n) { printf("Enter the element:"); scanf("%s", str); list[i] = str; i++; } for (i=0; i<n; i++) { printf("%d. %s\n", i+1, list[i]); } printf("Enter 1 to add, 2 to delete"); scanf("%d", &choice); if (choice == 1) { printf("Enter data to be added:"); scanf("%s", new_data); printf("Enter 1 to add at the beginning, 2 at the middle and 3 at the end."); scanf("%d", &add_choice); if (add_choice == 1) insert_start(n, new_data, list); else if (add_choice == 2) { printf("Where would you like to add the data?"); scanf("%d", loc); insert(n, new_data, loc, list); } else if (add_choice == 3) insert_end (n, new_data, list); else printf("It's none of the choices"); } getch(); }
|
On November 17 2010 08:40 icystorage wrote:Show nested quote +On November 17 2010 08:09 IMlemon wrote:On November 17 2010 07:56 icystorage wrote:hey guys, i ask for your help again, i asked for help on the algorithm in how to insert data at the beginning, middle and end of the list. Our prof asked us to code it and when I did, it crashed. i believe the problem is here. + Show Spoiler +while(i < n) { printf("Enter the element:"); scanf("%s", str); list[i] = str; i++; } for (i=0; i<n; i++) { printf("%d. %s\n", i+1, list[i]); }
i tried inputting the list first and displaying it, but when it reaches the bolded area, it crashes. Is there something wrong on my input of the list? Assuming the "list" you're talking about is some sort of implementation of linked list, adding a new element isn't that easy. What you're doing in your code is assigning a new values to elemenst of array. List is dynamic structure where each element contains a pointer to the next element, so to insert a new one you have to update the pointers of previous and next elements. And if you have no idea what I'm talking about google for "C linked list tutorial" or something like that :p. edit: after rereading your post it's posssible you just need to add new elements to an array? If so, your program crashes because there isn't enough memory allocated for the array. Look into malloc and realloc functions. uh, i dont think its a linked list so i have to make my own list first. Show nested quote +On November 17 2010 08:27 mcc wrote:On November 17 2010 07:56 icystorage wrote:hey guys, i ask for your help again, i asked for help on the algorithm in how to insert data at the beginning, middle and end of the list. Our prof asked us to code it and when I did, it crashed. i believe the problem is here. + Show Spoiler +while(i < n) { printf("Enter the element:"); scanf("%s", str); list[i] = str; i++; } for (i=0; i<n; i++) { printf("%d. %s\n", i+1, list[i]); }
i tried inputting the list first and displaying it, but when it reaches the bolded area, it crashes. Is there something wrong on my input of the list? Could you post a bigger chunk of your code ? okay, here it is + Show Spoiler + #include <stdio.h>
void insert(int n,char new_data,int loc, char list[]) { while(n>loc) { list[n+1] = list[n]; n = n-1; } list[loc] = new_data; }
void insert_start(int n, char new_data, char list[]) { while(n>=0) { list[n+1] = list[n]; n = n-1; } list[0] = new_data; }
void insert_end (int n, char new_data, char list[]) { list[n+1] = new_data; }
void del(int n,int loc, char list[]) { int x = 0; x = n; list[loc] = NULL; while(loc>n) { list[n-1] = list[n]; n = n-1; } list[x] = NULL; }
void del_end (int n, char list[]) { list[n] = NULL; }
void del_start(int n, char list[]) { list[0] = NULL; while(n>=0) { list[n-1] = list[n]; n = n-1; } }
int main() { int n = 0, loc = 0, choice = 0, i = 0, add_choice = 0; char new_data, str[20]; printf("Enter the number of elements on the list:"); scanf("%d/n", &n); char list[n]; while(i < n) { printf("Enter the element:"); scanf("%s", str); list[i] = str; i++; } for (i=0; i<n; i++) { printf("%d. %s\n", i+1, list[i]); } printf("Enter 1 to add, 2 to delete"); scanf("%d", &choice); if (choice == 1) { printf("Enter data to be added:"); scanf("%s", new_data); printf("Enter 1 to add at the beginning, 2 at the middle and 3 at the end."); scanf("%d", &add_choice); if (add_choice == 1) insert_start(n, new_data, list); else if (add_choice == 2) { printf("Where would you like to add the data?"); scanf("%d", loc); insert(n, new_data, loc, list); } else if (add_choice == 3) insert_end (n, new_data, list); else printf("It's none of the choices"); } getch(); }
What compiler are you using? It seems that are trying to dynamically allocate an array, which may or may not work under certain compilers
http://stackoverflow.com/questions/737240/c-c-array-size-at-run-time-w-o-dynamic-allocation-is-allowed
Edit: Wow I fail for missing this.
You are trying to assign an array of characters to a character in the line
list[i] = str;
you defined list to be char list[n]; which is just a list of characters (ie a string)
what you might be looking for is a list of strings, ie char list[n][MAX_CHAR_LENGTH] which will give you a list of n strings wtih MAX_CHAR_LENGTH characters in length
|
On November 17 2010 09:19 Phaded wrote:Show nested quote +On November 17 2010 08:40 icystorage wrote:On November 17 2010 08:09 IMlemon wrote:On November 17 2010 07:56 icystorage wrote:hey guys, i ask for your help again, i asked for help on the algorithm in how to insert data at the beginning, middle and end of the list. Our prof asked us to code it and when I did, it crashed. i believe the problem is here. + Show Spoiler +while(i < n) { printf("Enter the element:"); scanf("%s", str); list[i] = str; i++; } for (i=0; i<n; i++) { printf("%d. %s\n", i+1, list[i]); }
i tried inputting the list first and displaying it, but when it reaches the bolded area, it crashes. Is there something wrong on my input of the list? Assuming the "list" you're talking about is some sort of implementation of linked list, adding a new element isn't that easy. What you're doing in your code is assigning a new values to elemenst of array. List is dynamic structure where each element contains a pointer to the next element, so to insert a new one you have to update the pointers of previous and next elements. And if you have no idea what I'm talking about google for "C linked list tutorial" or something like that :p. edit: after rereading your post it's posssible you just need to add new elements to an array? If so, your program crashes because there isn't enough memory allocated for the array. Look into malloc and realloc functions. uh, i dont think its a linked list so i have to make my own list first. On November 17 2010 08:27 mcc wrote:On November 17 2010 07:56 icystorage wrote:hey guys, i ask for your help again, i asked for help on the algorithm in how to insert data at the beginning, middle and end of the list. Our prof asked us to code it and when I did, it crashed. i believe the problem is here. + Show Spoiler +while(i < n) { printf("Enter the element:"); scanf("%s", str); list[i] = str; i++; } for (i=0; i<n; i++) { printf("%d. %s\n", i+1, list[i]); }
i tried inputting the list first and displaying it, but when it reaches the bolded area, it crashes. Is there something wrong on my input of the list? Could you post a bigger chunk of your code ? okay, here it is + Show Spoiler + #include <stdio.h>
void insert(int n,char new_data,int loc, char list[]) { while(n>loc) { list[n+1] = list[n]; n = n-1; } list[loc] = new_data; }
void insert_start(int n, char new_data, char list[]) { while(n>=0) { list[n+1] = list[n]; n = n-1; } list[0] = new_data; }
void insert_end (int n, char new_data, char list[]) { list[n+1] = new_data; }
void del(int n,int loc, char list[]) { int x = 0; x = n; list[loc] = NULL; while(loc>n) { list[n-1] = list[n]; n = n-1; } list[x] = NULL; }
void del_end (int n, char list[]) { list[n] = NULL; }
void del_start(int n, char list[]) { list[0] = NULL; while(n>=0) { list[n-1] = list[n]; n = n-1; } }
int main() { int n = 0, loc = 0, choice = 0, i = 0, add_choice = 0; char new_data, str[20]; printf("Enter the number of elements on the list:"); scanf("%d/n", &n); char list[n]; while(i < n) { printf("Enter the element:"); scanf("%s", str); list[i] = str; i++; } for (i=0; i<n; i++) { printf("%d. %s\n", i+1, list[i]); } printf("Enter 1 to add, 2 to delete"); scanf("%d", &choice); if (choice == 1) { printf("Enter data to be added:"); scanf("%s", new_data); printf("Enter 1 to add at the beginning, 2 at the middle and 3 at the end."); scanf("%d", &add_choice); if (add_choice == 1) insert_start(n, new_data, list); else if (add_choice == 2) { printf("Where would you like to add the data?"); scanf("%d", loc); insert(n, new_data, loc, list); } else if (add_choice == 3) insert_end (n, new_data, list); else printf("It's none of the choices"); } getch(); }
What compiler are you using? It seems that are trying to dynamically allocate an array, which may or may not work under certain compilers http://stackoverflow.com/questions/737240/c-c-array-size-at-run-time-w-o-dynamic-allocation-is-allowed
im using Jen's File Editor (JFE) at first then tried it on DEV-C++. It crashes on both.
|
scanf("%d/n", &n); char list[n];
this is okay but i would rather use malloc for dynamically allocating storages (and remember to call free at the end). dynamically allocating stuff on the stack might be prone to stack overflow without warning.
scanf("%s", str); list[i] = str;
This doesn't make sense. see above post. You probably want char** list instead.
|
On November 17 2010 09:19 Phaded wrote:Show nested quote +On November 17 2010 08:40 icystorage wrote:On November 17 2010 08:09 IMlemon wrote:On November 17 2010 07:56 icystorage wrote:hey guys, i ask for your help again, i asked for help on the algorithm in how to insert data at the beginning, middle and end of the list. Our prof asked us to code it and when I did, it crashed. i believe the problem is here. + Show Spoiler +while(i < n) { printf("Enter the element:"); scanf("%s", str); list[i] = str; i++; } for (i=0; i<n; i++) { printf("%d. %s\n", i+1, list[i]); }
i tried inputting the list first and displaying it, but when it reaches the bolded area, it crashes. Is there something wrong on my input of the list? Assuming the "list" you're talking about is some sort of implementation of linked list, adding a new element isn't that easy. What you're doing in your code is assigning a new values to elemenst of array. List is dynamic structure where each element contains a pointer to the next element, so to insert a new one you have to update the pointers of previous and next elements. And if you have no idea what I'm talking about google for "C linked list tutorial" or something like that :p. edit: after rereading your post it's posssible you just need to add new elements to an array? If so, your program crashes because there isn't enough memory allocated for the array. Look into malloc and realloc functions. uh, i dont think its a linked list so i have to make my own list first. On November 17 2010 08:27 mcc wrote:On November 17 2010 07:56 icystorage wrote:hey guys, i ask for your help again, i asked for help on the algorithm in how to insert data at the beginning, middle and end of the list. Our prof asked us to code it and when I did, it crashed. i believe the problem is here. + Show Spoiler +while(i < n) { printf("Enter the element:"); scanf("%s", str); list[i] = str; i++; } for (i=0; i<n; i++) { printf("%d. %s\n", i+1, list[i]); }
i tried inputting the list first and displaying it, but when it reaches the bolded area, it crashes. Is there something wrong on my input of the list? Could you post a bigger chunk of your code ? okay, here it is + Show Spoiler + #include <stdio.h>
void insert(int n,char new_data,int loc, char list[]) { while(n>loc) { list[n+1] = list[n]; n = n-1; } list[loc] = new_data; }
void insert_start(int n, char new_data, char list[]) { while(n>=0) { list[n+1] = list[n]; n = n-1; } list[0] = new_data; }
void insert_end (int n, char new_data, char list[]) { list[n+1] = new_data; }
void del(int n,int loc, char list[]) { int x = 0; x = n; list[loc] = NULL; while(loc>n) { list[n-1] = list[n]; n = n-1; } list[x] = NULL; }
void del_end (int n, char list[]) { list[n] = NULL; }
void del_start(int n, char list[]) { list[0] = NULL; while(n>=0) { list[n-1] = list[n]; n = n-1; } }
int main() { int n = 0, loc = 0, choice = 0, i = 0, add_choice = 0; char new_data, str[20]; printf("Enter the number of elements on the list:"); scanf("%d/n", &n); char list[n]; while(i < n) { printf("Enter the element:"); scanf("%s", str); list[i] = str; i++; } for (i=0; i<n; i++) { printf("%d. %s\n", i+1, list[i]); } printf("Enter 1 to add, 2 to delete"); scanf("%d", &choice); if (choice == 1) { printf("Enter data to be added:"); scanf("%s", new_data); printf("Enter 1 to add at the beginning, 2 at the middle and 3 at the end."); scanf("%d", &add_choice); if (add_choice == 1) insert_start(n, new_data, list); else if (add_choice == 2) { printf("Where would you like to add the data?"); scanf("%d", loc); insert(n, new_data, loc, list); } else if (add_choice == 3) insert_end (n, new_data, list); else printf("It's none of the choices"); } getch(); }
What compiler are you using? It seems that are trying to dynamically allocate an array, which may or may not work under certain compilers http://stackoverflow.com/questions/737240/c-c-array-size-at-run-time-w-o-dynamic-allocation-is-allowedEdit: Wow I fail for missing this. You are trying to assign an array of characters to a character in the line list[i] = str; you defined list to be char list[n]; which is just a list of characters (ie a string) what you might be looking for is a list of strings, ie char list[n][MAX_CHAR_LENGTH] which will give you a list of n strings wtih MAX_CHAR_LENGTH characters in length
That would solve this problem, but his functions for adding new values still won't work. So some problems in this code: a) what the others pointed out about using char[n] as a list of strings, list of strings is char[m][n] or char** b) your functions for adding new values are wrong. Your list is initialized with initial n elements so in insert_end when you try to do list[n+1]=new_data (this is also actually error as it should be list[n] = new_data because you started from 0) will probably cause runtime error because you are trying to assign stuff to a memory that you did not allocate. Same goes for insert_start basically. Also you are not keeping number of elements in the list anywhere. In the beginning it is n, but after insert_X it is still n, in your example it won't matter because you allow adding only once, but if you tried to call insert twice results would be incorrect. c) Delete functions leave your list with holes, so subsequent calls do not work correctly, unless a lot of unnecessary logic would be added
Anyway as to how to solve it, first you have to decide if you want to keep using array to represent the list or really start using linked list, I support linked list as it is much easier and nicer solution.
But if you still want to use array, you should use char** list as your list and allocate it like this : list = (char**)malloc(n * sizeof(char*)); also you need to reallocate the array every time you add new element to the list or remove element from the list, which is pain in the ass and inefficient. You can make it more efficient by allocating more than necessary at that point and just keep track of what is actually used and what is not.
But using array is very inefficient and cumbersome compared to linked list, so I definitely think you should switch your implementation to use linked list.
|
Stop using arrays people. They are allocated on the heap, are cumbersome to use and has other pitfalls as well. Use vector<string> instead of that crap. And use streams instead of scanf, getline can read lines.
|
Hmm, anyone know if there really is no way of getting the size of a class instance in C#? sizeof only returns the size of the pointers, and Marshal.Sizeof does not work on class instances.
It kinda makes the #1 solution to object-to-byte streaming hard :p
Anyone know any other way of getting size of a class instance in C#? (imho it feels like a major limitation if its not possible)
|
On November 19 2010 00:35 KaiserJohan wrote:Hmm, anyone know if there really is no way of getting the size of a class instance in C#? sizeof only returns the size of the pointers, and Marshal.Sizeof does not work on class instances. It kinda makes the #1 solution to object-to-byte streaming hard :p Anyone know any other way of getting size of a class instance in C#? (imho it feels like a major limitation if its not possible)
There's no way to easily and programmatically extract the size of a reference type (i.e., non-pod class). You could use reflection or a profiling tool to calculate or observe the size of an instance of a reference type, but these approaches are either cumbersome (for the former) or brittle (for the latter).
The real question I'd pose to use is why you aren't using .NET's serialization library to write save your objects to byte streams instead?
|
On November 18 2010 15:32 Frigo wrote: Stop using arrays people. They are allocated on the heap, are cumbersome to use and has other pitfalls as well. Use vector<string> instead of that crap. And use streams instead of scanf, getline can read lines.
I assumed he is working in C, in C++ I would still not use vector in his case, because they are supposed to learn how to actually implement data structures, so having it ready would defeat the purpose. Of course in ideal case he should not use arrays nor vector, but linked list.
|
|
|
|