• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 13:15
CET 19:15
KST 03:15
  • 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
TL.net Map Contest #21: Winners11Intel X Team Liquid Seoul event: Showmatches and Meet the Pros10[ASL20] Finals Preview: Arrival13TL.net Map Contest #21: Voting12[ASL20] Ro4 Preview: Descent11
Community News
StarCraft, SC2, HotS, WC3, Returning to Blizzcon!45$5,000+ WardiTV 2025 Championship7[BSL21] RO32 Group Stage4Weekly Cups (Oct 26-Nov 2): Liquid, Clem, Solar win; LAN in Philly2Weekly Cups (Oct 20-26): MaxPax, Clem, Creator win10
StarCraft 2
General
SC: Evo Complete - Ranked Ladder OPEN ALPHA Mech is the composition that needs teleportation t TL.net Map Contest #21: Winners StarCraft, SC2, HotS, WC3, Returning to Blizzcon! RotterdaM "Serral is the GOAT, and it's not close"
Tourneys
Constellation Cup - Main Event - Stellar Fest Sparkling Tuna Cup - Weekly Open Tournament $5,000+ WardiTV 2025 Championship Merivale 8 Open - LAN - Stellar Fest Sea Duckling Open (Global, Bronze-Diamond)
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 499 Chilling Adaptation Mutation # 498 Wheel of Misfortune|Cradle of Death Mutation # 497 Battle Haredened Mutation # 496 Endless Infection
Brood War
General
FlaSh on: Biggest Problem With SnOw's Playstyle BGH Auto Balance -> http://bghmmr.eu/ [ASL20] Ask the mapmakers — Drop your questions BW General Discussion Where's CardinalAllin/Jukado the mapmaker?
Tourneys
[Megathread] Daily Proleagues [ASL20] Grand Finals [BSL21] RO32 Group A - Saturday 21:00 CET [BSL21] RO32 Group B - Sunday 21:00 CET
Strategy
PvZ map balance Current Meta How to stay on top of macro? Soma's 9 hatch build from ASL Game 2
Other Games
General Games
Stormgate/Frost Giant Megathread Should offensive tower rushing be viable in RTS games? Nintendo Switch Thread Path of Exile Dawn of War IV
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
TL Mafia Community Thread SPIRED by.ASL Mafia {211640}
Community
General
Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine Canadian Politics Mega-thread US Politics Mega-thread The Games Industry And ATVI
Fan Clubs
White-Ra Fan Club The herO Fan Club!
Media & Entertainment
[Manga] One Piece Anime Discussion Thread Movie Discussion! Korean Music Discussion Series you have seen recently...
Sports
2024 - 2026 Football Thread Formula 1 Discussion NBA General Discussion MLB/Baseball 2023 TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
SC2 Client Relocalization [Change SC2 Language] Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List
Blogs
Learning my new SC2 hotkey…
Hildegard
Coffee x Performance in Espo…
TrAiDoS
Saturation point
Uldridge
DnB/metal remix FFO Mick Go…
ImbaTosS
Reality "theory" prov…
perfectspheres
Our Last Hope in th…
KrillinFromwales
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1430 users

The Big Programming Thread - Page 874

Forum Index > General Forum
Post a Reply
Prev 1 872 873 874 875 876 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.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
April 19 2017 20:32 GMT
#17461
Question I thought was clever on an old exam:


Implement the function free_array with prototype shown below that frees the dynamically-allocated
memory associated with each array element in the first parameter passed to the function. The function needs to handle
the scenario where two or more array entries are pointing to the same dynamically-allocated memory (only one free
can be applied to a memory location). It is OK to modify the original array as part of your implementation, but you
MAY NOT define a new array in the function and you may not allocate more memory. If you do you will lose a
significant number of points.

void free_array(void *a[], int length) {


kinda a hard question since based on the exam length we are supposed to be able to write it in about 1 minute, lol

curious how many of you could get this in less than a minute
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
April 19 2017 20:56 GMT
#17462
On April 20 2017 05:32 travis wrote:
Question I thought was clever on an old exam:


Implement the function free_array with prototype shown below that frees the dynamically-allocated
memory associated with each array element in the first parameter passed to the function. The function needs to handle
the scenario where two or more array entries are pointing to the same dynamically-allocated memory (only one free
can be applied to a memory location). It is OK to modify the original array as part of your implementation, but you
MAY NOT define a new array in the function and you may not allocate more memory. If you do you will lose a
significant number of points.

void free_array(void *a[], int length) {


kinda a hard question since based on the exam length we are supposed to be able to write it in about 1 minute, lol

curious how many of you could get this in less than a minute


I don't do C manual memory management, but my guess is:

1. for loop
2. Check if null.
a. if not, free element and then set it to null
b. if null, go to next one
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
April 19 2017 21:04 GMT
#17463
nope won't work you will potentially free the same memory 2 or more times
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2017-04-19 21:09:50
April 19 2017 21:09 GMT
#17464
I don't see how it'll be freed more than once. It's set to null once you free 1 time. This isn't rocket science.

free(stuff);
stuff = NULL;

Next time:

if (stuff)
repeat
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2017-04-19 21:29:31
April 19 2017 21:28 GMT
#17465
int *p, *j;
p = malloc(2);
j = p;
ourfunction(array with p and j in it);


ourfunction(*array[]) {
for(x = 0; x < total elements; x++) {
if(array[x] != null) {
free(array[x]);
array[x] = null;
}
}
}

j will not be null because it points to our malloc. j gets freed, then set to null
p will not be null because it points to our malloc, our malloc will be freed a 2nd time (in error)
Manit0u
Profile Blog Joined August 2004
Poland17423 Posts
Last Edited: 2017-04-19 21:43:26
April 19 2017 21:29 GMT
#17466
On April 20 2017 05:32 travis wrote:
curious how many of you could get this in less than a minute


You either know how to do it right off the bat or you don't. If you do, you can do almost anything in a minute...

Also, won't the other pointer automatically be dereferenced when the memory it's pointing to goes away?

ie:


p & j are pointers to the same memory.

free(p);
p = NULL;
free(j); // nothing happens here since j is NULL at this point.


Essentially, what you could do is this:


ourfunction(*array[]) {
for (x = 0; x < total elements; x++) {
free(array[x]); // no need to check if array[x] is null since free doesn't care
array[x] = NULL;
}
}
Time is precious. Waste it wisely.
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2017-04-19 21:46:30
April 19 2017 21:44 GMT
#17467
Same stuff with C++ delete, I just didn't want to assume free is smart. Either way, my suggestion should work.

The key is in what is mentioned above:


free(j); // nothing happens here since j is NULL at this point.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2017-04-19 21:52:13
April 19 2017 21:47 GMT
#17468
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++ ?
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2017-04-19 21:52:27
April 19 2017 21:52 GMT
#17469
I see what you mean. Well, it seems you can't do much about it.

http://stackoverflow.com/a/8300866

Why would you end up with 2 different pointers pointing to the same thing? I don't remember doing this at work. Universities with dumb examples again.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
April 19 2017 21:53 GMT
#17470
I can't imagine why you would but this is the sort of crap they test us on, LOL
BluzMan
Profile Blog Joined April 2006
Russian Federation4235 Posts
April 19 2017 21:55 GMT
#17471
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.
You want 20 good men, but you need a bad pussy.
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
April 19 2017 21:56 GMT
#17472
Yes, it's stupid when people at universities decide to test meaningless, corner case which won't be used in real environment.
Either way, my C experience is limited. I work with C++ which is very far away from C raw pointers nowadays.
BluzMan
Profile Blog Joined April 2006
Russian Federation4235 Posts
April 19 2017 22:09 GMT
#17473
On April 20 2017 06:56 Shield wrote:
Yes, it's stupid when people at universities decide to test meaningless, corner case which won't be used in real environment.
Either way, my C experience is limited. I work with C++ which is very far away from C raw pointers nowadays.

Well, a situation with an array of possibly aliasing pointers is possible in C++ too, albeit not very probable, I see no harm in putting that into test. You can alleviate aliasing pointers with shared_ptr, but that comes at a price you sometimes don't want to pay.
You want 20 good men, but you need a bad pussy.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
April 19 2017 22:25 GMT
#17474
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.


well that's a way cooler solution than the given solution, I guess technically yours is faster than the given solution too
Nesserev
Profile Blog Joined January 2011
Belgium2760 Posts
Last Edited: 2017-04-19 22:53:08
April 19 2017 22:40 GMT
#17475
--- Nuked ---
Manit0u
Profile Blog Joined August 2004
Poland17423 Posts
Last Edited: 2017-04-19 23:08:17
April 19 2017 23:03 GMT
#17476
On April 20 2017 07:25 travis 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.


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);
}
Time is precious. Waste it wisely.
Manit0u
Profile Blog Joined August 2004
Poland17423 Posts
April 19 2017 23:06 GMT
#17477
On April 20 2017 07:40 Nesserev wrote:
So, I would like to check something with you guys. Today I installed an external harddrive for my father for daily automatic back-ups with the goal of keeping certain data safe from cryptolocker and such; however, I'm not sure whether or not I missed something, and I'm not a windows user :/.

The original setup was simple:
- 1 account called 'daddy69' with admin rights and password (not really 'daddy69', but whatever)
- everyone has access rights to external harddrive

After I was done, the setup was as follows:
- 1 account called 'backup' with admin rights and password, access to external harddrive and daddy69's directories
- 1 account called 'daddy69' with a password and no admin rights, can only detect but not access external harddrive
- only admins have access rights to external harddrive

I wrote a batchfile that automates the whole process of backing up certain directories to a fresh directory on the external drive. Then I added an automated task to run that script once every day when he's supposed to be sleeping. It seems to be working fine.

Anything I should change, or any recommendations? Did I miss anything, and did I reach my goals? Is this setup robust enough to survive cryptolocker or ... ?


http://lifehacker.com/how-to-back-up-your-computer-automatically-with-windows-1762867473
Time is precious. Waste it wisely.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
April 19 2017 23:30 GMT
#17478
**p;
*k;

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

both of these statements are the same?
Acrofales
Profile Joined August 2010
Spain18112 Posts
April 19 2017 23:45 GMT
#17479
Been a while since I programmed in C, but I doubt the statement 2 is valid syntax.
Nesserev
Profile Blog Joined January 2011
Belgium2760 Posts
Last Edited: 2017-04-20 00:07:48
April 20 2017 00:03 GMT
#17480
--- Nuked ---
Prev 1 872 873 874 875 876 1032 Next
Please log in or register to reply.
Live Events Refresh
Wardi Open
12:00
#60
WardiTV2250
IndyStarCraft 235
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
IndyStarCraft 235
UpATreeSC 87
StarCraft: Brood War
Rain 3549
Horang2 1587
Shuttle 779
firebathero 160
scan(afreeca) 52
sSak 37
Mong 30
Aegong 22
JulyZerg 17
ivOry 6
[ Show more ]
SilentControl 5
Dota 2
Gorgc5362
qojqva3580
420jenkins277
BananaSlamJamma178
XcaliburYe153
League of Legends
rGuardiaN20
Counter-Strike
fl0m572
byalli525
Other Games
FrodaN1033
Beastyqt715
ceh9567
KnowMe280
Lowko263
Sick257
Hui .188
Mew2King150
Liquid`VortiX147
ArmadaUGS79
QueenE48
Trikslyr46
Organizations
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 18 non-featured ]
StarCraft 2
• kabyraGe 105
• Reevou 1
• IndyKCrew
• LaughNgamezSOOP
• sooper7s
• AfreecaTV YouTube
• Migwel
• intothetv
• Kozan
StarCraft: Brood War
• Michael_bg 9
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• WagamamaTV218
League of Legends
• Nemesis3571
• TFBlade957
• imaqtpie471
Other Games
• Shiphtur284
Upcoming Events
Replay Cast
4h 45m
WardiTV Korean Royale
17h 45m
OSC
22h 45m
Replay Cast
1d 4h
Replay Cast
1d 14h
Kung Fu Cup
1d 17h
Classic vs Solar
herO vs Cure
Reynor vs GuMiho
ByuN vs ShoWTimE
Tenacious Turtle Tussle
2 days
The PondCast
2 days
RSL Revival
2 days
Solar vs Zoun
MaxPax vs Bunny
Kung Fu Cup
2 days
[ Show More ]
WardiTV Korean Royale
2 days
PiGosaur Monday
3 days
RSL Revival
3 days
Classic vs Creator
Cure vs TriGGeR
Kung Fu Cup
3 days
CranKy Ducklings
4 days
RSL Revival
4 days
herO vs Gerald
ByuN vs SHIN
Kung Fu Cup
4 days
BSL 21
5 days
Tarson vs Julia
Doodle vs OldBoy
eOnzErG vs WolFix
StRyKeR vs Aeternum
Sparkling Tuna Cup
5 days
RSL Revival
5 days
Reynor vs sOs
Maru vs Ryung
Kung Fu Cup
5 days
WardiTV Korean Royale
5 days
BSL 21
6 days
JDConan vs Semih
Dragon vs Dienmax
Tech vs NewOcean
TerrOr vs Artosis
Wardi Open
6 days
Monday Night Weeklies
6 days
Liquipedia Results

Completed

Proleague 2025-11-07
Stellar Fest: Constellation Cup
Eternal Conflict S1

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
SOOP Univ League 2025
YSL S2
BSL Season 21
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual

Upcoming

SLON Tour Season 2
BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
HSC XXVIII
RSL Offline Finals
WardiTV 2025
RSL Revival: Season 3
META Madness #9
BLAST Bounty Winter 2026
BLAST Bounty Winter 2026: Closed Qualifier
eXTREMESLAND 2025
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals Fall 2025
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.