• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 21:07
CEST 03:07
KST 10: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
[ASL19] Finals Recap: Standing Tall9HomeStory Cup 27 - Info & Preview18Classic wins Code S Season 2 (2025)16Code S RO4 & Finals Preview: herO, Rogue, Classic, GuMiho0TL Team Map Contest #5: Presented by Monster Energy6
Community News
[BSL20] Non-Korean Championship 4x BSL + 4x China1Flash Announces Hiatus From ASL63Weekly Cups (June 23-29): Reynor in world title form?13FEL Cracov 2025 (July 27) - $8000 live event22Esports World Cup 2025 - Final Player Roster16
StarCraft 2
General
Program: SC2 / XSplit / OBS Scene Switcher The SCII GOAT: A statistical Evaluation Statistics for vetoed/disliked maps Weekly Cups (June 23-29): Reynor in world title form? PiG Sty Festival #5: Playoffs Preview + Groups Recap
Tourneys
RSL: Revival, a new crowdfunded tournament series FEL Cracov 2025 (July 27) - $8000 live event Sparkling Tuna Cup - Weekly Open Tournament WardiTV Mondays Korean Starcraft League Week 77
Strategy
How did i lose this ZvP, whats the proper response Simple Questions Simple Answers
Custom Maps
[UMS] Zillion Zerglings
External Content
Mutation # 481 Fear and Lava Mutation # 480 Moths to the Flame Mutation # 479 Worn Out Welcome Mutation # 478 Instant Karma
Brood War
General
Player “Jedi” cheat on CSL SC uni coach streams logging into betting site Flash Announces Hiatus From ASL BW General Discussion Practice Partners (Official)
Tourneys
[BSL20] Non-Korean Championship 4x BSL + 4x China CSL Xiamen International Invitational The Casual Games of the Week Thread [BSL20] Grand Finals - Sunday 20:00 CET
Strategy
Simple Questions, Simple Answers I am doing this better than progamers do.
Other Games
General Games
Path of Exile Stormgate/Frost Giant Megathread Nintendo Switch Thread What do you want from future RTS games? Beyond All Reason
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
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Summer Games Done Quick 2025! Trading/Investing Thread Things Aren’t Peaceful in Palestine
Fan Clubs
SKT1 Classic Fan Club! Maru Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece [\m/] Heavy Metal Thread
Sports
Formula 1 Discussion 2024 - 2025 Football Thread NBA General Discussion TeamLiquid Health and Fitness Initiative For 2023 NHL Playoffs 2024
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
Blogs
Culture Clash in Video Games…
TrAiDoS
from making sc maps to makin…
Husyelt
Blog #2
tankgirl
StarCraft improvement
iopq
Trip to the Zoo
micronesia
Customize Sidebar...

Website Feedback

Closed Threads



Active: 635 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
Poland17243 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
Poland17243 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
Poland17243 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
Spain17971 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 9h 53m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Livibee 237
Nina 214
RuFF_SC2 96
ProTech74
StarCraft: Brood War
Artosis 992
Icarus 5
Dota 2
monkeys_forever667
Counter-Strike
tarik_tv7163
Fnx 1821
Stewie2K1147
Super Smash Bros
Mew2King133
Heroes of the Storm
Khaldor195
Other Games
summit1g10102
fl0m778
ViBE246
Maynarde184
JuggernautJason23
Organizations
Other Games
gamesdonequick46739
BasetradeTV56
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 14 non-featured ]
StarCraft 2
• davetesta35
• Berry_CruncH32
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• Ler56
League of Legends
• masondota2795
Upcoming Events
Wardi Open
9h 53m
Replay Cast
22h 53m
Sparkling Tuna Cup
1d 8h
WardiTV European League
1d 14h
MaNa vs sebesdes
Mixu vs Fjant
ByuN vs HeRoMaRinE
ShoWTimE vs goblin
Gerald vs Babymarine
Krystianer vs YoungYakov
PiGosaur Monday
1d 22h
The PondCast
2 days
WardiTV European League
2 days
Jumy vs NightPhoenix
Percival vs Nicoract
ArT vs HiGhDrA
MaxPax vs Harstem
Scarlett vs Shameless
SKillous vs uThermal
uThermal 2v2 Circuit
2 days
Replay Cast
2 days
RSL Revival
3 days
ByuN vs SHIN
Clem vs Reynor
[ Show More ]
Replay Cast
3 days
RSL Revival
4 days
Classic vs Cure
FEL
4 days
RSL Revival
5 days
FEL
5 days
FEL
5 days
Sparkling Tuna Cup
6 days
RSL Revival
6 days
FEL
6 days
Liquipedia Results

Completed

BSL Season 20
HSC XXVII
Heroes 10 EU

Ongoing

JPL Season 2
Acropolis #3
KCM Race Survival 2025 Season 2
CSL 17: 2025 SUMMER
Copa Latinoamericana 4
Jiahua Invitational
Championship of Russia 2025
RSL Revival: Season 1
Murky Cup #2
BLAST.tv Austin Major 2025
ESL Impact League Season 7
IEM Dallas 2025
PGL Astana 2025
Asian Champions League '25
BLAST Rivals Spring 2025
MESA Nomadic Masters
CCT Season 2 Global Finals
IEM Melbourne 2025

Upcoming

2025 ACS Season 2: Qualifier
CSLPRO Last Chance 2025
CSL Xiamen Invitational
2025 ACS Season 2
CSLPRO Chat StarLAN 3
K-Championship
uThermal 2v2 Main Event
SEL Season 2 Championship
FEL Cracov 2025
Esports World Cup 2025
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
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.