• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 05:43
CEST 11:43
KST 18: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
Team TLMC #5 - Finalists & Open Tournaments0[ASL20] Ro16 Preview Pt2: Turbulence10Classic Games #3: Rogue vs Serral at BlizzCon9[ASL20] Ro16 Preview Pt1: Ascent10Maestros of the Game: Week 1/Play-in Preview12
Community News
BSL 2025 Warsaw LAN + Legends Showmatch0Weekly Cups (Sept 8-14): herO & MaxPax split cups4WardiTV TL Team Map Contest #5 Tournaments1SC4ALL $6,000 Open LAN in Philadelphia8Weekly Cups (Sept 1-7): MaxPax rebounds & Clem saga continues29
StarCraft 2
General
#1: Maru - Greatest Players of All Time Weekly Cups (Sept 8-14): herO & MaxPax split cups Team Liquid Map Contest #21 - Presented by Monster Energy SpeCial on The Tasteless Podcast Team TLMC #5 - Finalists & Open Tournaments
Tourneys
Maestros of The Game—$20k event w/ live finals in Paris Sparkling Tuna Cup - Weekly Open Tournament SC4ALL $6,000 Open LAN in Philadelphia WardiTV TL Team Map Contest #5 Tournaments RSL: Revival, a new crowdfunded tournament series
Strategy
Custom Maps
External Content
Mutation # 491 Night Drive Mutation # 490 Masters of Midnight Mutation # 489 Bannable Offense Mutation # 488 What Goes Around
Brood War
General
Soulkey on ASL S20 ASL TICKET LIVE help! :D BW General Discussion NaDa's Body A cwal.gg Extension - Easily keep track of anyone
Tourneys
[ASL20] Ro16 Group D [ASL20] Ro16 Group C [Megathread] Daily Proleagues BSL 2025 Warsaw LAN + Legends Showmatch
Strategy
Simple Questions, Simple Answers Muta micro map competition Fighting Spirit mining rates [G] Mineral Boosting
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread Path of Exile Borderlands 3 General RTS Discussion Thread
Dota 2
Official 'what is Dota anymore' discussion LiquidDota to reintegrate into TL.net
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
Community
General
US Politics Mega-thread Canadian Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread UK Politics Mega-thread
Fan Clubs
The Happy Fan Club!
Media & Entertainment
Movie Discussion! [Manga] One Piece Anime Discussion Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion MLB/Baseball 2023
World Cup 2022
Tech Support
Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread High temperatures on bridge(s)
TL Community
BarCraft in Tokyo Japan for ASL Season5 Final The Automated Ban List
Blogs
I <=> 9
KrillinFromwales
The Personality of a Spender…
TrAiDoS
A very expensive lesson on ma…
Garnet
hello world
radishsoup
Lemme tell you a thing o…
JoinTheRain
RTS Design in Hypercoven
a11
Evil Gacha Games and the…
ffswowsucks
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1575 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
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 1031 Next
Please log in or register to reply.
Live Events Refresh
Next event in 17m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Rex 2
StarCraft: Brood War
Calm 5699
Bisu 731
Hyuk 162
HiyA 96
Hyun 96
sorry 86
ToSsGirL 84
Dewaltoss 82
Pusan 77
Light 74
[ Show more ]
Soma 60
actioN 56
Mini 49
ZerO 32
BeSt 30
Nal_rA 28
soO 27
Liquid`Ret 26
Sharp 24
Rush 19
Free 16
SilentControl 10
Dota 2
singsing1508
XcaliburYe237
boxi98170
League of Legends
JimRising 381
Counter-Strike
olofmeister1583
shoxiejesuss629
allub166
Other Games
XaKoH 143
NeuroSwarm75
Trikslyr15
Organizations
Other Games
gamesdonequick599
StarCraft: Brood War
lovetv 593
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 14 non-featured ]
StarCraft 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• iopq 1
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Jankos1421
• Stunt679
Other Games
• WagamamaTV81
Upcoming Events
RSL Revival
17m
Maru vs Reynor
Cure vs TriGGeR
Rex2
Map Test Tournament
1h 17m
The PondCast
3h 17m
RSL Revival
1d
Zoun vs Classic
Korean StarCraft League
1d 17h
BSL Open LAN 2025 - War…
1d 22h
RSL Revival
2 days
BSL Open LAN 2025 - War…
2 days
RSL Revival
3 days
Online Event
3 days
[ Show More ]
Wardi Open
4 days
Monday Night Weeklies
4 days
Sparkling Tuna Cup
5 days
LiuLi Cup
6 days
Liquipedia Results

Completed

Proleague 2025-09-10
Chzzk MurlocKing SC1 vs SC2 Cup #2
HCC Europe

Ongoing

BSL 20 Team Wars
KCM Race Survival 2025 Season 3
BSL 21 Points
ASL Season 20
CSL 2025 AUTUMN (S18)
LASL Season 20
RSL Revival: Season 2
Maestros of the Game
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

Upcoming

2025 Chongqing Offline CUP
BSL World Championship of Poland 2025
IPSL Winter 2025-26
BSL Season 21
SC4ALL: Brood War
BSL 21 Team A
Stellar Fest
SC4ALL: StarCraft II
EC S1
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
MESA Nomadic Masters Fall
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
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.