• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 05:02
CEST 11:02
KST 18:02
  • 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
[ASL22] Ro24 Preview: Summer's End3Serral wins HomeStory Cup 2915Serral wins Maestros of the Game 243ByuL, and the Limitations of Standard Play3Team Liquid Map Contest #22: Results and Winners7
Community News
GSTL Returns in 2026!42Weekly Cups (Aug 3-9): Protoss get shut out7RSL goes to London! 2026 Offline Finals Nov 21-2212Weekly Cups (July 27-Aug 2): SHIN's big week0SC4ALL II: Brood War - $2500 - Dec 5-611
StarCraft 2
General
GSTL Returns in 2026! Balance hotfix patch 5.0.16b (July 16) SC4ALL: II Talent Announcement! Protoss AoE skill expression Weekly Cups (Aug 3-9): Protoss get shut out
Tourneys
WardiTV Mondays PIG STY FESTIVAL 8.0! (13 - 23 August) 2026 KungFu Cup Announcement Sparkling Tuna Cup - Weekly Open Tournament 2026 GSTL Announcement
Strategy
[G] Having the right mentality to improve
Custom Maps
Nexus Wars 2021 GUIDE [M] (2) Industrial Park
External Content
The PondCast: SC2 News & Results Mutation # 539 Thunder Dome Mutation # 538 Media Blackout Mutation # 537 Hostile Territory
Brood War
General
[ASL22] Ro24 Preview: Summer's End Rush's Odyssey Controversy BW General Discussion StarCraft 64 is coming to Philly at SC4ALL II Best Games Kespa era
Tourneys
[ASL22] Ro24 Group A CSLAN 4 is Coming! ASL Season 22 LIVESTREAM with English Commentary [Megathread] Daily Proleagues
Strategy
Odyssey Mineral Stack Saturation Fighting Spirit mining rates Any training maps people recommend? Simple Questions, Simple Answers
Other Games
General Games
Nintendo Switch Thread General RTS Discussion Thread Anyone here play Quakeworld back in the day? Stormgate/Frost Giant Megathread EVE Corporation
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
[TL LoL EUW IHs] Teemo shall perish TSM pausing esports and CLG Dead
Heroes of the Storm
Heroes of the Storm 2.0
Hearthstone
Deck construction bug
TL Mafia
TL Mafia Power Rank TL Mafia Community Thread NeO.D_StephenKing vs This Guy From 1 Million Dance
Community
General
Artificial Intelligence Thread US Politics Mega-thread Russo-Ukrainian War Thread European Politico-economics QA Mega-thread The Letting Off Steam Thread
Fan Clubs
MarineLorD Fan Club The ShoWTimE Fan Club The herO Fan Club!
Media & Entertainment
Movie Discussion! Anime Discussion Thread Series you have seen recently... [Req][Books] Good Fantasy/SciFi books
Sports
MLB/Baseball 2023 Football (Soccer) Thread TeamLiquid Health and Fitness Initiative For 2023 NBA General Discussion Formula 1 Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread Simple Questions Simple Answers FPS when play League Of Legend on laptop
TL Community
The Automated Ban List Northern Ireland Global Starcraft
Blogs
LOCKPICKING NOOB
LUCKY_NOOB
Chinese Gen Z: Between Dream…
TrAiDoS
Cathedral Of CS And NY pizza a…
FuDDx
Please support my new stand…
Peanutsc
Hello guys!
LIN1s
Customize Sidebar...

Website Feedback

Closed Threads



Active: 11606 users

The Big Programming Thread - Page 642

Forum Index > General Forum
Post a Reply
Prev 1 640 641 642 643 644 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 101379
Profile Blog Joined August 2010
4849 Posts
June 15 2015 14:23 GMT
#12821
On June 15 2015 22:40 BisuDagger wrote:
Show nested quote +
On June 15 2015 22:33 Acrofales wrote:
On June 15 2015 22:11 BisuDagger wrote:
On June 15 2015 22:07 inn5013orecl wrote:
On June 15 2015 21:05 BisuDagger wrote:
Okay this is a pretty odd question and either there is no answer or it is stupidly obvious. Reply in terms of C++, C# please:

If I have a function OnMouseDown() and it calls a function IsUISelected() such that it looks like this:


void OnMouseDown()
{
IsUISelected();
}


If IsUISelected resolves to true in it's function I need to break out out of the OnMouseDown() function instead of continuing.

I'm trying to simply this statement. Simplying the state is purely just a curiosity not a necessity.

void OnMouseDown()
{
if (GlobalVariables.UISelected())
{
return;
}

}






Pretty sure that's as simple as you're going to get. Return pass control back to the caller of OnMouseDown(), effectively exiting out of the function. In this case, works exactly like a break statement in a loop.


void OnMouseDown()
{
if(IsUISelected()) // IsUISelected() value returned
return;
}

That's what I thought. I just have to use this check in so many damn places I was trying to save white space. Oh well lol.

edit: The reason I keep the brackets is to match a coding standard of another companies programmers I have to work with from time to time.

Restructure your program to require less of these checks? Or suck it up. Honestly, I can't think why you wouldn't just put all of that check and return statement on a single line (including {}s). Hell, you can add a line of commentary above it and still save 2 lines if you do it that way.

//Break out of mouse handler if UI is selected
if(IsUISelected()) { return; }


is imho just as clear as the 4-line version (although the line of commentary is not necessary at all, the line is pretty self-explanatory. Better commentary would be a reminder why breaking out of the mouse handler is the proper functioning of the program).

As I mentioned in my edit. For at least this project I'm matching a coding standard for another company. I'm the lead programmer within my company and I 100% agree with you, but the other companies guidelines are "always use brackets for readability" and "Don't comment code unless absolutely necessary to reduce clutter.". I've done really well in my profession by following the technical outlines of a company (even if you disagree) instead of fighting the system.


Yup, no matter how annoying they are, it's important to follow the set coding guidelines. In every company, there are some very annoying rules, e.g. in my last company we had to use an hungarian naming scheme where every object was $objSomething, which was really useless because almost everything was an object, you just didn't know what kind of object. However, in the end it's better to have everyone write code in the same way, even if it has some weird quirks, than have wildly different styles in the same code base. You can slowly change the style guidelines to make exceptions for guard clauses when you have enough influence and support, but until then it's always best to stick to what exists.

Joel Spolsky put it nicely in his article about hungarian notation


When you start out as a beginning programmer or you try to read code in a new language it all looks equally inscrutable. Until you understand the programming language itself you can’t even see obvious syntactic errors.

During the first phase of learning, you start to recognize the things that we usually refer to as “coding style.” So you start to notice code that doesn’t conform to indentation standards and Oddly-Capitalized variables.

It’s at this point you typically say, “Blistering Barnacles, we’ve got to get some consistent coding conventions around here!” and you spend the next day writing up coding conventions for your team and the next six days arguing about the One True Brace Style and the next three weeks rewriting old code to conform to the One True Brace Style until a manager catches you and screams at you for wasting time on something that can never make money, and you decide that it’s not really a bad thing to only reformat code when you revisit it, so you have about half of a True Brace Style and pretty soon you forget all about that and then you can start obsessing about something else irrelevant to making money like replacing one kind of string class with another kind of string class.


The code style doesn't really matter as long as it's good, readable code that works, so you might as well stick to the given rules and not waste your time arguing about braces or no braces.
Manit0u
Profile Blog Joined August 2004
Poland17831 Posts
Last Edited: 2015-06-16 08:00:00
June 15 2015 21:06 GMT
#12822
Does anyone here have any experience with QNX embedded systems?
Time is precious. Waste it wisely.
Isualin
Profile Joined March 2011
Germany1903 Posts
June 18 2015 06:44 GMT
#12823
Look at this gem. Maybe we will see an enterprise java application generation simulator someday.
| INnoVation | The literal god TY | ByuNjwa | LRSL when? |
necrosexy
Profile Joined March 2011
451 Posts
Last Edited: 2015-06-18 06:55:48
June 18 2015 06:55 GMT
#12824
On June 14 2015 15:11 Frolossus wrote:
Show nested quote +
On June 14 2015 14:28 necrosexy wrote:
any recommended resources for learning unix and shell programming?

http://www.tldp.org/LDP/Bash-Beginners-Guide/html/

On June 14 2015 20:11 Nesserev wrote:
Show nested quote +
On June 14 2015 14:28 necrosexy wrote:
any recommended resources for learning unix and shell programming?

'The Linux Command Line' from No Starch Press, combined with the resource that Frolossus linked above,
will be all you ever need.

Btw, No Starch Press has some of the best and most enjoyable books in the business. Check out their website:
http://www.nostarch.com/

Thanks!!
Ropid
Profile Joined March 2009
Germany3557 Posts
Last Edited: 2015-06-18 09:11:17
June 18 2015 09:08 GMT
#12825
@necrosexy:

Also check this guide here out:

http://mywiki.wooledge.org/BashGuide

It has pages that are good to bookmark, for example this here:

http://mywiki.wooledge.org/BashSheet

Depending on what you want to do, one of the pages on that site might be very important to bookmark:

http://mywiki.wooledge.org/Bashism?highlight=(bashism)

The background for what's on that page is: bash is not the default shell on a lot of systems. For Linux, Debian and Ubuntu use something simpler, and what you'll find on for example a router also won't be bash. The BSD's similarly don't use it. So if you write a script targeting the system's /bin/sh, you should be careful to not use features that only work if /bin/sh = bash. How to translate bash to plain sh is what's on that page.
"My goal is to replace my soul with coffee and become immortal."
necrosexy
Profile Joined March 2011
451 Posts
June 19 2015 05:15 GMT
#12826
On June 18 2015 18:08 Ropid wrote:
@necrosexy:

Also check this guide here out:

http://mywiki.wooledge.org/BashGuide

It has pages that are good to bookmark, for example this here:

http://mywiki.wooledge.org/BashSheet

Depending on what you want to do, one of the pages on that site might be very important to bookmark:

http://mywiki.wooledge.org/Bashism?highlight=(bashism)

The background for what's on that page is: bash is not the default shell on a lot of systems. For Linux, Debian and Ubuntu use something simpler, and what you'll find on for example a router also won't be bash. The BSD's similarly don't use it. So if you write a script targeting the system's /bin/sh, you should be careful to not use features that only work if /bin/sh = bash. How to translate bash to plain sh is what's on that page.

i see, thanks!
Manit0u
Profile Blog Joined August 2004
Poland17831 Posts
Last Edited: 2015-06-19 22:28:03
June 19 2015 21:24 GMT
#12827
Guys, how do you structure C projects? I've never written anything complex in C before but I plan on changing that and was wondering if there is any kind of "standard" folder structure or anything like that.

I was thinking about something along the lines of this:


/bin
-- executables go here
/config
-- makefiles, misc config files
/data
-- sqlite databases, images etc.
/scripts
-- Lua and other stuff
/src
/ext
-- external libraries (other people's stuff and such)
/lib
-- actual c code goes here
/sys
-- headers go here


Would that be any good? Or should I perhaps keep config, data and scripts inside the src folder?

Edit:

And while we're at it, could anyone help me out a bit with the basic stuff?


#include <stdio.h>
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y));
#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y));

// prototypes
int factorial(int n);
int* fill_range(int x, int size);
int* fill_range_reverse(int x, int size);
int* irange(int x, int y);

int main()
{
int fac = factorial(5);
int* range1 = irange(1, 10);
int* range2 = irange(10, 1);
int i = 0;

printf("Factorial of 5 is: %d\n", fac);

for (i = 0; i < 10; ++i)
{
printf("Normal range %d: %d\n", i, range1[i]);
}

for (i = 0; i < 10; ++i)
{
printf("Reverse range %d: %d\n", i, range2[i]);
}

return 0;
}

int factorial(int n)
{
if (n < 2)
return 1;

return (n * factorial(n - 1));
}

int* fill_range(int x, int size)
{
int range[size];
int i = 0;

while (i < size)
{
range[i] = x;

i += 1;
x += 1;
}

return range;
}

int* fill_range_reverse(int x, int size)
{
int range[size];
int i = 0;

while (i < size)
{
range[i] = x;

i += 1;
x -= 1;
}

return range;
}

int* irange(int x, int y)
{
int size = MAX(x, y) - MIN(x, y) + 1;

if (x > y)
return fill_range_reverse(x, size);

return fill_range(x, size);
}


                  
main.c: In function 'fill_range':
main.c:54:5: warning: function returns address of local variable [-Wreturn-local-addr]
return range;
^
main.c: In function fill_range_reverse':
main.c:70:5: warning: function returns address of local variable [-Wreturn-local-addr]
return range;
^


I must say that I'm at a loss here... Haven't touched C for years and I pretty much suck hard at it now... I'm too spoiled with new toys to remember exactly how all the static and const stuff worked in C, should I malloc the range initializers first?

Edit2:

Nevermind, sometimes I'm just too dumb...

+ Show Spoiler [fixed code] +


#include <stdio.h>
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y));
#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y));

// prototypes
int factorial(int n);
int* fill_range(int x, int size, int* range);
int* fill_range_reverse(int x, int size, int* range);
int* irange(int x, int y);

int main()
{
int fac = factorial(5);
int* range1 = irange(1, 10);
int* range2 = irange(10, 1);
int i = 0;

printf("Factorial of 5 is: %d\n", fac);

for (i = 0; i < 10; ++i)
{
printf("Normal range %d: %d\n", i, range1[i]);
}

for (i = 0; i < 10; ++i)
{
printf("Reverse range %d: %d\n", i, range2[i]);
}

return 0;
}

int factorial(int n)
{
if (n < 2)
return 1;

return (n * factorial(n - 1));
}

int* fill_range(int x, int size, int* range)
{
int i = 0;

while (i < size)
{
range[i] = x;

i += 1;
x += 1;
}

return range;
}

int* fill_range_reverse(int x, int size, int* range)
{
int i = 0;

while (i < size)
{
range[i] = x;

i += 1;
x -= 1;
}

return range;
}

int* irange(int x, int y)
{
int size = MAX(x, y) - MIN(x, y) + 1;
int range[size];

if (x > y)
return fill_range_reverse(x, size, range);

return fill_range(x, size, range);
}

Time is precious. Waste it wisely.
RoyGBiv_13
Profile Blog Joined August 2010
United States1275 Posts
Last Edited: 2015-06-19 22:32:06
June 19 2015 22:21 GMT
#12828
On June 20 2015 06:24 Manit0u wrote:
Guys, how do you structure C projects? I've never written anything complex in C before but I plan on changing that and was wondering if there is any kind of "standard" folder structure or anything like that.

I was thinking about something along the lines of this:


/bin
-- executables go here
/config
-- makefiles, misc config files
/data
-- sqlite databases, images etc.
/scripts
-- Lua and other stuff
/src
/ext
-- external libraries (other people's stuff and such)
/lib
-- actual c code goes here
/sys
-- headers go here


Would that be any good? Or should I perhaps keep config, data and scripts inside the src folder?


I've got a ton of experience with QNX, though it's been a few years I actually did any projects with it since now I work for a competitor.

That directory structure looks fine, I might put your actual C code in /src, and put external libraries in /lib and headers in /src/include or just /include.
Your Makefile and README should go in the root directory, not /config, but that doesn't mean you shouldn't have a folder for storing random configuration files in it as well, which may include other Makefiles being imported with various compiler options being defined.

Depending on what your project's end output file will be, I prefer to place my project folder within the same folder as the examples for the OS. The benefit is that a lot of your code is going to be copied over from those examples, and that you can put the entire OS in version control along with your project (which you should do, in case you accidentally change something you shouldn't have).

+ Show Spoiler +
If you want to open source your project, don't do this if you had to have a license to decrypt the OS libraries in the first place to install them. If the OS you are using is already open source, consider branching it instead of making a new repository


You'll also need a obj/ folder for storing intermediate object files.

For those who missed the change in his code, the warning was because he stored an array on the stack, then returned it, so if the calling function tried to use that array, it could point to completely different data stored on the stack later.
Any sufficiently advanced technology is indistinguishable from magic
Nesserev
Profile Blog Joined January 2011
Belgium2760 Posts
Last Edited: 2015-06-19 22:35:08
June 19 2015 22:33 GMT
#12829
--- Nuked ---
RoyGBiv_13
Profile Blog Joined August 2010
United States1275 Posts
June 19 2015 22:35 GMT
#12830
On June 20 2015 07:33 Nesserev wrote:
Well, you're supposed to dynamically allocate the memory for the array.


// creates a local array on the stack
int range[size];

...

return range; // range is "destroyed" at the end of this function :(



// pointer used to point to array
int* d_range;

// create dynamically allocated array on heap
d_range = (int *)malloc(sizeof(int) * size);

...

return d_range;


Who is going to free() that malloc()?

Manit0u's fixed implementation allows the user of the function to allocate the memory where they want, instead of forcing it onto the heap.
Any sufficiently advanced technology is indistinguishable from magic
Manit0u
Profile Blog Joined August 2004
Poland17831 Posts
Last Edited: 2015-06-19 22:53:06
June 19 2015 22:35 GMT
#12831
On June 20 2015 07:21 RoyGBiv_13 wrote:
Show nested quote +
On June 20 2015 06:24 Manit0u wrote:
Guys, how do you structure C projects? I've never written anything complex in C before but I plan on changing that and was wondering if there is any kind of "standard" folder structure or anything like that.

I was thinking about something along the lines of this:


/bin
-- executables go here
/config
-- makefiles, misc config files
/data
-- sqlite databases, images etc.
/scripts
-- Lua and other stuff
/src
/ext
-- external libraries (other people's stuff and such)
/lib
-- actual c code goes here
/sys
-- headers go here


Would that be any good? Or should I perhaps keep config, data and scripts inside the src folder?


I've got a ton of experience with QNX, though it's been a few years I actually did any projects with it since now I work for a competitor.

That directory structure looks fine, I might put your actual C code in /src, and put external libraries in /lib and headers in /src/include or just /include.
Your Makefile and README should go in the root directory, not /config, but that doesn't mean you shouldn't have a folder for storing random configuration files in it as well, which may include other Makefiles being imported with various compiler options being defined.

Depending on what your project's end output file will be, I prefer to place my project folder within the same folder as the examples for the OS. The benefit is that a lot of your code is going to be copied over from those examples, and that you can put the entire OS in version control along with your project (which you should do, in case you accidentally change something you shouldn't have).

+ Show Spoiler +
If you want to open source your project, don't do this if you had to have a license to decrypt the OS libraries in the first place to install them. If the OS you are using is already open source, consider branching it instead of making a new repository


You'll also need a obj/ folder for storing intermediate object files.


With the structure I wasn't asking about QNX specifically. I'm just toying around with ANSI C in my leisure time.

I was asking about QNX because a friend of mine asked me for help and I simply can't refuse stuff that involves learning some new code. The task at hand was relatively simple but I've found QNX's online documentation to be pretty lacking (as in, requiring you to have really good knowledge of the system to understand it, at which point you probably don't need it). I also hat it when they always seem to do for( ; ; ) instead of while(true) or something. But I digress...

The problem he had to solve was the password/pin thingie. Where you give the user a message, wait 5 seconds for input and reset ad infinitum. The task specifically asked to use the alarm() function. While making a simple loop and doing alarm(5) inside of it is easy, actually handling the alarm is not. From what I read in the docs, when the time for the alarm elapses, it sends the SIGALRM which kills the current process, thus exiting the loop and all. I've read that you should handle this signal with sigaction() or somesuch but I couldn't really find a way to make sigaction() resume/restart the loop.

Could you shed some light on the matter?

On June 20 2015 07:35 RoyGBiv_13 wrote:
Manit0u's fixed implementation allows the user of the function to allocate the memory where they want, instead of forcing it onto the heap.


Is it a bad thing?

Edit:

Also, the code purist in my heart tells me to refactor the code, remove fill_range_reverse and simply pass another argument to the fill range function (a boolean) that controls incrementation/decrementation inside of the loop to remove code duplication.

+ Show Spoiler [like so] +


#include <stdio.h>
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y));
#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y));

// prototypes
int factorial(int n);
int sum(int n);
int check_increment(int x, int reverse)
int* fill_range(int x, int size, int* range, int reverse);
int* irange(int x, int y);

int main()
{
int fac = factorial(5);
int* range1 = irange(1, 10);
int* range2 = irange(10, 1);
int i = 0;

printf("Factorial of 5 is: %d\n", fac);

for (i = 0; i < 10; ++i)
{
printf("Normal range %d: %d\n", i, range1[i]);
}

for (i = 0; i < 10; ++i)
{
printf("Reverse range %d: %d\n", i, range2[i]);
}

return 0;
}

int factorial(int n)
{
if (n < 2)
return 1;

return (n * factorial(n - 1));
}

int sum(int n)
{
return ((n * (n + 1)) / 2);
}

int check_increment(int x, int reverse)
{
return reverse ? x -= 1 : x += 1;
}

int* fill_range(int x, int size, int* range, int reverse)
{
int i = 0;

while (i < size)
{
range[i] = x;

i += 1;
x = check_increment(x, reverse);
}

return range;
}


int* irange(int x, int y)
{
int size = MAX(x, y) - MIN(x, y) + 1;
int range[size];

return x > ? fill_range(x, size, range, 1) : fill_range(x, size, range, 0);
}

Time is precious. Waste it wisely.
Nesserev
Profile Blog Joined January 2011
Belgium2760 Posts
Last Edited: 2015-06-19 22:39:31
June 19 2015 22:38 GMT
#12832
--- Nuked ---
RoyGBiv_13
Profile Blog Joined August 2010
United States1275 Posts
Last Edited: 2015-06-19 22:47:35
June 19 2015 22:46 GMT
#12833
On June 20 2015 07:35 Manit0u wrote:
Show nested quote +
On June 20 2015 07:21 RoyGBiv_13 wrote:
On June 20 2015 06:24 Manit0u wrote:
Guys, how do you structure C projects? I've never written anything complex in C before but I plan on changing that and was wondering if there is any kind of "standard" folder structure or anything like that.

I was thinking about something along the lines of this:


/bin
-- executables go here
/config
-- makefiles, misc config files
/data
-- sqlite databases, images etc.
/scripts
-- Lua and other stuff
/src
/ext
-- external libraries (other people's stuff and such)
/lib
-- actual c code goes here
/sys
-- headers go here


Would that be any good? Or should I perhaps keep config, data and scripts inside the src folder?


I've got a ton of experience with QNX, though it's been a few years I actually did any projects with it since now I work for a competitor.

That directory structure looks fine, I might put your actual C code in /src, and put external libraries in /lib and headers in /src/include or just /include.
Your Makefile and README should go in the root directory, not /config, but that doesn't mean you shouldn't have a folder for storing random configuration files in it as well, which may include other Makefiles being imported with various compiler options being defined.

Depending on what your project's end output file will be, I prefer to place my project folder within the same folder as the examples for the OS. The benefit is that a lot of your code is going to be copied over from those examples, and that you can put the entire OS in version control along with your project (which you should do, in case you accidentally change something you shouldn't have).

+ Show Spoiler +
If you want to open source your project, don't do this if you had to have a license to decrypt the OS libraries in the first place to install them. If the OS you are using is already open source, consider branching it instead of making a new repository


You'll also need a obj/ folder for storing intermediate object files.


With the structure I wasn't asking about QNX specifically. I'm just toying around with ANSI C in my leisure time.

I was asking about QNX because a friend of mine asked me for help and I simply can't refuse stuff that involves learning some new code. The task at hand was relatively simple but I've found QNX's online documentation to be pretty lacking (as in, requiring you to have really good knowledge of the system to understand it, at which point you probably don't need it). I also hat it when they always seem to do for(; instead of while(true) or something. But I digress...

The problem he had to solve was the password/pin thingie. Where you give the user a message, wait 5 seconds for input and reset ad infinitum. The task specifically asked to use the alarm() function. While making a simple loop and doing alarm(5) inside of it is easy, actually handling the alarm is not. From what I read in the docs, when the time for the alarm elapses, it sends the SIGALRM which kills the current process, thus exiting the loop and all. I've read that you should handle this signal with sigaction() or somesuch but I couldn't really find a way to make sigaction() resume/restart the loop.

Could you shed some light on the matter?


Hmm, this is actually closer to pure POSIX than anything QNX related, so you're in luck because the documentation for that is actually really well filled out (but never in one place).

Signals won't actually kill the process, it will continue to run after handling the signal, so if you're already in an infinite spin loop, you won't get out of it. Instead, have the signal modify a global variable that the spin loop is based off of (instead of while(true), use while(spinning), where spinning is volatile) http://stackoverflow.com/questions/14042647/using-sigaction-and-alarm-to-break-from-infinite-loop


Any sufficiently advanced technology is indistinguishable from magic
Manit0u
Profile Blog Joined August 2004
Poland17831 Posts
June 19 2015 22:56 GMT
#12834
Thanks, that helps a ton.
Time is precious. Waste it wisely.
iaretehnoob
Profile Joined June 2004
Sweden741 Posts
June 19 2015 23:10 GMT
#12835
That fixed code still has the exact same bug btw, just expertly hidden from the compiler.
Manit0u
Profile Blog Joined August 2004
Poland17831 Posts
June 19 2015 23:20 GMT
#12836
On June 20 2015 08:10 iaretehnoob wrote:
That fixed code still has the exact same bug btw, just expertly hidden from the compiler.


Which bug?
Time is precious. Waste it wisely.
Nesserev
Profile Blog Joined January 2011
Belgium2760 Posts
June 19 2015 23:40 GMT
#12837
--- Nuked ---
Manit0u
Profile Blog Joined August 2004
Poland17831 Posts
Last Edited: 2015-06-19 23:57:45
June 19 2015 23:42 GMT
#12838
And how should I remedy this?

Edit:

Nevermind. I see my error. I'll think how to solve this.

Edit2:

Should I declare range as global variable, malloc it inside the irange function and free it after use? I was kind of hoping I could avoid declaring globals or assigning array size before calling the irange
Time is precious. Waste it wisely.
Ropid
Profile Joined March 2009
Germany3557 Posts
June 20 2015 10:30 GMT
#12839
You could use malloc() in irange() to create your int[] range on the heap instead of the stack, then perhaps rename irange() to alloc_irange() to make it clear that the user of this function has to free() the buffer in the future.

Or you could put the malloc() in main(), so that it's in the same block of your code where you'll also have to do the free(). In that case you might want to have some sort of info function that calculates the required size to use with malloc() for certain x, y values.
"My goal is to replace my soul with coffee and become immortal."
Manit0u
Profile Blog Joined August 2004
Poland17831 Posts
Last Edited: 2015-06-20 11:07:28
June 20 2015 11:07 GMT
#12840
And what if I put range and other stuff into a struct? Would that be a good idea?

Somehow I completely forgot about structs...
Time is precious. Waste it wisely.
Prev 1 640 641 642 643 644 1032 Next
Please log in or register to reply.
Live Events Refresh
Next event in 58m
[ Submit Event ]
Live Streams
Refresh
StarCraft: Brood War
910 1076
PianO 1006
Killer 698
Larva 402
EffOrt 265
Mind 220
ZerO 137
Sexy 72
ZergMaN 42
sSak 32
[ Show more ]
Mong 31
Bale 29
Sea 28
Noble 22
sorry 16
Terrorterran 11
Counter-Strike
olofmeister1484
zeus98
kRYSTAL_59
Super Smash Bros
Mew2King94
Other Games
summit1g6218
singsing1542
ceh9662
crisheroes185
ZerO(Twitch)6
[ Show 13 non-featured ]
StarCraft 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• Migwel
StarCraft: Brood War
• Light_VIP 17
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• WagamamaTV709
• lizZardDota2294
League of Legends
• Stunt493
• HappyZerGling134
Upcoming Events
Afreeca Starleague
58m
Rush vs Hm
Bisu vs Shuttle
WardiTV Weekly
1h 58m
The Patches Monday
6h 58m
Afreeca Starleague
1d
Sharp vs Shinee
Action vs Shine
GSL
1d 1h
PiGosaur Cup
1d 14h
Replay Cast
1d 23h
Afreeca Starleague
2 days
BeSt vs Paralyze
Jaedong vs Speed
Kung Fu Cup
2 days
Replay Cast
2 days
[ Show More ]
The PondCast
3 days
KCM Race Survival
3 days
Replay Cast
3 days
PiG Sty Festival
4 days
CranKy Ducklings
5 days
PiG Sty Festival
5 days
Sparkling Tuna Cup
6 days
PiG Sty Festival
6 days
Liquipedia Results

Completed

CSLAN 4
CranK Gathers Season 4: BW vs SC2 Team League
Eternal Conflict S2 Finale

Ongoing

KCM Race Survival 2026 Season 3
K-JUNGMAN
ASL Season 22
RSL Revival: Season 6
PiG Sty Festival 8.0
META DYMY #4
Esports World Cup 2026
Esports World Cup 2026: LCQ
BLAST Bounty Summer 2026
BLAST Bounty Summer Qual
Stake Ranked Episode 3
XSE Pro League 2026
IEM Cologne Major 2026

Upcoming

CSL Season 22: Qualifier 1
Escore Tournament S3: W8
CSL Season 22: Qualifier 2
CSL 2026 AUTUMN (S22)
Acropolis #5
Acropolis #5 - TRS
Blizzard Classic Cup 2026
HSC XXX
SC4ALL II: StarCraft II
Kung Fu Cup 2026 Grand Finals
RSL Offline Finals
Big Dog Cup 2026 Div 1
Stake Ranked Episode 5
PGL Masters Bucharest 2026
Thunderpick World Champ. '26
ESL Pro League Season 24
Stake Ranked Episode 4
1win Private Club #1
Logitech G Connect 2026
SL StarSeries Fall 2026
FISSURE Playground #5
BLAST Open Fall 2026
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.