• 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: 1425 users

The Big Programming Thread - Page 576

Forum Index > General Forum
Post a Reply
Prev 1 574 575 576 577 578 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.
Dagobert
Profile Blog Joined July 2009
Netherlands1858 Posts
Last Edited: 2015-02-02 11:31:26
February 02 2015 11:27 GMT
#11501
From abductedonut's post:
Ah, and just for some light reading... Here's a post by Linus Torvalds... the creator of the Linux operating system. I think this is hilariously interesting!


Actually, the "operating system" you're talking about is called GNU. Linux is the kernel.

While I'm at it, here's a link to a talk by the initiator of the GNU operating system:
+ Show Spoiler +

LaNague
Profile Blog Joined April 2010
Germany9118 Posts
February 02 2015 13:30 GMT
#11502
shifting bits and comparing bit per bit is really useful when you work with hardware registers, like in micopocessors in machinery, because often how and what they do is determined by what bits are set to 1 or 0.
Nesserev
Profile Blog Joined January 2011
Belgium2760 Posts
February 02 2015 14:34 GMT
#11503
--- Nuked ---
spinesheath
Profile Blog Joined June 2009
Germany8679 Posts
February 02 2015 17:37 GMT
#11504
On February 02 2015 20:27 Dagobert wrote:
While I'm at it, here's a link to a talk by the initiator of the GNU operating system:
+ Show Spoiler +
https://www.youtube.com/watch?v=Ag1AKIl_2GM


"when you bring cookies to school you can't keep them to yourself, you've got to share them" - in a talk about freedom.
Not that I dislike the idea, but he really doesn't make a lot of compelling arguments.
If you have a good reason to disagree with the above, please tell me. Thank you.
Ben...
Profile Joined January 2011
Canada3485 Posts
February 02 2015 18:47 GMT
#11505
On February 02 2015 23:34 Nesserev wrote:
Show nested quote +
On January 31 2015 13:40 Nesserev wrote:
Poll: Vote on the book that you would like to read:

Facts and Fallacies of Software Engineering (0)
 
0%

Coders at Work (0)
 
0%

Programming Pearls (2)
 
22%

Code Complete (7)
 
78%

9 total votes

Your vote: Vote on the book that you would like to read:

(Vote): Facts and Fallacies of Software Engineering
(Vote): Coders at Work
(Vote): Programming Pearls
(Vote): Code Complete


Well, seems like the first book will be Code Complete.

With 960 pages, Code Complete is kind of a beast to conquer, which is both terrifying and exciting. It's rather cheap on amazon (if you're used to academic prices), and it's such a popular book that you can probably get it at your campus library. Well, I don't think anyone will have any problems getting their hands on a copy.

I wonder if most people will be able to finish it in only one month

I have that book and have been chipping away at it since summer a bit at a time. It is a dense read to say the least.
"Cliiiiiiiiiiiiiiiiide" -Tastosis
Cynry
Profile Blog Joined August 2010
810 Posts
February 02 2015 23:07 GMT
#11506
Hey guys, I have a question NOT regarding an assignment this time. Nor it is a segfault :D
So, I was there doing a realloc function for some int * arrays, and I wondered, would it be possible to do that for any type of array, with the same function.

# define TAB_TYPE char

TAB_TYPE **tab_realloc(TAB_TYPE **old_tab, int old_size, int new_size)
{
TAB_TYPE **res;
int i;

if (!(res = (TAB_TYPE **)ft_memalloc(sizeof(TAB_TYPE *) * new_size)))
return (NULL);
i = 0;
while (i < new_size)
{
if (i < old_size)
{
res[i] = old_tab[i];
ft_memdel((void **)&old_tab[i]);
}
else
res[i] = NULL;
i++;
}
ft_memdel((void **)old_tab);
return (res);
}


This is where I got, and it doesn't quite work. No problem if TAB_TYPE is an int, but for char some of the data is copied and then disappear for some reason. I would be interested in the reason, but that's not what I'm after. Is something like this even possible ? One would just define what type of data the arrays are made of, and the function does it work properly no matter said type ?
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2015-02-02 23:22:38
February 02 2015 23:20 GMT
#11507
1. res[i] = old_tab[i] says set the res[i] to be the address to a TAB_TYPE that old_tab[i] points to.
2. ft_memdel((void **)&old_tab[i]) says free the memory at the address specified by old_tab[i].

that means res[i] now points (is an address) to memory that has been freed, and calls to res[i][x]* have undefined behavior.

*i think
There is no one like you in the universe.
meatpudding
Profile Joined March 2011
Australia520 Posts
February 03 2015 06:03 GMT
#11508
Are you just trying to copy the contents of one array to another? Sounds like a job for memcpy.

res = malloc(sizeof(void*) * new_size);
memset(res, 0, sizeof(void*) * new_size);
if (new_size < old_size)
memcpy(res, old_tab, sizeof(void*) * new_size);
else
memcpy(res, old_tab, sizeof(void*) * old_size);
free(old_tab);
return res;
Be excellent to each other.
memcpy
Profile Blog Joined April 2010
United States459 Posts
Last Edited: 2015-02-03 06:37:33
February 03 2015 06:37 GMT
#11509
On February 03 2015 15:03 meatpudding wrote:
Are you just trying to copy the contents of one array to another? Sounds like a job for memcpy.


You called?
Arnstein
Profile Blog Joined May 2010
Norway3381 Posts
February 03 2015 08:29 GMT
#11510
On February 03 2015 15:37 memcpy wrote:
Show nested quote +
On February 03 2015 15:03 meatpudding wrote:
Are you just trying to copy the contents of one array to another? Sounds like a job for memcpy.


You called?


Haha!

On February 02 2015 23:34 Nesserev wrote:
Show nested quote +
On January 31 2015 13:40 Nesserev wrote:
Poll: Vote on the book that you would like to read:

Facts and Fallacies of Software Engineering (0)
 
0%

Coders at Work (0)
 
0%

Programming Pearls (2)
 
22%

Code Complete (7)
 
78%

9 total votes

Your vote: Vote on the book that you would like to read:

(Vote): Facts and Fallacies of Software Engineering
(Vote): Coders at Work
(Vote): Programming Pearls
(Vote): Code Complete


Well, seems like the first book will be Code Complete.

With 960 pages, Code Complete is kind of a beast to conquer, which is both terrifying and exciting. It's rather cheap on amazon (if you're used to academic prices), and it's such a popular book that you can probably get it at your campus library. Well, I don't think anyone will have any problems getting their hands on a copy.

I wonder if most people will be able to finish it in only one month


There's no problem in having it for more than one month, it definitely is worth it. Also, it's the best thing Microsoft ever released.
rsol in response to the dragoon voice being heard in SCII: dragoon ai reaches new lows: wanders into wrong game
Khalum
Profile Joined September 2010
Austria831 Posts
February 03 2015 10:42 GMT
#11511
On February 03 2015 15:37 memcpy wrote:
Show nested quote +
On February 03 2015 15:03 meatpudding wrote:
Are you just trying to copy the contents of one array to another? Sounds like a job for memcpy.


You called?


Ahahahaha !!

[I have nothing else to contribute at this time]
Cynry
Profile Blog Joined August 2010
810 Posts
February 03 2015 11:21 GMT
#11512
On February 03 2015 15:03 meatpudding wrote:
Are you just trying to copy the contents of one array to another? Sounds like a job for memcpy.

res = malloc(sizeof(void*) * new_size);
memset(res, 0, sizeof(void*) * new_size);
if (new_size < old_size)
memcpy(res, old_tab, sizeof(void*) * new_size);
else
memcpy(res, old_tab, sizeof(void*) * old_size);
free(old_tab);
return res;

Not quite. Let's say I have an array containing 5 arrays of int. I want to expand it to 6 arrays, so I figured I have to "realloc" it. That's the part I need for my assignment, but not what I was asking about. I'm wondering if one could write a function that would work whatever the type of array of array is, int, char...

C wiki page says you can't have an array of void, so I thought this was not an option, hence why I used a macro TAB_TYPE (for which ARRAY_TYPE would be a better name I guess) that the user defines and that lets the function knows what type of data it's working with.

Dunno if that's clear...
Khalum
Profile Joined September 2010
Austria831 Posts
Last Edited: 2015-02-03 14:17:03
February 03 2015 14:15 GMT
#11513
I haven't done this in a while, but I'm curious.

My guess is: If you're copying the pointers and not the actual content, the type should not matter. Since you're basically only copying addresses.

[edit] But if you resize the array to be smaller than the previous one, you'll have leaks?
ZenithM
Profile Joined February 2011
France15952 Posts
February 03 2015 14:26 GMT
#11514
On February 03 2015 15:37 memcpy wrote:
Show nested quote +
On February 03 2015 15:03 meatpudding wrote:
Are you just trying to copy the contents of one array to another? Sounds like a job for memcpy.


You called?

That's pretty funny ;D
Manit0u
Profile Blog Joined August 2004
Poland17423 Posts
Last Edited: 2015-02-03 16:31:14
February 03 2015 16:27 GMT
#11515
On February 03 2015 20:21 Cynry wrote:
Show nested quote +
On February 03 2015 15:03 meatpudding wrote:
Are you just trying to copy the contents of one array to another? Sounds like a job for memcpy.

res = malloc(sizeof(void*) * new_size);
memset(res, 0, sizeof(void*) * new_size);
if (new_size < old_size)
memcpy(res, old_tab, sizeof(void*) * new_size);
else
memcpy(res, old_tab, sizeof(void*) * old_size);
free(old_tab);
return res;

Not quite. Let's say I have an array containing 5 arrays of int. I want to expand it to 6 arrays, so I figured I have to "realloc" it. That's the part I need for my assignment, but not what I was asking about. I'm wondering if one could write a function that would work whatever the type of array of array is, int, char...

C wiki page says you can't have an array of void, so I thought this was not an option, hence why I used a macro TAB_TYPE (for which ARRAY_TYPE would be a better name I guess) that the user defines and that lets the function knows what type of data it's working with.

Dunno if that's clear...


Perhaps you could use structs instead of arrays?

This might (or might not) help you. I don't know, I don't remember, was a long time ago when I've written it (or anything else in C) and what I'm showing here might even be an older (non-working) version of it (but who knows? Maybe you'll get lucky):


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct DiceConfiguration
{
int DiceNumber;
int DiceType;
};

struct DiceThrowsResult
{
int DiceNumber;
int Sum;
int ThrowsResults[];
};

struct DiceConfiguration* GetConfiguration();
struct DiceThrowsResult* ThrowDices(struct DiceConfiguration *config);
void GenerateRandomSeed(void);
int ThrowDice(struct DiceConfiguration *config);
void PrintDiceThrowsResult(struct DiceThrowsResult *result);
void ReleaseMemory(struct DiceConfiguration *config, struct DiceThrowsResult *result);

int main(void)
{
struct DiceConfiguration *config;
struct DiceThrowsResult *result;

GenerateRandomSeed();

config = GetConfiguration();

while(config != NULL)
{
result = ThrowDices(config);

PrintDiceThrowsResult(result);

ReleaseMemory(config, result);

config = GetConfiguration();
}

return 0;
}

struct DiceConfiguration* GetConfiguration()
{
int diceNumber = 0;
int diceType = 0;

printf("Enter the number of dice to throw (0 to quit): ");
scanf("%d", &diceNumber);

if(diceNumber == 0)
{
return NULL;
}
else
{
printf("Enter dice type (sides): ");
scanf("%d", &diceType);

if(diceType < 2)
{
printf("Dice can't have less than 2 sides!\n");

return NULL;
}
else
{
struct DiceConfiguration *config;
config = malloc(sizeof(struct DiceConfiguration));

config->DiceNumber = diceNumber;
config->DiceType = diceType;

return config;
}
}
}

struct DiceThrowsResult* ThrowDices(struct DiceConfiguration *config)
{
struct DiceThrowsResult *result;
result = malloc(sizeof(struct DiceThrowsResult) + config->DiceNumber * sizeof(int));
result->DiceNumber = config->DiceNumber;

for (int i = 0; i < config->DiceNumber; i += 1)
{
result->ThrowsResults[i] = ThrowDice(config);
result->Sum += result->ThrowsResults[i];
}

return result;
}

void GenerateRandomSeed(void)
{
srand((unsigned int) time(0));
}

int ThrowDice(struct DiceConfiguration *config)
{
return rand() % config->DiceType + 1;
}

void PrintDiceThrowsResult(struct DiceThrowsResult *result)
{

for (int i = 0; i < result->DiceNumber; i += 1)
{
printf("%d ", result->ThrowsResults[i]);
}

printf("Sum: %d\n", result->Sum);
}

void ReleaseMemory(struct DiceConfiguration *config, struct DiceThrowsResult *result)
{
if (config != NULL)
{
free(config);
config = NULL;
}

if (result != NULL)
{
free(result);
result = NULL;
}
}
Time is precious. Waste it wisely.
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2015-02-03 18:37:24
February 03 2015 18:36 GMT
#11516
I just learned today that C++ does not have a 'switch' for strings. And maybe even enums? That's mediocre. :/
tofucake
Profile Blog Joined October 2009
Hyrule19151 Posts
February 03 2015 18:36 GMT
#11517
C++ doesn't have strings. It has a string class.
Liquipediaasante sana squash banana
Khalum
Profile Joined September 2010
Austria831 Posts
Last Edited: 2015-02-03 18:38:12
February 03 2015 18:38 GMT
#11518
On February 04 2015 03:36 darkness wrote:
I just learned today that C++ does not have a 'switch' for strings. And maybe even enums? That's mediocre. :/

switch for enums works.
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
February 03 2015 18:38 GMT
#11519
On February 04 2015 03:36 tofucake wrote:
C++ doesn't have strings. It has a string class.


So yes, that's still a string. I don't care if it was even required to do silly stuff like:


switch (string.c_str())


The point is it doesn't work, and C#/Java both have it.
RoyGBiv_13
Profile Blog Joined August 2010
United States1275 Posts
Last Edited: 2015-02-03 19:33:11
February 03 2015 19:28 GMT
#11520
On February 04 2015 03:38 darkness wrote:
Show nested quote +
On February 04 2015 03:36 tofucake wrote:
C++ doesn't have strings. It has a string class.


So yes, that's still a string. I don't care if it was even required to do silly stuff like:


switch (string.c_str())


The point is it doesn't work, and C#/Java both have it.


Typically it's done based on switching on the first character then matching strings from there.

Switch statements are compiled into jump tables, which are the result of comparing a register to an immediate value, and branching if they match. The jump table is stored in the .text section next to the comparisons, and the branching executes incredibly fast as many CPUs have instructions just for this execution structure.

The way it works in C# and Java is to compile it into a lengthy if-elseif type branching statement, so instead of switch having a different meaning in the code, its just syntactic sugar. Not that syntactic sugar isn't nice...
Any sufficiently advanced technology is indistinguishable from magic
Prev 1 574 575 576 577 578 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 81
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
qojqva3452
420jenkins277
BananaSlamJamma171
XcaliburYe148
League of Legends
rGuardiaN20
Counter-Strike
fl0m572
byalli525
Other Games
FrodaN1033
Beastyqt715
ceh9575
KnowMe280
Lowko263
Sick262
Hui .186
Liquid`VortiX147
Mew2King100
ArmadaUGS88
QueenE50
Trikslyr46
Organizations
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 18 non-featured ]
StarCraft 2
• kabyraGe 101
• Reevou 1
• IndyKCrew
• LaughNgamezSOOP
• sooper7s
• AfreecaTV YouTube
• Migwel
• intothetv
• Kozan
StarCraft: Brood War
• Michael_bg 8
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• WagamamaTV218
League of Legends
• Nemesis3622
• TFBlade947
• imaqtpie259
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.