• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 06:16
CEST 12:16
KST 19:16
  • 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
[ASL22] Ro16 Preview: Holy Diver4[ASL22] Ro16 Preview: Rough Waters10[ASL22] Ro24 Preview: Siren's Call8[ASL22] Ro24 Preview: Summer's End9Serral wins HomeStory Cup 2915
Community News
Weekly Cups (Sep 7-12): SHIN, ByuN, MaxPax double down1StarCraft open world shooter announced at BlizzCon98Weekly Cups (Aug 30-Sep 7): herO thrives amid growing schism10Official StarCraft website teases new content ahead of BlizzCon?179Stellar Fest TWO the Moon (Dec 16-20)9
StarCraft 2
General
Weekly Cups (Sep 7-12): SHIN, ByuN, MaxPax double down How do you feel about the StarCraft shooter announcement at BlizzCon 2026? Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool StarCraft open world shooter announced at BlizzCon Balance hotfix patch 5.0.16b (July 16)
Tourneys
RSL Revival: Season 6 - Qualifiers and Main Event RSL goes to London! 2026 Offline Finals Nov 21-22 SC2 AI Tournament 2026 Fall Sparkling Tuna Cup - Weekly Open Tournament KSL Week #92
Strategy
[G] Having the right mentality to improve
Custom Maps
Nexus Wars 2021 GUIDE [M] (2) Industrial Park
External Content
Mutation # 543 Enhanced Defenses The PondCast: SC2 News & Results Mutation # 542 The Ascended Mutation # 541 Binary Choice
Brood War
General
Remastered crash fix suggestions ASL22 General Discussion Why Protoss Struggles on ASL (Ft. Shuttle, Tulbo) BGH Auto Balance -> http://bghmmr.eu/ Official StarCraft website teases new content ahead of BlizzCon?
Tourneys
[ASL22] Ro16 Group D [Megathread] Daily Proleagues [ASL22] Ro16 Group C [ASL22] Ro16 Group B
Strategy
Replay Review Process - What do you do? Simple Questions, Simple Answers Odyssey Mineral Stack Saturation Game Theory for Starcraft
Other Games
General Games
Nintendo Switch Thread Warcraft III: The Frozen Throne Stormgate/Frost Giant Megathread EVE Corporation Diablo IV
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
[TL LoL EUW IHs] Teemo shall perish
Heroes of the Storm
Heroes of the Storm 2.0
Hearthstone
Deck construction bug
TL Mafia
TL Mafia Community Thread
Community
General
US Politics Mega-thread Post Your Watch! Canadian Politics Mega-thread Russo-Ukrainian War Thread Artificial Intelligence Thread
Fan Clubs
MarineLorD Fan Club The Creator Fan Club The ShoWTimE Fan Club
Media & Entertainment
[Manga] One Piece Movie Discussion! Diablo Animated Series on Netflix
Sports
Football (Soccer) Thread TeamLiquid Health and Fitness Initiative For 2023 MLB/Baseball 2023
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List Northern Ireland Global Starcraft
Blogs
Virtual Romance, Real-Life C…
TrAiDoS
Regacy Esports:Our Goa…
regacyesports
Dreaming of BW patches (mod…
c3rberUs
LOCKPICKING NOOB
LUCKY_NOOB
Customize Sidebar...

Website Feedback

Closed Threads



Active: 5607 users

The Big Programming Thread - Page 24

Forum Index > General Forum
Post a Reply
Prev 1 22 23 24 25 26 1032 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
Jollibee19350 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
Jollibee19350 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
Jollibee19350 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 1032 Next
Please log in or register to reply.
Live Events Refresh
The PondCast
10:00
Episode 109
CranKy Ducklings12
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
ProTech104
StarCraft: Brood War
Shuttle 1260
Bisu 1160
firebathero 748
actioN 697
Horang2 437
EffOrt 281
Soma 211
Mini 184
Soulkey 181
Snow 129
[ Show more ]
ZerO 95
Light 93
Rush 90
Killer 84
Larva 81
Shine 73
Stork 68
Hm[arnc] 48
Last 39
910 38
hero 35
Barracks 34
Pusan 29
Sharp 25
sSak 24
soO 22
Britney 0
Dota 2
ODPixel224
League of Legends
Reynor78
Counter-Strike
olofmeister1137
kennyS772
edward39
Super Smash Bros
Mew2King72
Other Games
singsing1514
byalli863
JimRising 379
XaKoH 188
Sick186
ZerO(Twitch)9
Organizations
Other Games
gamesdonequick721
StarCraft: Brood War
Kim Chul Min (afreeca) 601
Other Games
BasetradeTV41
[ Show 13 non-featured ]
StarCraft 2
• StrangeGG 47
• CranKy Ducklings SOOP8
• Letter147
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• Migwel
StarCraft: Brood War
• iopq 14
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Jankos2340
Upcoming Events
Kung Fu Cup
44m
Replay Cast
13h 44m
KCM Race Survival
23h 44m
IntoTheTV X SOOP
1d
Cure vs SHIN
herO vs Solar
Percival vs Classic
ByuN vs Rogue
OSC
1d 11h
Replay Cast
1d 13h
IntoTheTV X SOOP
2 days
Replay Cast
2 days
GSL
3 days
Replay Cast
3 days
[ Show More ]
GSL
4 days
Shopify Rebellion Sundays
4 days
Spirit vs Mixu
Replay Cast
4 days
Afreeca Starleague
4 days
Shine vs Rush
WardiTV Weekly
5 days
Monday Night Weeklies
5 days
Sparkling Tuna Cup
5 days
Afreeca Starleague
5 days
Light vs EffOrt
PiGosaur Cup
6 days
Liquipedia Results

Completed

Acropolis #5 - TRS
Blizzard Classic Cup 2026
Big Dog Cup 2026 Div 1

Ongoing

KCM Race Survival 2026 Season 3
K-JUNGMAN
ASL Season 22
Super Anchor Qualifying S3
CSL 2026 AUTUMN (S22)
Acropolis #5
Calamity Invitational
FISSURE Playground #3
BLAST Open Fall 2026
Esports World Cup 2026
BLAST Bounty Summer 2026
BLAST Bounty Summer Qual
Stake Ranked Episode 3
XSE Pro League 2026

Upcoming

Acropolis #5 - GSA
Acropolis #5 - GSB
Acropolis #5 - GSC
SC4ALL II: Brood War
HSC XXX
Stellar Fest 2: Lunar Cup
SC4ALL II: StarCraft II
Kung Fu Cup 2026 Grand Finals
RSL Offline Finals
Copium Cup
PGL Major Singapore 2026
Stake Ranked Episode 6
BLAST Rivals Fall 2026
IEM Beijing 2026
Stake Ranked Episode 5
PGL Masters Bucharest 2026
1win Private Club #2
Thunderpick World Champ. '26
ESL Pro League Season 24
Stake Ranked Episode 4
1win Private Club #1
Logitech G Play Connect 2026
SL StarSeries Fall 2026
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 © 2026 TLnet. All Rights Reserved.