• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 16:55
CEST 22:55
KST 05:55
  • 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] Ro24 Preview: Siren's Call8[ASL22] Ro24 Preview: Summer's End9Serral wins HomeStory Cup 2915Serral wins Maestros of the Game 244ByuL, and the Limitations of Standard Play3
Community News
Official StarCraft website teases new content ahead of BlizzCon?3Stellar Fest TWO the Moon (Dec 16-20)8Weekly Cups (August 24-30): Patches' balance mod takes over2New 3v3 BGH Ladder (and more) on ShieldBattery!40Weekly Cups (August 17-23): Zerg dominate the week6
StarCraft 2
General
Nexon wins bid to develop StarCraft IP content, distribute Overwatch mobile game SC4ALL: II Winner Will Earn a Spot at HSC 30! Balance hotfix patch 5.0.16b (July 16) September World Ranking: herO leads, Serral climbs Weekly Cups (August 10-16): SHIN doubles
Tourneys
IntoTheTV X SOOP SC2 League : Weekly & Monthly SC2 INu's Battles#20 [BO.9] 2026 GSTL Announcement Stellar Fest TWO the Moon (Dec 16-20) PIG STY FESTIVAL 8.0! (13 - 23 August)
Strategy
[G] Having the right mentality to improve
Custom Maps
Nexus Wars 2021 GUIDE [M] (2) Industrial Park
External Content
Mutation # 541 Binary Choice The PondCast: SC2 News & Results Mutation # 540 Dodge This Mutation # 539 Thunder Dome
Brood War
General
Official StarCraft website teases new content ahead of BlizzCon? [Personal Project Share] Terran Defense v0.60 BW General Discussion BGH Auto Balance -> http://bghmmr.eu/ XUN appreciation thread
Tourneys
KCM Race Survival 2026 Season 3 [Megathread] Daily Proleagues BSL LAN Party - Kraków 29-30 August - OPEN SIGNUPS [ASL22] Ro24 Group F
Strategy
Replay Review Process - What do you do? Game Theory for Starcraft Odyssey Mineral Stack Saturation Fighting Spirit mining rates
Other Games
General Games
Diablo IV EVE Corporation Nintendo Switch Thread [Maplestory Hardcore] Let's Play~!! General RTS Discussion Thread
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
[TL LoL EUW IHs] Teemo shall perish TSM pausing esports and CLG Dead
Heroes of the Storm
Heroes of the Storm 2.0
Hearthstone
Deck construction bug
TL Mafia
TL Mafia Power Rank TL Mafia Community Thread NeO.D_StephenKing vs This Guy From 1 Million Dance
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine Artificial Intelligence Thread Russo-Ukrainian War Thread Canadian Politics Mega-thread
Fan Clubs
MarineLorD Fan Club The Creator Fan Club The ShoWTimE Fan Club
Media & Entertainment
Movie Discussion! Anime Discussion Thread
Sports
Football (Soccer) Thread TeamLiquid Health and Fitness Initiative For 2023 MLB/Baseball 2023 NBA General Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List Northern Ireland Global Starcraft
Blogs
Regacy Esports: The Bh…
regacyesports
Violent Games and Crime Rate…
TrAiDoS
LOCKPICKING NOOB
LUCKY_NOOB
Cathedral Of CS And NY pizza a…
FuDDx
Please support my new stand…
Peanutsc
Customize Sidebar...

Website Feedback

Closed Threads



Active: 4457 users

The Big Programming Thread - Page 392

Forum Index > General Forum
Post a Reply
Prev 1 390 391 392 393 394 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.
EscPlan9
Profile Blog Joined December 2006
United States2777 Posts
Last Edited: 2013-11-12 21:59:34
November 12 2013 21:57 GMT
#7821
Really should just initialize the loop variable inside the loops. Cleans up the code a bunch:


#include <stdio.h>

int main()
{
int array[10][10];
int count;

for ( int i = 0; i<10; ++i )
{
for (int j = 0; j<10; ++j )
{
array[i][j] = count;
count++;
}
}

for (int i = 0; i<10; ++i )
{
for (int j = 0; j<10; ++j )
{
printf( "%d", array[i][j] );
printf( " " );
}
printf("\n");

}
}


Undefeated TL Tecmo Super Bowl League Champion
bangsholt
Profile Joined June 2011
Denmark138 Posts
Last Edited: 2013-11-12 21:59:28
November 12 2013 21:58 GMT
#7822
A two simple rules for for-loops in C89:

Declare loop variable outside of for loop
Always initialize loop variable *at least* in for loop -> for(i = 0; i < 10; i++)

So do that and remove the i = 0; j = 0; in the middle of the file.

Edit for EscPlan9: That's not legal C89, which the style he uses with declaring the loop variable outside of the for loop indicates

An additional rule is to *ALWAYS* initialize your variables - this is not <insert language> where all variables are automatically some value if not initialized properly.
Yoshi-
Profile Joined October 2008
Germany10227 Posts
Last Edited: 2013-11-12 22:01:46
November 12 2013 22:01 GMT
#7823
The first loop may not work properly since you don't initialize it
Arnstein
Profile Blog Joined May 2010
Norway3381 Posts
November 12 2013 22:06 GMT
#7824
Yeah, coming from C++ (this is my first C code), I tried to initialize inside of for in the beginning, but as bangsholt pointed out, it won't work.

And thanks for the tip bangsholt, will definitely do the i = 0 inside of the for from now on!
rsol in response to the dragoon voice being heard in SCII: dragoon ai reaches new lows: wanders into wrong game
misirlou
Profile Joined June 2010
Portugal3316 Posts
November 12 2013 22:17 GMT
#7825
whattttttttttt i said the same thing
spinesheath
Profile Blog Joined June 2009
Germany8679 Posts
November 12 2013 22:32 GMT
#7826
Didn't you feel something is odd when you had the statement
i;

in your for loop?
If you have a good reason to disagree with the above, please tell me. Thank you.
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2013-11-12 22:39:50
November 12 2013 22:39 GMT
#7827
Can anyone recommend to me UML drawing tools please? I need to draw a class diagram, use case, sequence diagram, interface design (optionally). I feel MS Word can't help me much.
Cyx.
Profile Joined November 2010
Canada806 Posts
Last Edited: 2013-11-13 00:31:51
November 13 2013 00:30 GMT
#7828
On November 13 2013 07:01 Yoshi- wrote:
The first loop may not work properly since you don't initialize it

Should probably emphasize this - even if your code gives you the result you expect, there's no guarantees i is getting initialized to the zero value you want it to, and so the behaviour of your code is undefined currently. You should just make sure to initialize the value in the for loop's initialization statement like people have said, that's what it's there for.

e: quoting the people above who said pretty much the same thing for emphasis:

On November 13 2013 06:40 misirlou wrote:
Show nested quote +
On November 13 2013 06:36 Arnstein wrote:
Oh, of course! Added i = 0 and j = 0 between the loops now, and now it works. Thanks!

j=0 between the outer fors (the i for's) won't acomplish what you want because j comes out of the first i iteration with value 10, and if you don't reset it, it stays 10 (which you do reset on the j=0; part of the for) and it would just print the first row.
Also dont forget to set i=0 before the first for.
You're welcome

The j=0 next to the i=0 isn't really accomplishing anything, because atm it will assign it 0 again when it enters the loop. If you do get a habbit of setting the variables like that and happen to forget j=0 inside the i loop, it will only print the first row. Also, you're not setting i=0 before the first loop. Depending on compilers, systems and randomness, your program will not work because i will have a random value.

On November 13 2013 06:57 EscPlan9 wrote:
Really should just initialize the loop variable inside the loops. Cleans up the code a bunch:



NihiLStarcraft
Profile Blog Joined January 2010
Denmark1413 Posts
November 13 2013 00:44 GMT
#7829
On November 13 2013 07:39 darkness wrote:
Can anyone recommend to me UML drawing tools please? I need to draw a class diagram, use case, sequence diagram, interface design (optionally). I feel MS Word can't help me much.


I like draw.io - works very well for me and it can be integrated into Google docs if you're using that. It's entirely web-based too, so no need to download a program. (If that's not your thing, try StarUML, I've used that before and - while dated - it does the job just as well).
Arnstein
Profile Blog Joined May 2010
Norway3381 Posts
November 13 2013 07:36 GMT
#7830
On November 13 2013 07:17 misirlou wrote:
whattttttttttt i said the same thing


aww, I thanked you too! <3
rsol in response to the dragoon voice being heard in SCII: dragoon ai reaches new lows: wanders into wrong game
mcc
Profile Joined October 2010
Czech Republic4646 Posts
Last Edited: 2013-11-13 11:31:26
November 13 2013 11:28 GMT
#7831
On November 13 2013 06:44 Arnstein wrote:
I did like this, and it works fine now:
+ Show Spoiler +


#include <stdio.h>

int main()
{
int array[10][10];
int i;
int j;
int count;
for ( i; i<10; ++i )
{
for ( j = 0; j<10; ++j )
{
array[i][j] = count;
count++;
}
}
i = 0;
j = 0;
for ( i; i<10; ++i )
{
for ( j = 0; j<10; ++j )
{
printf( "%d", array[i][j] );
printf( " " );
}
printf("\n");

}
}



This will work in debug mode (probably), but not in release, at least not reliably. There is nothing forcing compiler to initialize i to 0 before first loop.

EDIT: Talking in VS terms In general terms, without forced variable initialization this will not work reliably.
xeliana
Profile Joined August 2010
Austria60 Posts
November 13 2013 12:08 GMT
#7832
On November 13 2013 07:39 darkness wrote:
Can anyone recommend to me UML drawing tools please? I need to draw a class diagram, use case, sequence diagram, interface design (optionally). I feel MS Word can't help me much.


if it is nothing TOO complicated i you can always try MS visio.
keep calm and play dota!
Arnstein
Profile Blog Joined May 2010
Norway3381 Posts
November 13 2013 12:14 GMT
#7833
I fucking love this community! I can't believe a simple double array thing gets this much attention, and I get all kinds of tips and ideas and help! Thanks guys, I love you <3
rsol in response to the dragoon voice being heard in SCII: dragoon ai reaches new lows: wanders into wrong game
Mr. Wiggles
Profile Blog Joined August 2010
Canada5894 Posts
November 13 2013 16:53 GMT
#7834
On November 13 2013 07:39 darkness wrote:
Can anyone recommend to me UML drawing tools please? I need to draw a class diagram, use case, sequence diagram, interface design (optionally). I feel MS Word can't help me much.

I'm taking a SE course right now, and personally I just use Dia which is available on both linux and windows for free. I've used it and it's worked pretty nicely for the class diagrams I've made for my group project of around 40ish classes. It has builtin functionality for UML too. I use it, because I work on Linux, and most of the stuff I need to do for my course is pretty simple.

Otherwise, if you're using windows and have a dreamspark account or something, I've heard Microsoft Visio is pretty decent too.

Lastly, I've heard people suggest eUML2 if you're using eclipse. What's different for this one is that after you create your UML class diagram, you can use the software to generate a bunch of code for you based on the UML, so it makes all the classes, all the method stubs, etc. in eclipse. If you're drawing this UML for something you're actually going to code, this might be something to look into. I haven't used it myself though, so I can't comment on how nice it is to use.
you gotta dance
heroyi
Profile Blog Joined March 2009
United States1064 Posts
Last Edited: 2013-11-14 16:12:50
November 14 2013 16:09 GMT
#7835
Question in C (any other related languages I suppose):

I have a question about malloc. Specifically the difference between that and just a defined array. I have read around but I am still having a hard time to wrapping my head around it. I know that malloc essentially means to allocate a specific amount of memory (whatever amount you choose so) for, usually, a pointer. Webs say that malloc is used for dynamic purposes (even faster than say vector in c++ since it doesnt have to "initialize" the memory with a bunch of zeros thus malloc being "faster"). But my problem is that too me it doesn't seem so dynamic since you have to define the malloc just like an defined array. Or does it work by adding a function/pointer or whatever solution you wish to implement to the malloc if it fails the if test (checking to see if there is enough memory, if not then add more?)

Those that can answer this can you also give a small example of malloc being dynamic.

tl;dr I don't understand how malloc is dynamic since the argument requires a define size.
wat wat in my pants
spinesheath
Profile Blog Joined June 2009
Germany8679 Posts
Last Edited: 2013-11-14 16:45:25
November 14 2013 16:42 GMT
#7836
You can give a variable length to malloc. Say you write a program that takes a number x as input from the command line and then allocates x bytes of memory. You can't do that with arrays. Arrays need their size known at compile time, malloc lets you determine the size at runtime.

Do not worry about speed unless you have run a performance analysis. Always make sure that your malloc'd memory is initialized to something reasonable. calloc is malloc + zero initialize so it might be a good idea to use that if you don't have an initialization loop right after the call to malloc.

You can also use realloc to - who would have guessed - reallocate some malloc'd memory to a new size, keeping the existing data where possible (if you reduce the size, part of the data will be cut off). Notice that realloc can return a new array pointer to a different location, copying the old data.
If you have a good reason to disagree with the above, please tell me. Thank you.
bangsholt
Profile Joined June 2011
Denmark138 Posts
November 14 2013 18:07 GMT
#7837
You're asking the difference between

a) int myArray[16]

and

b) int * myArray = (int)malloc(16 * sizeof(int));

a) is stack allocated, b) is heap allocated. This means that if, you declare a) inside a function, the array will be deleted once the function is done running - if you do b) it will stay, because it's not related to the stack.

This also means that you can leak memory when you use malloc - you have to remember to free your heap allocated variables again by calling free(myArray) - if you do this on a stack variable, welcome to segfault world :D

Then there's also what spinesheath says in addition.
zzdd
Profile Joined December 2010
United States484 Posts
November 14 2013 18:48 GMT
#7838
On November 15 2013 01:09 heroyi wrote:
+ Show Spoiler +

Question in C (any other related languages I suppose):

I have a question about malloc. Specifically the difference between that and just a defined array. I have read around but I am still having a hard time to wrapping my head around it. I know that malloc essentially means to allocate a specific amount of memory (whatever amount you choose so) for, usually, a pointer. Webs say that malloc is used for dynamic purposes (even faster than say vector in c++ since it doesnt have to "initialize" the memory with a bunch of zeros thus malloc being "faster"). But my problem is that too me it doesn't seem so dynamic since you have to define the malloc just like an defined array. Or does it work by adding a function/pointer or whatever solution you wish to implement to the malloc if it fails the if test (checking to see if there is enough memory, if not then add more?)

Those that can answer this can you also give a small example of malloc being dynamic.

tl;dr I don't understand how malloc is dynamic since the argument requires a define size.

With malloc you don't have to know the size of the array before hand and you can make it any size. Hence it is easier to use for a dynamic set of inputs. If I'm getting input from a file or whatever that can have 0-1000+ entries, I can count the entries and make a perfectly sized array with malloc. With a normal array I would just have to make a huge array and hope its big enough and if the data is smaller your wasting space. Usually when calling malloc you use sizeof() for the size of your data type and then some variable that has the count of the entries to get the size of the array. Pretty much it just gives you a block of memory for you to use however you want.

Another thing is if you get more information or less you can realloc or malloc again. Calloc btw has the initialization.
CecilSunkure
Profile Blog Joined May 2010
United States2829 Posts
November 14 2013 20:40 GMT
#7839
On November 15 2013 01:09 heroyi wrote:
Question in C (any other related languages I suppose):

I have a question about malloc. Specifically the difference between that and just a defined array. I have read around but I am still having a hard time to wrapping my head around it. I know that malloc essentially means to allocate a specific amount of memory (whatever amount you choose so) for, usually, a pointer. Webs say that malloc is used for dynamic purposes (even faster than say vector in c++ since it doesnt have to "initialize" the memory with a bunch of zeros thus malloc being "faster"). But my problem is that too me it doesn't seem so dynamic since you have to define the malloc just like an defined array. Or does it work by adding a function/pointer or whatever solution you wish to implement to the malloc if it fails the if test (checking to see if there is enough memory, if not then add more?)

Those that can answer this can you also give a small example of malloc being dynamic.

tl;dr I don't understand how malloc is dynamic since the argument requires a define size.

With an array you specify the size at compile time, hard coded in your C code. With malloc you can pass a variable containing the size of memory to retrieve. This variable can be set via user input, so the user of your program can tell the program how much memory to allocate. This is not something you can do with an array on the stack.
Ben...
Profile Joined January 2011
Canada3485 Posts
November 14 2013 22:32 GMT
#7840
On November 13 2013 07:39 darkness wrote:
Can anyone recommend to me UML drawing tools please? I need to draw a class diagram, use case, sequence diagram, interface design (optionally). I feel MS Word can't help me much.
We have to use UMLet for that type of stuff. It does the job. If you are doing Java it can read in .java files and automatically create the classes for you. It will not create relationships between classes though. It's a simple Java Archive so you can run it on anything. I just leave it in my Dropbox folder.
"Cliiiiiiiiiiiiiiiiide" -Tastosis
Prev 1 390 391 392 393 394 1032 Next
Please log in or register to reply.
Live Events Refresh
Next event in 1h 35m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
mouzHeroMarine 549
SpeCial 101
StarCraft: Brood War
Shuttle 824
White-Ra 149
Dewaltoss 132
Sexy 37
NaDa 11
Counter-Strike
fl0m1265
Super Smash Bros
Mew2King61
Other Games
Liquid`RaSZi1922
ArmadaUGS145
Pyrionflax118
RotterdaM105
C9.Mang090
Trikslyr42
JuggernautJason29
ViBE6
Organizations
Other Games
BasetradeTV42
[ Show 18 non-featured ]
StarCraft 2
• Berry_CruncH231
• musti20045 3
• intothetv
• Kozan
• Migwel
• AfreecaTV YouTube
• IndyKCrew
StarCraft: Brood War
• Michael_bg 9
• Pr0nogo 2
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• masondota2823
• WagamamaTV669
League of Legends
• TFBlade906
Other Games
• imaqtpie1321
• Scarra418
• Shiphtur233
Upcoming Events
OSC
1h 35m
IntoTheTV X SOOP
14h 5m
CranKy Ducklings
1d 13h
Big Brain Bouts
1d 13h
Garitos vs ArT
Lambo vs Percival
TriGGeR vs ByuN
Sparkling Tuna Cup
2 days
OSC
2 days
Shopify Rebellion Sundays
2 days
Mixu vs TBD
Spirit vs TBD
Clem vs TBD
Patches Events
2 days
Afreeca Starleague
3 days
Soma vs Shuttle
BeSt vs Rush
WardiTV Weekly
3 days
[ Show More ]
Afreeca Starleague
4 days
Leta vs ggaemo
Jaedong vs Queen
GSL
4 days
PiGosaur Cup
5 days
Kung Fu Cup
5 days
Replay Cast
6 days
The PondCast
6 days
Liquipedia Results

Completed

Proleague 2026-09-02
PiG Sty Festival 8.0
Light Tournament 2026

Ongoing

KCM Race Survival 2026 Season 3
K-JUNGMAN
ASL Season 22
Super Anchor Qualifying S3
CSL 2026 AUTUMN (S22)
RSL Revival: Season 6
Calamity Invitational
Big Dog Cup 2026 Div 1
BLAST Open Fall 2026
Esports World Cup 2026
Esports World Cup 2026: LCQ
BLAST Bounty Summer 2026
BLAST Bounty Summer Qual
Stake Ranked Episode 3
XSE Pro League 2026
IEM Cologne Major 2026

Upcoming

Acropolis #5
Acropolis #5 - TRS
Escore Tournament S3: King of Kings
Blizzard Classic Cup 2026
Acropolis #5 - GSA
Acropolis #5 - GSB
Acropolis #5 - GSC
HSC XXX
Stellar Fest 2: Lunar Cup
SC4ALL II: StarCraft II
Kung Fu Cup 2026 Grand Finals
RSL Offline Finals
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
FISSURE Playground #3
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.