• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 10:51
CET 16:51
KST 00:51
  • 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
RSL Revival - 2025 Season Finals Preview8RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12
Community News
Weekly Cups (Dec 15-21): Classic wins big, MaxPax & Clem take weeklies3ComeBackTV's documentary on Byun's Career !10Weekly Cups (Dec 8-14): MaxPax, Clem, Cure win4Weekly Cups (Dec 1-7): Clem doubles, Solar gets over the hump1Weekly Cups (Nov 24-30): MaxPax, Clem, herO win2
StarCraft 2
General
What's the best tug of war? The Grack before Christmas Weekly Cups (Dec 15-21): Classic wins big, MaxPax & Clem take weeklies ComeBackTV's documentary on Byun's Career ! Micro Lags When Playing SC2?
Tourneys
OSC Season 13 World Championship $5,000+ WardiTV 2025 Championship $100 Prize Pool - Winter Warp Gate Masters Showdow Sparkling Tuna Cup - Weekly Open Tournament Winter Warp Gate Amateur Showdown #1
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 505 Rise From Ashes Mutation # 504 Retribution Mutation # 503 Fowl Play Mutation # 502 Negative Reinforcement
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ Recommended FPV games (post-KeSPA) BW General Discussion FlaSh on: Biggest Problem With SnOw's Playstyle soO on: FanTaSy's Potential Return to StarCraft
Tourneys
Small VOD Thread 2.0 [Megathread] Daily Proleagues [BSL21] LB QuarterFinals - Sunday 21:00 CET [BSL21] WB SEMIFINALS - Saturday 21:00 CET
Strategy
Simple Questions, Simple Answers Game Theory for Starcraft Current Meta Fighting Spirit mining rates
Other Games
General Games
Nintendo Switch Thread Stormgate/Frost Giant Megathread Beyond All Reason Path of Exile General RTS Discussion Thread
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
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Mafia Game Mode Feedback/Ideas Survivor II: The Amazon Sengoku Mafia TL Mafia Community Thread
Community
General
US Politics Mega-thread The Games Industry And ATVI Russo-Ukrainian War Thread How Does UI/UX Design Influence User Trust? Things Aren’t Peaceful in Palestine
Fan Clubs
White-Ra Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece
Sports
2024 - 2026 Football Thread Formula 1 Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List TL+ Announced Where to ask questions and add stream?
Blogs
National Diversity: A Challe…
TrAiDoS
I decided to write a webnov…
DjKniteX
James Bond movies ranking - pa…
Topin
Thanks for the RSL
Hildegard
Customize Sidebar...

Website Feedback

Closed Threads



Active: 2206 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
Next event in 1h 9m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
SKillous 133
Livibee 131
BRAT_OK 77
mouzStarbuck 77
StarCraft: Brood War
Sea 4861
Rain 4726
Shuttle 721
EffOrt 709
ggaemo 396
Mini 213
firebathero 204
Snow 193
Sharp 129
Barracks 126
[ Show more ]
hero 102
Hyun 90
JYJ 74
Sea.KH 74
Terrorterran 36
zelot 27
Sexy 24
JulyZerg 16
scan(afreeca) 14
Shine 13
yabsab 13
SilentControl 12
Bale 8
HiyA 5
Dota 2
qojqva5009
XcaliburYe777
Fuzer 219
League of Legends
Reynor97
Trikslyr0
Counter-Strike
zeus1100
edward191
Heroes of the Storm
Khaldor208
Other Games
singsing2126
B2W.Neo855
Mlord348
crisheroes347
XaKoH 130
Organizations
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 14 non-featured ]
StarCraft 2
• Light_VIP 62
• HeavenSC 32
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• Michael_bg 10
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• lizZardDota2125
Upcoming Events
Big Brain Bouts
1h 9m
Elazer vs Nicoract
Reynor vs Scarlett
Replay Cast
9h 9m
Sparkling Tuna Cup
1d 18h
Krystianer vs TBD
TriGGeR vs SKillous
Percival vs TBD
ByuN vs Nicoract
OSC
2 days
Replay Cast
2 days
Wardi Open
2 days
OSC
3 days
Solar vs MaxPax
ByuN vs Krystianer
Spirit vs TBD
OSC
6 days
Liquipedia Results

Completed

KCM Race Survival 2025 Season 4
WardiTV 2025
META Madness #9

Ongoing

C-Race Season 1
IPSL Winter 2025-26
BSL Season 21
Slon Tour Season 2
CSL Season 19: Qualifier 2
eXTREMESLAND 2025
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025

Upcoming

CSL 2025 WINTER (S19)
BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
Big Gabe Cup #3
OSC Championship Season 13
Nations Cup 2026
ESL Pro League Season 23
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
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.