• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 08:07
CEST 14:07
KST 21:07
  • 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
[ASL21] Finals Preview: Two Legacies18Code S Season 2 (2026) - RO12 Preview2herO wins GSL Code S Season 1 (2026)5Code S Season 1 (2026) - RO4 & Finals Preview5[ASL21] Ro4 Preview: On Course12
Community News
Crank Gathers Season 4: BW vs SC2 Team League0Weekly Cups (May 11-17): Classic wins double0Code S Season 1 (2026) - RO8 Results2Weekly Cups (May 4-10): Clem, MaxPax, herO win1Maestros of The Game 2 announcement and schedule !18
StarCraft 2
General
herO wins GSL Code S Season 1 (2026) Code S Season 2 (2026) - RO12 Preview Weekly Cups (May 11-17): Classic wins double Code S Season 1 (2026) - RO4 & Finals Preview Team Liquid Map Contest #22 - The Finalists
Tourneys
GSL Code S Season 2 (2026) Crank Gathers Season 4: BW vs SC2 Team League GSL Code S Season 1 (2026) Sparkling Tuna Cup - Weekly Open Tournament Maestros of The Game 2 announcement and schedule !
Strategy
Custom Maps
[D]RTS in all its shapes and glory <3 [A] Nemrods 1/4 players
External Content
Mutation # 527 Hell Train The PondCast: SC2 News & Results Mutation # 526 Rubber and Glue Mutation # 525 Wheel of Misfortune
Brood War
General
25 Years Since Brood War Patch 1.08 VPN experiences vespene.gg — BW replays in browser (Spoiler) ASL21 Winner's Interview [ASL21] Finals Preview: Two Legacies
Tourneys
[ASL21] Grand Finals Escore Tournament StarCraft Season 2 [Megathread] Daily Proleagues Small VOD Thread 2.0
Strategy
Any training maps people recommend? Muta micro map competition [G] Hydra ZvZ: An Introduction Fighting Spirit mining rates
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread Dawn of War IV ZeroSpace Megathread Warcraft III: The Frozen Throne
Dota 2
The Story of Wings Gaming
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 TL Mafia Community Thread Five o'clock TL Mafia
Community
General
Russo-Ukrainian War Thread US Politics Mega-thread Trading/Investing Thread European Politico-economics QA Mega-thread YouTube Thread
Fan Clubs
The herO Fan Club!
Media & Entertainment
[Manga] One Piece Anime Discussion Thread [Req][Books] Good Fantasy/SciFi books
Sports
2024 - 2026 Football Thread McBoner: A hockey love story TeamLiquid Health and Fitness Initiative For 2023 Formula 1 Discussion
World Cup 2022
Tech Support
streaming software Strange computer issues (software)
TL Community
The Automated Ban List
Blogs
Esports Organizations: Raisi…
TrAiDoS
Why RTS gamers make better f…
gosubay
ramps on octagon
StaticNine
Funny Nicknames
LUCKY_NOOB
ASL S21 English Commentary…
namkraft
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1279 users

The Big Programming Thread - Page 875

Forum Index > General Forum
Post a Reply
Prev 1 873 874 875 876 877 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.
BluzMan
Profile Blog Joined April 2006
Russian Federation4235 Posts
Last Edited: 2017-04-20 00:12:40
April 20 2017 00:04 GMT
#17481
On April 20 2017 08:30 travis wrote:
**p;
*k;

statement 1: p = &k;
statement 2: *p = k;

both of these statements are the same?


lets assume there's no aliasing here, say:

T* k = 0x01, &k == 0x10 (k as a variable holds the value of 0x01 which is an address of some object of type T, it's own address is 0x10)
T* m = 0x02, &m == 0x20 (same for m)
T** p = &m;

then, initially:
p == 0x20

1) p = &k which translates to p = 0x10 which leads to: p == 0x10, m == 0x02
2) *p = k which translates to m = 0x01 which leads to: p == 0x20, m == 0x01

EDIT: made a little mistake on the first try but basically those are quite different
You want 20 good men, but you need a bad pussy.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
April 20 2017 00:07 GMT
#17482
ah ok yeah i get it, thanks
pointers are *still* rough haha
Manit0u
Profile Blog Joined August 2004
Poland17750 Posts
April 20 2017 01:15 GMT
#17483
One of the reasons why I work with highly abstract languages in the first place (Ruby). I just write a bunch of pseudo code and it "just works". I don't have to worry about so much bullshit which leaves me plenty of time to figure out what and how my code should do things instead of fighting a constant battle just to make it work.

I don't know. I enjoy me some C now and then, but working with it full-time would be too nerve-wracking for me.
Time is precious. Waste it wisely.
Hanh
Profile Joined June 2016
146 Posts
April 20 2017 01:43 GMT
#17484
On April 20 2017 06:55 BluzMan wrote:
Show nested quote +
On April 20 2017 06:47 travis wrote:
but j is not null at that point, j is still a pointer to the section of memory that was freed.

p points to null, j contains the address it was assigned when we did j = p

also the above example is bad too. you don't want to free a bunch of memory that was never allocated you'll get seg faults or worse

maybe you guys are referencing behavior of free in c++ ?


The first thing that comes to mind without allocating memory would be casting the input to an array of ints, sorting it and just using continue if a[i] == a[i - 1].

Double free is a dangerous error and should be avoided at all costs. Furthermore, you can't tell if a pointer has been freed just by looking at it, otherwise, C programming would have been really easy. Freeing a nullptr is generally safe, but you have to set it to nullptr first.


Don't cast a pointer to int, it is not portable and unsafe. Just leave it as a pointer.
Hanh
Profile Joined June 2016
146 Posts
April 20 2017 01:45 GMT
#17485
On April 20 2017 08:03 Manit0u wrote:
Show nested quote +
On April 20 2017 07:25 travis wrote:
On April 20 2017 06:55 BluzMan wrote:
On April 20 2017 06:47 travis wrote:
but j is not null at that point, j is still a pointer to the section of memory that was freed.

p points to null, j contains the address it was assigned when we did j = p

also the above example is bad too. you don't want to free a bunch of memory that was never allocated you'll get seg faults or worse

maybe you guys are referencing behavior of free in c++ ?


The first thing that comes to mind without allocating memory would be casting the input to an array of ints, sorting it and just using continue if a[i] == a[i - 1].

Double free is a dangerous error and should be avoided at all costs. Furthermore, you can't tell if a pointer has been freed just by looking at it, otherwise, C programming would have been really easy. Freeing a nullptr is generally safe, but you have to set it to nullptr first.


well that's a way cooler solution than the given solution, I guess technically yours is faster than the given solution too


This is indeed nice...


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

int compare(const void *a, const void *b);

int main(void) {
int i = 0;
int len = 2;
int **ary = malloc(sizeof(int) * len);
int *p = malloc(sizeof(int));;
int *j = p;

ary[0] = p;
ary[1] = j;

qsort(ary, len, sizeof(int), compare);

for (i; i < len; i++) {
if (i > 0 && ary[i] != ary[i - 1]) {
free(ary[i]);
}
}

free(ary);

printf("We are free!\n");

return 0;
}

int compare(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}


This doesn't free ary[0]
Manit0u
Profile Blog Joined August 2004
Poland17750 Posts
April 20 2017 08:19 GMT
#17486
On April 20 2017 10:45 Hanh wrote:
Show nested quote +
On April 20 2017 08:03 Manit0u wrote:
On April 20 2017 07:25 travis wrote:
On April 20 2017 06:55 BluzMan wrote:
On April 20 2017 06:47 travis wrote:
but j is not null at that point, j is still a pointer to the section of memory that was freed.

p points to null, j contains the address it was assigned when we did j = p

also the above example is bad too. you don't want to free a bunch of memory that was never allocated you'll get seg faults or worse

maybe you guys are referencing behavior of free in c++ ?


The first thing that comes to mind without allocating memory would be casting the input to an array of ints, sorting it and just using continue if a[i] == a[i - 1].

Double free is a dangerous error and should be avoided at all costs. Furthermore, you can't tell if a pointer has been freed just by looking at it, otherwise, C programming would have been really easy. Freeing a nullptr is generally safe, but you have to set it to nullptr first.


well that's a way cooler solution than the given solution, I guess technically yours is faster than the given solution too


This is indeed nice...


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

int compare(const void *a, const void *b);

int main(void) {
int i = 0;
int len = 2;
int **ary = malloc(sizeof(int) * len);
int *p = malloc(sizeof(int));;
int *j = p;

ary[0] = p;
ary[1] = j;

qsort(ary, len, sizeof(int), compare);

for (i; i < len; i++) {
if (i > 0 && ary[i] != ary[i - 1]) {
free(ary[i]);
}
}

free(ary);

printf("We are free!\n");

return 0;
}

int compare(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}


This doesn't free ary[0]


You're right. I'm dumb
Time is precious. Waste it wisely.
Acrofales
Profile Joined August 2010
Spain18295 Posts
Last Edited: 2017-04-20 12:39:02
April 20 2017 12:12 GMT
#17487
On April 20 2017 09:03 Nesserev wrote:
(Also, both are valid statements.)


// original setup
p -> a -> d
k -> e

// statement 1
a -> d
p -> k -> e

// statement 2
p -> a -> e
k -> e


Interesting. You're allowed to put operators on the LHS of assignments? Any operator (that makes sense)? Or just pointer operators?

In other words, can you do:


int i, j, k;
i + 1 = 8; // i is 7

(i == 7? j : k) = 101;


I sure hope not, but just wondering. Would be funny to write almost prolog-esque code in C. Just to fuck with whoever has to debug.

E: thinking about it a bit more, dereferencing in the LHS of the statement makes perfect sense, it's just syntax I am not used to seeing. So nvm about my questions about operators. I'm sure they're not allowed. No sane imperative language should allow that kind of stuff
Hanh
Profile Joined June 2016
146 Posts
April 20 2017 13:28 GMT
#17488
It's quite common in C because that's how you would write to a pointer location. You can assign to a lvalue which is an expression that has a location in memory. So things like,


*(p+1)
(*p).a
*(2+p)


are ok. They could also be written as

p[1]
p->a
p[2]


BluzMan
Profile Blog Joined April 2006
Russian Federation4235 Posts
April 20 2017 15:46 GMT
#17489
On April 20 2017 21:12 Acrofales wrote:
Show nested quote +
On April 20 2017 09:03 Nesserev wrote:
(Also, both are valid statements.)


// original setup
p -> a -> d
k -> e

// statement 1
a -> d
p -> k -> e

// statement 2
p -> a -> e
k -> e


Interesting. You're allowed to put operators on the LHS of assignments? Any operator (that makes sense)? Or just pointer operators?

In other words, can you do:


int i, j, k;
i + 1 = 8; // i is 7

(i == 7? j : k) = 101;


I sure hope not, but just wondering. Would be funny to write almost prolog-esque code in C. Just to fuck with whoever has to debug.

E: thinking about it a bit more, dereferencing in the LHS of the statement makes perfect sense, it's just syntax I am not used to seeing. So nvm about my questions about operators. I'm sure they're not allowed. No sane imperative language should allow that kind of stuff


It takes a while to get your head around, but in C++ even this is possible:
 func() = value; 
You want 20 good men, but you need a bad pussy.
BluzMan
Profile Blog Joined April 2006
Russian Federation4235 Posts
April 20 2017 15:53 GMT
#17490
On April 20 2017 10:43 Hanh wrote:
Show nested quote +
On April 20 2017 06:55 BluzMan wrote:
On April 20 2017 06:47 travis wrote:
but j is not null at that point, j is still a pointer to the section of memory that was freed.

p points to null, j contains the address it was assigned when we did j = p

also the above example is bad too. you don't want to free a bunch of memory that was never allocated you'll get seg faults or worse

maybe you guys are referencing behavior of free in c++ ?


The first thing that comes to mind without allocating memory would be casting the input to an array of ints, sorting it and just using continue if a[i] == a[i - 1].

Double free is a dangerous error and should be avoided at all costs. Furthermore, you can't tell if a pointer has been freed just by looking at it, otherwise, C programming would have been really easy. Freeing a nullptr is generally safe, but you have to set it to nullptr first.


Don't cast a pointer to int, it is not portable and unsafe. Just leave it as a pointer.


I don't exactly remember what the standard says about comparing pointers that don't belong the the same range, but I'm pretty sure it's a step towards undefined behaviour. I'm also pretty sure that it defines an uintptr_t type that you can safely cast any pointer to. I should have made myself clear on that one, yeah.
You want 20 good men, but you need a bad pussy.
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2017-04-20 22:19:36
April 20 2017 22:07 GMT
#17491
On April 21 2017 00:46 BluzMan wrote:
Show nested quote +
On April 20 2017 21:12 Acrofales wrote:
On April 20 2017 09:03 Nesserev wrote:
(Also, both are valid statements.)


// original setup
p -> a -> d
k -> e

// statement 1
a -> d
p -> k -> e

// statement 2
p -> a -> e
k -> e


Interesting. You're allowed to put operators on the LHS of assignments? Any operator (that makes sense)? Or just pointer operators?

In other words, can you do:


int i, j, k;
i + 1 = 8; // i is 7

(i == 7? j : k) = 101;


I sure hope not, but just wondering. Would be funny to write almost prolog-esque code in C. Just to fuck with whoever has to debug.

E: thinking about it a bit more, dereferencing in the LHS of the statement makes perfect sense, it's just syntax I am not used to seeing. So nvm about my questions about operators. I'm sure they're not allowed. No sane imperative language should allow that kind of stuff


It takes a while to get your head around, but in C++ even this is possible:
 func() = value; 


Only if func() returns an lvalue-reference though? So, it's consistent with lvalue's idea.

Edit: As far as I remember, lvalue can only be on the left side. It's a thing that exists after the line is executed (unlike rvalue). So, if a function returns a reference (Object&), then it's an object that persists after function's call, hence it's considered to be lvalue so it's OK to have:

 func() = value; 


Essentially, you're saying

 variable = value; 
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2017-04-20 22:30:21
April 20 2017 22:28 GMT
#17492
edit: ah no, explanation is way diff from C
Hanh
Profile Joined June 2016
146 Posts
April 20 2017 23:16 GMT
#17493
On April 21 2017 00:53 BluzMan wrote:
Show nested quote +
On April 20 2017 10:43 Hanh wrote:
On April 20 2017 06:55 BluzMan wrote:
On April 20 2017 06:47 travis wrote:
but j is not null at that point, j is still a pointer to the section of memory that was freed.

p points to null, j contains the address it was assigned when we did j = p

also the above example is bad too. you don't want to free a bunch of memory that was never allocated you'll get seg faults or worse

maybe you guys are referencing behavior of free in c++ ?


The first thing that comes to mind without allocating memory would be casting the input to an array of ints, sorting it and just using continue if a[i] == a[i - 1].

Double free is a dangerous error and should be avoided at all costs. Furthermore, you can't tell if a pointer has been freed just by looking at it, otherwise, C programming would have been really easy. Freeing a nullptr is generally safe, but you have to set it to nullptr first.


Don't cast a pointer to int, it is not portable and unsafe. Just leave it as a pointer.


I don't exactly remember what the standard says about comparing pointers that don't belong the the same range, but I'm pretty sure it's a step towards undefined behaviour. I'm also pretty sure that it defines an uintptr_t type that you can safely cast any pointer to. I should have made myself clear on that one, yeah.


Well, if you can't compare pointers on your architecture you can't compare pointers casted to integers either.
On segmented memory architecture, two pointers can have different values and yet refer to the same physical location. Even in linear models, it could happen if for example these pointers are obtained from mmap. But otherwise, pointers to the same type have total ordering.

BTW uintptr_t is an optional type.
Manit0u
Profile Blog Joined August 2004
Poland17750 Posts
April 21 2017 12:12 GMT
#17494
Time is precious. Waste it wisely.
Acrofales
Profile Joined August 2010
Spain18295 Posts
April 21 2017 12:26 GMT
#17495
Lol. That is one dated song! :D
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2017-04-21 17:44:31
April 21 2017 17:42 GMT
#17496
we've been assigned a bit of a harder project than anything we've had before
anyways i need to check up on my understanding of trees/recursion

Let's say I am given a tree of "commands", and the commands come in 2 types: instructions and conjunctions. Each node on the tree is an instruction, or a conjunction (such as &&, or piping).

commands have been entered into the tree such that the first most instruction you execute will be the furthest left leaf.

The tree is a binary tree, where instructions are always leafs and conjunctions always have a left and a right node.

Would I be correct in that the way I should traverse this tree, to execute commands in order, should be something like this:


recursive_traversal(node) {
if node is a tree {
recursive_traversal(left)
visit current node
recursive_traversal(right)
}
if node is a leaf {
visit current node
}
}



edit: wait.. why am I doing this. In this case I could just do an in order traversal of nodes couldn't I? hmmm now I am worried there was some reason why I wanted to do this in this way
Acrofales
Profile Joined August 2010
Spain18295 Posts
Last Edited: 2017-04-21 17:57:07
April 21 2017 17:56 GMT
#17497
The pseudocode you posted IS an in-order traversal of nodes. And seems correct to me...
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2017-04-21 18:07:49
April 21 2017 18:07 GMT
#17498
On April 22 2017 02:56 Acrofales wrote:
The pseudocode you posted IS an in-order traversal of nodes. And seems correct to me...


well for in order traversal can't I get rid of the the first if statement and remove the 2nd if statement/block entirely ?

oh wait, no I can't huh. well I am a bit silly aren't I
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2017-04-22 14:23:17
April 22 2017 14:00 GMT
#17499
edit: nevermind. rubber ducky debugging. sorry for spam
tofucake
Profile Blog Joined October 2009
Hyrule19215 Posts
Last Edited: 2017-04-22 14:30:34
April 22 2017 14:30 GMT
#17500
don't apologize to my duck

[image loading]
Liquipediaasante sana squash banana
Prev 1 873 874 875 876 877 1032 Next
Please log in or register to reply.
Live Events Refresh
Wardi Open
11:00
#88
WardiTV694
IntoTheiNu 609
TKL 309
Ryung 87
Rex70
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
TKL 309
Ryung 87
Rex 70
ProTech68
Railgan 46
StarCraft: Brood War
Britney 51074
Calm 7486
Jaedong 713
Hyuk 402
Mini 346
EffOrt 338
Larva 275
BeSt 232
actioN 227
firebathero 218
[ Show more ]
Light 217
Mind 199
Soulkey 185
Pusan 130
ggaemo 124
Rush 115
Killer 108
Snow 77
Sharp 68
Liquid`Ret 64
scan(afreeca) 52
[sc1f]eonzerg 52
Sea.KH 46
Aegong 41
Mong 35
ToSsGirL 34
Barracks 26
Shinee 26
JulyZerg 25
sorry 24
Movie 16
soO 15
IntoTheRainbow 15
ajuk12(nOOB) 13
Noble 8
Sexy 5
Icarus 5
Terrorterran 3
Dota 2
Dendi1470
XaKoH 546
XcaliburYe227
Counter-Strike
olofmeister2527
byalli741
x6flipin348
allub204
Other Games
B2W.Neo874
Lowko414
Pyrionflax305
crisheroes304
Beastyqt264
Happy148
hiko69
Organizations
Counter-Strike
PGL919
StarCraft: Brood War
UltimateBattle 531
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
[ Show 15 non-featured ]
StarCraft 2
• iHatsuTV 10
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• iopq 3
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• HappyZerGling53
League of Legends
• Jankos2913
• Stunt244
Upcoming Events
Monday Night Weeklies
3h 53m
Replay Cast
11h 53m
Kung Fu Cup
22h 53m
GSL
1d 21h
herO vs Classic
Cure vs Clem
uThermal 2v2 Circuit
2 days
Replay Cast
2 days
GSL
2 days
Maru vs SHIN
Zoun vs Rogue
WardiTV Spring Champion…
2 days
SKillous vs Strange
Lambo vs Strange
Ryung vs Strange
Lambo vs Ryung
Ryung vs SKillous
Lambo vs SKillous
Replay Cast
3 days
Maestros of the Game
4 days
[ Show More ]
Replay Cast
4 days
RSL Revival
4 days
Lambo vs SHIN
Solar vs Rogue
herO vs Clem
IPSL
5 days
ZZZero vs WorsT
Julia vs eOnzErG
Replay Cast
5 days
RSL Revival
5 days
IPSL
6 days
Dragon vs Artosis
dxtr13 vs Hawk
BSL
6 days
Wardi Open
6 days
Liquipedia Results

Completed

ASL Season 21
2026 GSL S1
Heroes Pulsing #1

Ongoing

2026 KK StarCraft Pro League
BSL Season 22
IPSL Spring 2026
KCM Race Survival 2026 Season 2
KK 2v2 League Season 1
YSL S3
Acropolis #4
CSCL: Masked Kings S4
SCTL 2026 Spring
WardiTV Spring 2026
2026 GSL S2
RSL Revival: Season 5
CS Asia Championships 2026
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026
BLAST Rivals Spring 2026
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026
ESL Pro League S23 Finals

Upcoming

Escore Tournament S2: King of Kings
BSL 22 Non-Korean Championship
CSLAN 4
Blizzard Classic Cup 2026
Kung Fu Cup 2026 Grand Finals
HSC XXIX
uThermal 2v2 2026 Main Event
Maestros of the Game 2
Bounty Cup 2026
BLAST Bounty Summer 2026
BLAST Bounty Summer Qual
Stake Ranked Episode 3
XSE Pro League 2026
IEM Cologne Major 2026
Stake Ranked Episode 2
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.