• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 10:43
CEST 16:43
KST 23:43
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
[ASL19] Finals Recap: Standing Tall9HomeStory Cup 27 - Info & Preview18Classic wins Code S Season 2 (2025)16Code S RO4 & Finals Preview: herO, Rogue, Classic, GuMiho0TL Team Map Contest #5: Presented by Monster Energy6
Community News
Flash Announces Hiatus From ASL50Weekly Cups (June 23-29): Reynor in world title form?12FEL Cracov 2025 (July 27) - $8000 live event16Esports World Cup 2025 - Final Player Roster16Weekly Cups (June 16-22): Clem strikes back1
StarCraft 2
General
The SCII GOAT: A statistical Evaluation The GOAT ranking of GOAT rankings How does the number of casters affect your enjoyment of esports? Statistics for vetoed/disliked maps Esports World Cup 2025 - Final Player Roster
Tourneys
RSL: Revival, a new crowdfunded tournament series [GSL 2025] Code S: Season 2 - Semi Finals & Finals $5,100+ SEL Season 2 Championship (SC: Evo) FEL Cracov 2025 (July 27) - $8000 live event HomeStory Cup 27 (June 27-29)
Strategy
How did i lose this ZvP, whats the proper response Simple Questions Simple Answers
Custom Maps
[UMS] Zillion Zerglings
External Content
Mutation # 480 Moths to the Flame Mutation # 479 Worn Out Welcome Mutation # 478 Instant Karma Mutation # 477 Slow and Steady
Brood War
General
Player “Jedi” cheat on CSL BGH Auto Balance -> http://bghmmr.eu/ Unit and Spell Similarities Help: rep cant save Flash Announces Hiatus From ASL
Tourneys
[BSL20] Grand Finals - Sunday 20:00 CET [Megathread] Daily Proleagues Small VOD Thread 2.0 [BSL20] GosuLeague RO16 - Tue & Wed 20:00+CET
Strategy
Simple Questions, Simple Answers I am doing this better than progamers do.
Other Games
General Games
Nintendo Switch Thread Stormgate/Frost Giant Megathread Path of Exile What do you want from future RTS games? Beyond All Reason
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread Trading/Investing Thread The Games Industry And ATVI
Fan Clubs
SKT1 Classic Fan Club! Maru Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece [\m/] Heavy Metal Thread
Sports
2024 - 2025 Football Thread NBA General Discussion Formula 1 Discussion TeamLiquid Health and Fitness Initiative For 2023 NHL Playoffs 2024
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
Blogs
Culture Clash in Video Games…
TrAiDoS
from making sc maps to makin…
Husyelt
Blog #2
tankgirl
StarCraft improvement
iopq
Trip to the Zoo
micronesia
Customize Sidebar...

Website Feedback

Closed Threads



Active: 762 users

The Big Programming Thread - Page 24

Forum Index > General Forum
Post a Reply
Prev 1 22 23 24 25 26 1031 Next
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.
mcc
Profile Joined October 2010
Czech Republic4646 Posts
November 16 2010 18:02 GMT
#461
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.
fabiano
Profile Blog Joined August 2009
Brazil4644 Posts
Last Edited: 2010-11-16 18:38:42
November 16 2010 18:34 GMT
#462
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.
"When the geyser died, a probe came out" - SirJolt
KaiserJohan
Profile Joined May 2010
Sweden1808 Posts
November 16 2010 19:48 GMT
#463
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.
England will fight to the last American
fabiano
Profile Blog Joined August 2009
Brazil4644 Posts
November 16 2010 20:03 GMT
#464
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.
"When the geyser died, a probe came out" - SirJolt
King K. Rool
Profile Blog Joined May 2009
Canada4408 Posts
Last Edited: 2010-11-16 22:33:02
November 16 2010 22:13 GMT
#465
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.
KaiserJohan
Profile Joined May 2010
Sweden1808 Posts
November 16 2010 22:23 GMT
#466
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
England will fight to the last American
King K. Rool
Profile Blog Joined May 2009
Canada4408 Posts
November 16 2010 22:25 GMT
#467
Ah too bad. Thanks for the input anyways. Off to stack overflow I go.
icystorage
Profile Blog Joined November 2008
Jollibee19343 Posts
Last Edited: 2010-11-16 22:57:12
November 16 2010 22:56 GMT
#468
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?
LiquidDota StaffAre you ready for a Miracle-? We are! The International 2017 Champions!
IMlemon
Profile Blog Joined May 2008
Lithuania296 Posts
November 16 2010 23:09 GMT
#469
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.
My future's so bright, I gotta wear shades.
mcc
Profile Joined October 2010
Czech Republic4646 Posts
November 16 2010 23:27 GMT
#470
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 ?
Snipinpanda
Profile Blog Joined October 2007
United States1227 Posts
November 16 2010 23:33 GMT
#471
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 >_>.
icystorage
Profile Blog Joined November 2008
Jollibee19343 Posts
November 16 2010 23:40 GMT
#472
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();
}

LiquidDota StaffAre you ready for a Miracle-? We are! The International 2017 Champions!
Phaded
Profile Joined August 2010
Australia579 Posts
Last Edited: 2010-11-17 00:25:39
November 17 2010 00:19 GMT
#473
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
I am down but I am far from over
icystorage
Profile Blog Joined November 2008
Jollibee19343 Posts
November 17 2010 00:21 GMT
#474
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.
LiquidDota StaffAre you ready for a Miracle-? We are! The International 2017 Champions!
imDerek
Profile Blog Joined August 2007
United States1944 Posts
November 17 2010 00:44 GMT
#475

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.
Least favorite progamers: Leta, Zero, Mind, Shine, free, really <-- newly added
mcc
Profile Joined October 2010
Czech Republic4646 Posts
November 17 2010 01:48 GMT
#476
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

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


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.

Frigo
Profile Joined August 2009
Hungary1023 Posts
November 18 2010 06:32 GMT
#477
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.
http://www.fimfiction.net/user/Treasure_Chest
KaiserJohan
Profile Joined May 2010
Sweden1808 Posts
Last Edited: 2010-11-18 15:41:27
November 18 2010 15:35 GMT
#478
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)

England will fight to the last American
Kambing
Profile Joined May 2010
United States1176 Posts
Last Edited: 2010-11-18 17:14:34
November 18 2010 17:07 GMT
#479
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?
mcc
Profile Joined October 2010
Czech Republic4646 Posts
November 19 2010 00:53 GMT
#480
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.
Prev 1 22 23 24 25 26 1031 Next
Please log in or register to reply.
Live Events Refresh
OSC
13:00
King of the Hill #216
davetesta87
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Lowko584
Hui .252
Vindicta 28
StarCraft: Brood War
Britney 57495
Calm 6793
Rain 3544
Shuttle 1426
Horang2 1401
EffOrt 1305
Hyuk 847
Larva 600
Light 294
BeSt 266
[ Show more ]
hero 201
Mini 166
Leta 164
ToSsGirL 113
Mind 87
Mong 75
Pusan 73
Snow 71
Hyun 62
Rush 49
Shinee 47
Barracks 45
JYJ39
Movie 26
soO 24
PianO 18
HiyA 13
Nal_rA 12
Backho 11
Shine 9
yabsab 8
IntoTheRainbow 8
SilentControl 7
zelot 3
Dota 2
Gorgc10538
qojqva2694
League of Legends
singsing2653
Other Games
Grubby2727
B2W.Neo1534
hiko948
DeMusliM571
Fuzer 292
XaKoH 261
ArmadaUGS126
QueenE25
Organizations
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 16 non-featured ]
StarCraft 2
• IndyKCrew
• sooper7s
• AfreecaTV YouTube
• Migwel
• intothetv
• LaughNgamezSOOP
• Kozan
StarCraft: Brood War
• Michael_bg 9
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• C_a_k_e 2657
• WagamamaTV410
League of Legends
• Nemesis6783
• Jankos1184
• TFBlade818
Upcoming Events
WardiTV European League
1h 17m
Scarlett vs Percival
Jumy vs ArT
YoungYakov vs Shameless
uThermal vs Fjant
Nicoract vs goblin
Harstem vs Gerald
FEL
1h 17m
Big Brain Bouts
1h 17m
Korean StarCraft League
12h 17m
CranKy Ducklings
19h 17m
RSL Revival
19h 17m
FEL
1d 1h
RSL Revival
1d 19h
FEL
1d 21h
BSL: ProLeague
2 days
Dewalt vs Bonyth
[ Show More ]
Replay Cast
3 days
Sparkling Tuna Cup
3 days
The PondCast
4 days
Replay Cast
5 days
RSL Revival
5 days
Replay Cast
6 days
RSL Revival
6 days
Liquipedia Results

Completed

Proleague 2025-06-28
HSC XXVII
Heroes 10 EU

Ongoing

JPL Season 2
BSL 2v2 Season 3
BSL Season 20
Acropolis #3
KCM Race Survival 2025 Season 2
CSL 17: 2025 SUMMER
Copa Latinoamericana 4
Championship of Russia 2025
RSL Revival: Season 1
Murky Cup #2
BLAST.tv Austin Major 2025
ESL Impact League Season 7
IEM Dallas 2025
PGL Astana 2025
Asian Champions League '25
BLAST Rivals Spring 2025
MESA Nomadic Masters
CCT Season 2 Global Finals
IEM Melbourne 2025

Upcoming

2025 ACS Season 2: Qualifier
CSLPRO Last Chance 2025
2025 ACS Season 2
CSLPRO Chat StarLAN 3
K-Championship
uThermal 2v2 Main Event
SEL Season 2 Championship
FEL Cracov 2025
Esports World Cup 2025
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.