• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 19:20
CET 01:20
KST 09:20
  • 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
Behind the Blue - Team Liquid History Book15Clem wins HomeStory Cup 289HomeStory Cup 28 - Info & Preview13Rongyi Cup S3 - Preview & Info8herO wins SC2 All-Star Invitational14
Community News
ACS replaced by "ASL Season Open" - Starts 21/0220LiuLi Cup: 2025 Grand Finals (Feb 10-16)26Weekly Cups (Feb 2-8): Classic, Solar, MaxPax win2Nexon's StarCraft game could be FPS, led by UMS maker10PIG STY FESTIVAL 7.0! (19 Feb - 1 Mar)14
StarCraft 2
General
How do you think the 5.0.15 balance patch (Oct 2025) for StarCraft II has affected the game? Nexon's StarCraft game could be FPS, led by UMS maker Terran Scanner Sweep Behind the Blue - Team Liquid History Book Weekly Cups (Jan 12-18): herO, MaxPax, Solar win
Tourneys
PIG STY FESTIVAL 7.0! (19 Feb - 1 Mar) LiuLi Cup: 2025 Grand Finals (Feb 10-16) RSL Season 4 announced for March-April RSL Revival: Season 4 Korea Qualifier (Feb 14) Sparkling Tuna Cup - Weekly Open Tournament
Strategy
Custom Maps
Map Editor closed ? [A] Starcraft Sound Mod
External Content
The PondCast: SC2 News & Results Mutation # 512 Overclocked Mutation # 511 Temple of Rebirth Mutation # 510 Safety Violation
Brood War
General
Which units you wish saw more use in the game? ACS replaced by "ASL Season Open" - Starts 21/02 StarCraft player reflex TE scores [ASL21] Potential Map Candidates Gypsy to Korea
Tourneys
[Megathread] Daily Proleagues Escore Tournament StarCraft Season 1 Small VOD Thread 2.0 KCM Race Survival 2026 Season 1
Strategy
Fighting Spirit mining rates Zealot bombing is no longer popular? Simple Questions, Simple Answers Current Meta
Other Games
General Games
Nintendo Switch Thread Path of Exile Diablo 2 thread Battle Aces/David Kim RTS Megathread ZeroSpace 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
TL Mafia Community Thread Mafia Game Mode Feedback/Ideas Vanilla Mini Mafia
Community
General
US Politics Mega-thread Ask and answer stupid questions here! European Politico-economics QA Mega-thread The Games Industry And ATVI Russo-Ukrainian War Thread
Fan Clubs
The IdrA Fan Club The herO Fan Club!
Media & Entertainment
[Req][Books] Good Fantasy/SciFi books [Manga] One Piece Anime Discussion Thread
Sports
2024 - 2026 Football Thread
World Cup 2022
Tech Support
TL Community
The Automated Ban List
Blogs
ADHD And Gaming Addiction…
TrAiDoS
My 2025 Magic: The Gathering…
DARKING
Life Update and thoughts.
FuDDx
How do archons sleep?
8882
StarCraft improvement
iopq
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1856 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
Poland17661 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
Poland17661 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
Poland17661 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
Spain18215 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
Replay Cast
00:00
HomeStory Cup 28 - Group C
CranKy Ducklings51
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft: Brood War
NaDa 15
Dota 2
Dendi1695
syndereN425
League of Legends
JimRising 506
Other Games
gofns20684
tarik_tv14769
summit1g11570
FrodaN5315
KnowMe293
ToD255
JuggernautJason25
Organizations
Other Games
gamesdonequick1601
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 17 non-featured ]
StarCraft 2
• musti20045 45
• davetesta45
• Kozan
• LaughNgamezSOOP
• sooper7s
• AfreecaTV YouTube
• intothetv
• Migwel
• IndyKCrew
StarCraft: Brood War
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• WagamamaTV266
• Noizen48
League of Legends
• Doublelift5678
Other Games
• imaqtpie1723
• Shiphtur229
Upcoming Events
Sparkling Tuna Cup
9h 40m
LiuLi Cup
10h 40m
Maru vs Reynor
Serral vs Rogue
Ladder Legends
17h 40m
Replay Cast
23h 40m
Replay Cast
1d 8h
Wardi Open
1d 11h
Monday Night Weeklies
1d 16h
OSC
1d 23h
WardiTV Winter Champion…
2 days
Replay Cast
3 days
[ Show More ]
WardiTV Winter Champion…
3 days
Replay Cast
3 days
PiG Sty Festival
4 days
The PondCast
4 days
KCM Race Survival
4 days
WardiTV Winter Champion…
4 days
Replay Cast
4 days
PiG Sty Festival
5 days
Epic.LAN
5 days
Replay Cast
5 days
PiG Sty Festival
6 days
CranKy Ducklings
6 days
Epic.LAN
6 days
Replay Cast
6 days
Liquipedia Results

Completed

Proleague 2026-02-14
Rongyi Cup S3
Underdog Cup #3

Ongoing

KCM Race Survival 2026 Season 1
LiuLi Cup: 2025 Grand Finals
Nations Cup 2026
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
eXTREMESLAND 2025
SL Budapest Major 2025

Upcoming

Escore Tournament S1: King of Kings
[S:21] ASL SEASON OPEN 1st Round
[S:21] ASL SEASON OPEN 1st Round Qualifier
[S:21] ASL SEASON OPEN 2nd Round
[S:21] ASL SEASON OPEN 2nd Round Qualifier
Acropolis #4
IPSL Spring 2026
HSC XXIX
uThermal 2v2 2026 Main Event
Bellum Gens Elite Stara Zagora 2026
RSL Revival: Season 4
WardiTV Winter 2026
BLAST Rivals Spring 2026
CCT Season 3 Global Finals
FISSURE Playground #3
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026
ESL Pro League Season 23
ESL Pro League Season 23
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.