• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 05:42
CEST 11:42
KST 18:42
  • 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
Team TLMC #5 - Finalists & Open Tournaments0[ASL20] Ro16 Preview Pt2: Turbulence10Classic Games #3: Rogue vs Serral at BlizzCon9[ASL20] Ro16 Preview Pt1: Ascent10Maestros of the Game: Week 1/Play-in Preview12
Community News
BSL 2025 Warsaw LAN + Legends Showmatch0Weekly Cups (Sept 8-14): herO & MaxPax split cups4WardiTV TL Team Map Contest #5 Tournaments1SC4ALL $6,000 Open LAN in Philadelphia8Weekly Cups (Sept 1-7): MaxPax rebounds & Clem saga continues29
StarCraft 2
General
#1: Maru - Greatest Players of All Time Weekly Cups (Sept 8-14): herO & MaxPax split cups Team Liquid Map Contest #21 - Presented by Monster Energy SpeCial on The Tasteless Podcast Team TLMC #5 - Finalists & Open Tournaments
Tourneys
Maestros of The Game—$20k event w/ live finals in Paris Sparkling Tuna Cup - Weekly Open Tournament SC4ALL $6,000 Open LAN in Philadelphia WardiTV TL Team Map Contest #5 Tournaments RSL: Revival, a new crowdfunded tournament series
Strategy
Custom Maps
External Content
Mutation # 491 Night Drive Mutation # 490 Masters of Midnight Mutation # 489 Bannable Offense Mutation # 488 What Goes Around
Brood War
General
Soulkey on ASL S20 ASL TICKET LIVE help! :D BW General Discussion NaDa's Body A cwal.gg Extension - Easily keep track of anyone
Tourneys
[ASL20] Ro16 Group D [ASL20] Ro16 Group C [Megathread] Daily Proleagues BSL 2025 Warsaw LAN + Legends Showmatch
Strategy
Simple Questions, Simple Answers Muta micro map competition Fighting Spirit mining rates [G] Mineral Boosting
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread Path of Exile Borderlands 3 General RTS Discussion Thread
Dota 2
Official 'what is Dota anymore' discussion LiquidDota to reintegrate into TL.net
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread
Community
General
US Politics Mega-thread Canadian Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread UK Politics Mega-thread
Fan Clubs
The Happy Fan Club!
Media & Entertainment
Movie Discussion! [Manga] One Piece Anime Discussion Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion MLB/Baseball 2023
World Cup 2022
Tech Support
Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread High temperatures on bridge(s)
TL Community
BarCraft in Tokyo Japan for ASL Season5 Final The Automated Ban List
Blogs
I <=> 9
KrillinFromwales
The Personality of a Spender…
TrAiDoS
A very expensive lesson on ma…
Garnet
hello world
radishsoup
Lemme tell you a thing o…
JoinTheRain
RTS Design in Hypercoven
a11
Evil Gacha Games and the…
ffswowsucks
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1583 users

The Big Programming Thread - Page 874

Forum Index > General Forum
Post a Reply
Prev 1 872 873 874 875 876 1031 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
Poland17341 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
Poland17341 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
Poland17341 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
Spain18050 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 1031 Next
Please log in or register to reply.
Live Events Refresh
Next event in 18m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Rex 1
StarCraft: Brood War
Calm 5699
Bisu 731
Hyuk 162
HiyA 96
Hyun 96
sorry 86
ToSsGirL 84
Dewaltoss 82
Pusan 77
Light 74
[ Show more ]
Soma 60
actioN 56
Mini 49
ZerO 32
BeSt 30
Liquid`Ret 29
Nal_rA 28
soO 27
Sharp 24
Rush 19
Free 16
SilentControl 10
Dota 2
singsing1260
XcaliburYe206
boxi98170
League of Legends
JimRising 381
Counter-Strike
olofmeister1583
shoxiejesuss598
allub166
Other Games
XaKoH 143
NeuroSwarm80
Trikslyr15
Organizations
StarCraft: Brood War
lovetv 593
Other Games
gamesdonequick587
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 14 non-featured ]
StarCraft 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• iopq 1
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Jankos1421
• Stunt679
Other Games
• WagamamaTV81
Upcoming Events
RSL Revival
18m
Maru vs Reynor
Cure vs TriGGeR
Rex1
Map Test Tournament
1h 18m
The PondCast
3h 18m
RSL Revival
1d
Zoun vs Classic
Korean StarCraft League
1d 17h
BSL Open LAN 2025 - War…
1d 22h
RSL Revival
2 days
BSL Open LAN 2025 - War…
2 days
RSL Revival
3 days
Online Event
3 days
[ Show More ]
Wardi Open
4 days
Monday Night Weeklies
4 days
Sparkling Tuna Cup
5 days
LiuLi Cup
6 days
Liquipedia Results

Completed

Proleague 2025-09-10
Chzzk MurlocKing SC1 vs SC2 Cup #2
HCC Europe

Ongoing

BSL 20 Team Wars
KCM Race Survival 2025 Season 3
BSL 21 Points
ASL Season 20
CSL 2025 AUTUMN (S18)
LASL Season 20
RSL Revival: Season 2
Maestros of the Game
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1

Upcoming

2025 Chongqing Offline CUP
BSL World Championship of Poland 2025
IPSL Winter 2025-26
BSL Season 21
SC4ALL: Brood War
BSL 21 Team A
Stellar Fest
SC4ALL: StarCraft II
EC S1
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
MESA Nomadic Masters Fall
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
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.