• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 16:31
CET 22:31
KST 06:31
  • 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 (Dec 15-21): Classic wins big, MaxPax & Clem take weeklies3ComeBackTV's documentary on Byun's Career !11Weekly Cups (Dec 8-14): MaxPax, Clem, Cure win4Weekly Cups (Dec 1-7): Clem doubles, Solar gets over the hump1Weekly Cups (Nov 24-30): MaxPax, Clem, herO win2
StarCraft 2
General
ComeBackTV's documentary on Byun's Career ! Team TLMC #5: Winners Announced! What's the best tug of war? The Grack before Christmas Weekly Cups (Dec 15-21): Classic wins big, MaxPax & Clem take weeklies
Tourneys
OSC Season 13 World Championship $5,000+ WardiTV 2025 Championship $100 Prize Pool - Winter Warp Gate Masters Showdow Sparkling Tuna Cup - Weekly Open Tournament Winter Warp Gate Amateur Showdown #1
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 505 Rise From Ashes Mutation # 504 Retribution Mutation # 503 Fowl Play Mutation # 502 Negative Reinforcement
Brood War
General
BW General Discussion How soO Began His ProGaming Dreams Klaucher discontinued / in-game color settings BGH Auto Balance -> http://bghmmr.eu/ Recommended FPV games (post-KeSPA)
Tourneys
[Megathread] Daily Proleagues [BSL21] LB SemiFinals - Saturday 21:00 CET [BSL21] WB & LB Finals - Sunday 21:00 CET Small VOD Thread 2.0
Strategy
Simple Questions, Simple Answers Game Theory for Starcraft Current Meta Fighting Spirit mining rates
Other Games
General Games
Nintendo Switch Thread Mechabellum Stormgate/Frost Giant Megathread Beyond All Reason Path of Exile
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
Mafia Game Mode Feedback/Ideas Survivor II: The Amazon Sengoku Mafia TL Mafia Community Thread
Community
General
US Politics Mega-thread 12 Days of Starcraft The Games Industry And ATVI Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine
Fan Clubs
White-Ra Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece
Sports
2024 - 2026 Football Thread Formula 1 Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List TL+ Announced Where to ask questions and add stream?
Blogs
National Diversity: A Challe…
TrAiDoS
I decided to write a webnov…
DjKniteX
James Bond movies ranking - pa…
Topin
Thanks for the RSL
Hildegard
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1442 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
Poland17546 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
Poland17546 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
Poland17546 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
Spain18159 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
BSL 21
20:00
LB SemiFinal
Sziky vs eOnzErG
ZZZero.O217
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
IndyStarCraft 215
DisKSc2 47
StarCraft: Brood War
Shuttle 622
ZZZero.O 217
Dewaltoss 159
JulyZerg 42
910 35
Dota 2
syndereN379
Counter-Strike
fl0m1423
pashabiceps1337
Heroes of the Storm
Liquid`Hasu455
Khaldor198
Other Games
Grubby7048
tarik_tv3257
Beastyqt885
B2W.Neo317
ToD286
RotterdaM234
ArmadaUGS135
XaKoH 108
Railgan19
ViBE15
Organizations
Other Games
gamesdonequick1249
StarCraft 2
angryscii 41
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 18 non-featured ]
StarCraft 2
• StrangeGG 69
• printf 53
• davetesta49
• IndyKCrew
• Migwel
• AfreecaTV YouTube
• intothetv
• Kozan
• sooper7s
• LaughNgamezSOOP
StarCraft: Brood War
• 80smullet 21
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• WagamamaTV1272
• Ler81
League of Legends
• Stunt249
Other Games
• imaqtpie2751
Upcoming Events
Sparkling Tuna Cup
12h 29m
Krystianer vs Classic
TriGGeR vs SKillous
Percival vs Ryung
ByuN vs Nicoract
OSC
20h 29m
BSL 21
22h 29m
Cross vs Dewalt
Replay Cast
1d 11h
Wardi Open
1d 14h
OSC
2 days
Solar vs MaxPax
ByuN vs Krystianer
Spirit vs TBD
OSC
5 days
Korean StarCraft League
6 days
OSC
6 days
OSC
6 days
Liquipedia Results

Completed

Escore Tournament S1 - W1
WardiTV 2025
META Madness #9

Ongoing

C-Race Season 1
IPSL Winter 2025-26
BSL Season 21
CSL Season 19: Qualifier 2
eXTREMESLAND 2025
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025

Upcoming

CSL 2025 WINTER (S19)
Escore Tournament S1 - W2
Escore Tournament S1 - W3
BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
Big Gabe Cup #3
OSC Championship Season 13
Nations Cup 2026
ESL Pro League Season 23
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
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.