• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 18:23
CET 00:23
KST 08:23
  • 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 (Jan 5-11): Clem wins big offline, Trigger upsets4$21,000 Rongyi Cup Season 3 announced (Jan 22-Feb 7)15Weekly Cups (Dec 29-Jan 4): Protoss rolls, 2v2 returns7[BSL21] Non-Korean Championship - Starts Jan 103SC2 All-Star Invitational: Jan 17-1828
StarCraft 2
General
Weekly Cups (Jan 5-11): Clem wins big offline, Trigger upsets SC2 All-Star Invitational: Jan 17-18 When will we find out if there are more tournament SC2 Spotted on the EWC 2026 list? Weekly Cups (Dec 29-Jan 4): Protoss rolls, 2v2 returns
Tourneys
SC2 AI Tournament 2026 Sparkling Tuna Cup - Weekly Open Tournament $21,000 Rongyi Cup Season 3 announced (Jan 22-Feb 7) $25,000 Streamerzone StarCraft Pro Series announced WardiTV Winter Cup
Strategy
Simple Questions Simple Answers
Custom Maps
Map Editor closed ?
External Content
Mutation # 508 Violent Night Mutation # 507 Well Trained Mutation # 506 Warp Zone Mutation # 505 Rise From Ashes
Brood War
General
[ASL21] Potential Map Candidates BGH Auto Balance -> http://bghmmr.eu/ BW General Discussion A cwal.gg Extension - Easily keep track of anyone Potential ASL qualifier breakthroughs?
Tourneys
[Megathread] Daily Proleagues [BSL21] Grand Finals - Sunday 21:00 CET [BSL21] Non-Korean Championship - Starts Jan 10 SLON Grand Finals – Season 2
Strategy
Game Theory for Starcraft Simple Questions, Simple Answers Current Meta [G] How to get started on ladder as a new Z player
Other Games
General Games
Beyond All Reason Nintendo Switch Thread Awesome Games Done Quick 2026! Mechabellum Stormgate/Frost Giant Megathread
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
Vanilla Mini Mafia Mafia Game Mode Feedback/Ideas
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine European Politico-economics QA Mega-thread Trading/Investing Thread
Fan Clubs
White-Ra Fan Club
Media & Entertainment
Anime Discussion Thread
Sports
2024 - 2026 Football Thread
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List
Blogs
My 2025 Magic: The Gathering…
DARKING
Physical Exercise (HIIT) Bef…
TrAiDoS
Life Update and thoughts.
FuDDx
How do archons sleep?
8882
James Bond movies ranking - pa…
Topin
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1591 users

The Big Programming Thread - Page 865

Forum Index > General Forum
Post a Reply
Prev 1 863 864 865 866 867 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.
Biolunar
Profile Joined February 2012
Germany224 Posts
March 29 2017 14:46 GMT
#17281
On March 29 2017 23:02 TMG26 wrote:
Show nested quote +
On March 29 2017 07:28 Blitzkrieg0 wrote:
On March 29 2017 06:58 Biolunar wrote:
Casting the return value of malloc is a bad idea. void* will be implicitly converted into any other pointer type, so don’t cast it.


I've always seen casting malloc as part of the coding standard. While pointless it is a sanity check.


It's also recommended in older C standards.

Definitely not. In old C you can use functions without providing a declaration, in which case a return value of ‘int’ is assumed. So let’s say you forget to include stdlib.h but you use malloc. The complier then assumes there is a function ‘int malloc()’ somewhere. Now you cast that returned int into your pointer type and use it. Great. You just traded a compile time error in for a runtime error. Don’t cast malloc.
What is best? To crush the Zerg, see them driven before you, and hear the lamentations of the Protoss.
BluzMan
Profile Blog Joined April 2006
Russian Federation4235 Posts
March 29 2017 19:53 GMT
#17282
On March 29 2017 23:46 Biolunar wrote:
Show nested quote +
On March 29 2017 23:02 TMG26 wrote:
On March 29 2017 07:28 Blitzkrieg0 wrote:
On March 29 2017 06:58 Biolunar wrote:
Casting the return value of malloc is a bad idea. void* will be implicitly converted into any other pointer type, so don’t cast it.


I've always seen casting malloc as part of the coding standard. While pointless it is a sanity check.


It's also recommended in older C standards.

Definitely not. In old C you can use functions without providing a declaration, in which case a return value of ‘int’ is assumed. So let’s say you forget to include stdlib.h but you use malloc. The complier then assumes there is a function ‘int malloc()’ somewhere. Now you cast that returned int into your pointer type and use it. Great. You just traded a compile time error in for a runtime error. Don’t cast malloc.


Ermm, I'm curious how the linker would assemble the executable without a function definition?
You want 20 good men, but you need a bad pussy.
Cyx.
Profile Joined November 2010
Canada806 Posts
Last Edited: 2017-03-29 20:35:24
March 29 2017 20:34 GMT
#17283
On March 30 2017 04:53 BluzMan wrote:
Show nested quote +
On March 29 2017 23:46 Biolunar wrote:
On March 29 2017 23:02 TMG26 wrote:
On March 29 2017 07:28 Blitzkrieg0 wrote:
On March 29 2017 06:58 Biolunar wrote:
Casting the return value of malloc is a bad idea. void* will be implicitly converted into any other pointer type, so don’t cast it.


I've always seen casting malloc as part of the coding standard. While pointless it is a sanity check.


It's also recommended in older C standards.

Definitely not. In old C you can use functions without providing a declaration, in which case a return value of ‘int’ is assumed. So let’s say you forget to include stdlib.h but you use malloc. The complier then assumes there is a function ‘int malloc()’ somewhere. Now you cast that returned int into your pointer type and use it. Great. You just traded a compile time error in for a runtime error. Don’t cast malloc.


Ermm, I'm curious how the linker would assemble the executable without a function definition?

It has a definition for standard library functions since it links against the stdlib by default. Most compilers will complain about 'implicit declaration of malloc' or something like that but they'll generally allow it unless you have warnings way up. Although I don't see how this trades a compile-time error for a runtime error... either way, you just get a warning about the implicit definition, with maybe an additional warning about casting a pointer to integer.

e: also, I've never seen casting malloc recommended in C, it's required if you use malloc in C++ (which you just shouldn't do) but every C programmer I know says 'don't cast malloc'.
TMG26
Profile Joined July 2012
Portugal2017 Posts
March 30 2017 08:30 GMT
#17284
On March 30 2017 05:34 Cyx. wrote:
Show nested quote +
On March 30 2017 04:53 BluzMan wrote:
On March 29 2017 23:46 Biolunar wrote:
On March 29 2017 23:02 TMG26 wrote:
On March 29 2017 07:28 Blitzkrieg0 wrote:
On March 29 2017 06:58 Biolunar wrote:
Casting the return value of malloc is a bad idea. void* will be implicitly converted into any other pointer type, so don’t cast it.


I've always seen casting malloc as part of the coding standard. While pointless it is a sanity check.


It's also recommended in older C standards.

Definitely not. In old C you can use functions without providing a declaration, in which case a return value of ‘int’ is assumed. So let’s say you forget to include stdlib.h but you use malloc. The complier then assumes there is a function ‘int malloc()’ somewhere. Now you cast that returned int into your pointer type and use it. Great. You just traded a compile time error in for a runtime error. Don’t cast malloc.


Ermm, I'm curious how the linker would assemble the executable without a function definition?

It has a definition for standard library functions since it links against the stdlib by default. Most compilers will complain about 'implicit declaration of malloc' or something like that but they'll generally allow it unless you have warnings way up. Although I don't see how this trades a compile-time error for a runtime error... either way, you just get a warning about the implicit definition, with maybe an additional warning about casting a pointer to integer.

e: also, I've never seen casting malloc recommended in C, it's required if you use malloc in C++ (which you just shouldn't do) but every C programmer I know says 'don't cast malloc'.


I assumed it was recommend in old standards, because in the book "The C Programming Language" (K&R) they cast malloc.
Supporter of the situational Blink Dagger on Storm.
Manit0u
Profile Blog Joined August 2004
Poland17601 Posts
Last Edited: 2017-03-30 10:35:14
March 30 2017 10:34 GMT
#17285
PGSQL question:

Tables


A >---< B >---< C

>---< is a many-to-many relation with an intermediate table containing
ids of referenced table rows


What I need


SELECT DISTINCT C.id WHERE [A_B.b_ids] @> [C_B.b_ids]


Basically, I need all C's whose related Bs are all in A's related Bs (for a single A) - a matching subset if you will. Any idea how I should construct this query?
Time is precious. Waste it wisely.
berated-
Profile Blog Joined February 2007
United States1134 Posts
Last Edited: 2017-03-30 10:56:43
March 30 2017 10:54 GMT
#17286
On March 30 2017 19:34 Manit0u wrote:
PGSQL question:

Tables


A >---< B >---< C

>---< is a many-to-many relation with an intermediate table containing
ids of referenced table rows


What I need


SELECT DISTINCT C.id WHERE [A_B.b_ids] @> [C_B.b_ids]


Basically, I need all C's whose related Bs are all in A's related Bs (for a single A) - a matching subset if you will. Any idea how I should construct this query?


Is that not just distinct c from c join b join a, through the intermediate tables? Is it too large to do so?

Or just intermediate join intermediate join c.
Manit0u
Profile Blog Joined August 2004
Poland17601 Posts
Last Edited: 2017-03-30 12:05:19
March 30 2017 11:46 GMT
#17287
On March 30 2017 19:54 berated- wrote:
Show nested quote +
On March 30 2017 19:34 Manit0u wrote:
PGSQL question:

Tables


A >---< B >---< C

>---< is a many-to-many relation with an intermediate table containing
ids of referenced table rows


What I need


SELECT DISTINCT C.id WHERE [A_B.b_ids] @> [C_B.b_ids]


Basically, I need all C's whose related Bs are all in A's related Bs (for a single A) - a matching subset if you will. Any idea how I should construct this query?


Is that not just distinct c from c join b join a, through the intermediate tables? Is it too large to do so?

Or just intermediate join intermediate join c.


I think I've found a solution:


SELECT res.id
FROM (
SELECT C.id, array_agg(C_B.b_id) AS b_ids
FROM "C" INNER JOIN "C_B" ON "C_B"."c_id" = "C"."id"
GROUP BY "C"."id"
) AS res
WHERE (res.b_ids <@ ARRAY[21,84,57,58,8,40,34,59,38,71,35,67,48,62,92,99,12,14,88,5,53])


Edit:

On another note...
[image loading]
Time is precious. Waste it wisely.
Nesserev
Profile Blog Joined January 2011
Belgium2760 Posts
Last Edited: 2017-03-30 12:44:29
March 30 2017 12:34 GMT
#17288
--- Nuked ---
Acrofales
Profile Joined August 2010
Spain18182 Posts
March 30 2017 13:01 GMT
#17289
Lol. Googling that leads to reddit, which is just proof that programmers love programming... as long as it's not actual work.

https://www.reddit.com/r/programming/comments/buosj/add_a_number_to_another_number_in_javascript_img/
meadbert
Profile Blog Joined October 2010
United States681 Posts
March 30 2017 14:31 GMT
#17290
On March 30 2017 05:34 Cyx. wrote:

It has a definition for standard library functions since it links against the stdlib by default. Most compilers will complain about 'implicit declaration of malloc' or something like that but they'll generally allow it unless you have warnings way up. Although I don't see how this trades a compile-time error for a runtime error... either way, you just get a warning about the implicit definition, with maybe an additional warning about casting a pointer to integer.

e: also, I've never seen casting malloc recommended in C, it's required if you use malloc in C++ (which you just shouldn't do) but every C programmer I know says 'don't cast malloc'.


Regarding casting malloc. I use gcc 5.3.1 with -Wall and it will still warn if I do not cast malloc.

Also I have seen this bug a few times that can arise from not casting malloc.

int *heap = malloc(size*sizeof(int)) - 1; // <--- THIS IS A BUG!!!

You might wonder why on earth someone has added -1 and it has to do with a performance optimization when using binary heaps.
If you use a 0 based array then your children are 2*i + 1 and 2*i + 2 while your parent is (i - 1)/2;
Note that for speed you would want to be doing left shifts and right shifts rather than multiplying and dividing. I believe in some architectures shifting is free although I do not have the details. It may be those are not used anymore, but in any case I have seen this particular bug show up more than once and it is very common to see this trick used in general.

The problem is that finding a child or parent will always require and addition or subtraction operation which is NOT free.

If you use 1 based arrays then your children are 2*i and 2*i+1 and your parent is i/2.
If you use shifts again then only a third of the time do you need the addition so your heap is sped up by a lot on certain architectures.

For this reason I see people move their heap pointer one "spot" to the left and then index by 1 so heap[1] is the root of the heap.

The problem is in the buggy line above they meant to shift left by 1 integer, but instead they shifted left by 1 byte because malloc returned a void* so instead they will end up reading and writing past the end of the array when reading and writing the last integer in the array.

Correct ways to use that hack would be:
int *heap = malloc(size*sizeof(int));
heap--;

or

int *heap = ((int*)malloc(size*sizeof(int))) - 1;

or

int *heap = malloc(size*sizeof(int)) - sizeof(int);

The point is you need to be careful when adding or subtracting from a value returned by malloc.
Hanh
Profile Joined June 2016
146 Posts
March 30 2017 15:06 GMT
#17291
gcc displays a warning in that case.
Biolunar
Profile Joined February 2012
Germany224 Posts
March 30 2017 17:15 GMT
#17292
On March 30 2017 23:31 meadbert wrote:
Regarding casting malloc. I use gcc 5.3.1 with -Wall and it will still warn if I do not cast malloc.

This is hard for me to believe. void* -> T* is an implicit lossless conversion that should compile without warning. Did you compile it as C++? Does not warn me with -Wall -Wextra on gcc nor on clang with -Weverything.

On March 30 2017 23:31 meadbert wrote:
int *heap = malloc(size*sizeof(int)) - 1; // <--- THIS IS A BUG!!!

Yes that’s a bug, because it does not compile malloc returns void* and void* does not allow pointer arithmetic. As you stated correctly you need to cast it before you can do some arithmetic on the return value (or save the value and decrement it afterwards).
What is best? To crush the Zerg, see them driven before you, and hear the lamentations of the Protoss.
Acrofales
Profile Joined August 2010
Spain18182 Posts
March 30 2017 17:48 GMT
#17293
Just want to vent a little bit.

I have what I know to be quite a shitty dataset, but it's the only dataset I can find that might remotely be used to model what I want to model.

So I program my feature extraction part (took about a week to extract the features that my hypothesis says should explain what I want), and release some out-of-the-box regression models on a simple train/test split. Utterly terrible results. I have now spent the last 2 days trying to tune the algorithms, and anything I do, it would literally be better to just always expect the mean value (of my data set) rather than use the regression models that I have fit. So now I have a bit of an awkward situation: I know the data set is garbage, but I thought it would allow me to at least do enough of a validation to send my idea to a small conference. How the hell do I write a paper with no results... basically saying "I have this idea, but the only available data set is garbage. I need to collect more data to be able to test the hypothesis. Please accept this paper anyway, because the idea is good. Right?"

Rant out. If anybody has good ideas, I'm not an ML expert. I'm a proficient data miner. I tried ridge regression, lasso (or rather, I just used elastic net and varied the coefficient) and random forest regression. My data set is tiny (95 samples) and very noisy.
spinesheath
Profile Blog Joined June 2009
Germany8679 Posts
March 30 2017 18:25 GMT
#17294
So you can't really provide evidence for or against your hypothesis? First impressions still count way too much whenever humans are involved. It might not be a good thing for you or your hypothesis to publish it in such a poor condition.
If you have a good reason to disagree with the above, please tell me. Thank you.
meadbert
Profile Blog Joined October 2010
United States681 Posts
Last Edited: 2017-03-30 19:20:11
March 30 2017 19:10 GMT
#17295
On March 31 2017 02:15 Biolunar wrote:
Show nested quote +
On March 30 2017 23:31 meadbert wrote:
Regarding casting malloc. I use gcc 5.3.1 with -Wall and it will still warn if I do not cast malloc.

This is hard for me to believe. void* -> T* is an implicit lossless conversion that should compile without warning. Did you compile it as C++? Does not warn me with -Wall -Wextra on gcc nor on clang with -Weverything.

Show nested quote +
On March 30 2017 23:31 meadbert wrote:
int *heap = malloc(size*sizeof(int)) - 1; // <--- THIS IS A BUG!!!

Yes that’s a bug, because it does not compile malloc returns void* and void* does not allow pointer arithmetic. As you stated correctly you need to cast it before you can do some arithmetic on the return value (or save the value and decrement it afterwards).

I just tested this code:

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
void *c = malloc(10*sizeof(*c)) - 1;
int *i = c + 3;
int *j = c;
int d = i - j;

printf("%d\n", d);
return 0;
}

I used -Wall and -Wextra.

I got no warnings. So it appears that the cast from void* to int* does not generate a warning, but also the pointer subtraction on void* worked just fine with not so much as a warning.

EDIT: I just did some research and pointer addition on void* is illegal in both C and C++, but GCC apparently allows for it which explains the results that I saw.
Biolunar
Profile Joined February 2012
Germany224 Posts
March 30 2017 19:33 GMT
#17296
On March 31 2017 04:10 meadbert wrote:
I just tested this code:

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
void *c = malloc(10*sizeof(*c)) - 1;
int *i = c + 3;
int *j = c;
int d = i - j;

printf("%d\n", d);
return 0;
}

I used -Wall and -Wextra.

I got no warnings. So it appears that the cast from void* to int* does not generate a warning, but also the pointer subtraction on void* worked just fine with not so much as a warning.

EDIT: I just did some research and pointer addition on void* is illegal in both C and C++, but GCC apparently allows for it which explains the results that I saw.

Hmm you are right, gcc does not complain by default.
man gcc
-Wpointer-arith
Warn about anything that depends on the "size of" a function type or of "void". GNU C assigns these types a size
of 1, for convenience in calculations with "void *" pointers and pointers to functions. In C++, warn also when an
arithmetic operation involves "NULL". This warning is also enabled by -Wpedantic.

I always use -pedantic-errors, so that all warnings demanded by strict ISO C are issued as an error.
What is best? To crush the Zerg, see them driven before you, and hear the lamentations of the Protoss.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
March 30 2017 20:15 GMT
#17297
I have a feeling this thread is gonna really blow up over the summer when I take Intro to Algorithms.
Manit0u
Profile Blog Joined August 2004
Poland17601 Posts
March 30 2017 21:05 GMT
#17298
On March 31 2017 05:15 travis wrote:
I have a feeling this thread is gonna really blow up over the summer when I take Intro to Algorithms.


Summer is not for school...
Time is precious. Waste it wisely.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
March 30 2017 21:18 GMT
#17299
Well it is unless you want to hire me for an internship.
WarSame
Profile Blog Joined February 2010
Canada1950 Posts
March 30 2017 22:44 GMT
#17300
Could someone help me parse
int (*getFunc())(int, int) { … }
, which is a function that returns a pointer to a function?

I understand that it returns an int, but I don't get what is happening in the first set of brackets.
Can it be I stayed away too long? Did you miss these rhymes while I was gone?
Prev 1 863 864 865 866 867 1032 Next
Please log in or register to reply.
Live Events Refresh
Next event in 10h 37m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
UpATreeSC 146
Nathanias 140
CosmosSc2 38
StarCraft: Brood War
Shuttle 581
Artosis 348
910 39
NaDa 16
Dota 2
syndereN399
capcasts77
canceldota14
LuMiX1
Counter-Strike
Foxcn392
Super Smash Bros
Mew2King108
AZ_Axe15
Other Games
tarik_tv5264
Liquid`RaSZi2388
Grubby2149
FrodaN1374
shahzam540
Liquid`Hasu223
C9.Mang0215
B2W.Neo171
Maynarde75
Fnx 65
ZombieGrub44
KnowMe30
Organizations
Other Games
gamesdonequick2198
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 19 non-featured ]
StarCraft 2
• Hupsaiya 67
• davetesta38
• HeavenSC 18
• sooper7s
• Migwel
• AfreecaTV YouTube
• LaughNgamezSOOP
• intothetv
• IndyKCrew
• Kozan
StarCraft: Brood War
• Azhi_Dahaki36
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• masondota21243
League of Legends
• Doublelift4530
• Scarra1059
Other Games
• imaqtpie2581
• Shiphtur327
Upcoming Events
The PondCast
10h 37m
OSC
12h 37m
Jumy vs sebesdes
Nicoract vs GgMaChine
ReBellioN vs MaNa
Lemon vs TriGGeR
Gerald vs Cure
Creator vs SHIN
OSC
1d 12h
All Star Teams
2 days
INnoVation vs soO
Serral vs herO
Cure vs Solar
sOs vs Scarlett
Classic vs Clem
Reynor vs Maru
uThermal 2v2 Circuit
2 days
All Star Teams
3 days
MMA vs DongRaeGu
Rogue vs Oliveira
Sparkling Tuna Cup
3 days
OSC
3 days
Replay Cast
4 days
Wardi Open
4 days
[ Show More ]
Monday Night Weeklies
4 days
The PondCast
6 days
Liquipedia Results

Completed

Proleague 2026-01-13
Big Gabe Cup #3
NA Kuram Kup

Ongoing

C-Race Season 1
IPSL Winter 2025-26
BSL 21 Non-Korean Championship
CSL 2025 WINTER (S19)
OSC Championship Season 13
Underdog Cup #3
BLAST Bounty Winter Qual
eXTREMESLAND 2025
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025

Upcoming

Escore Tournament S1: W4
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
Rongyi Cup S3
SC2 All-Star Inv. 2025
Nations Cup 2026
BLAST Open Spring 2026
ESL Pro League Season 23
ESL Pro League Season 23
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
TLPD

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

Advertising | Privacy Policy | Terms Of Use | Contact Us

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