Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 555 556 [557] 558 559 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 882153 times)

Malus

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8340 on: October 28, 2015, 11:42:36 pm »

The idiomatic "C" way of doing things would be to pass in the address of memory you allocated before calling the function, store the result in that memory, then return an error code (or 0 for success).
Something like this:
Code: [Select]
#include <stdio.h>
#include <stdlib.h>

int doStuff(int* data)
{
    data[0] = 0;
    data[1] = 1;
    data[2] = 2;
    return 0; // woohoo
}

int main(void)
{
    int* data = malloc(sizeof(int)*3);

    if(!doStuff(data))
        return 0; // success!
    else
        return -1; // something went wrong
}

Getting more comfortable with direct memory allocations is never a bad thing, anyway.
« Last Edit: October 28, 2015, 11:45:39 pm by Malus »
Logged

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #8341 on: October 29, 2015, 03:30:47 am »

The lack of a corresponding free for that malloc makes me twitch :P
Logged

Malus

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8342 on: October 29, 2015, 03:36:04 am »

The lack of a corresponding free for that malloc makes me twitch :P
Hey, the OS will take care of it! Have to leave some work for the operating system, right?
Logged

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8343 on: October 29, 2015, 01:08:46 pm »

damn operating systems taking our jerbs
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #8344 on: October 29, 2015, 05:01:33 pm »

Finally getting to professionally build a WebAPI project after a year of doing pure client-side work (It's for the client-side work, but still).

Oh pure-problem of Restful APIs, why did I ever leave you? This is so much nicer than constantly having to worry about "Yeah, but will it work in Internet Explorer?" :)
Logged

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #8345 on: October 29, 2015, 06:52:54 pm »

"Yeah, but will it work in Internet Explorer?"
If someone is using IE then they don't deserve to have your code work.

...

Lately I've been digging into C# and one of the biggest hurdles I'm having in moving from Python is that I can't arbitrarily increase the length of arrays. Is there a way around this or am I just going to have to -gulp- learn proper programming practices?
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Willfor

  • Bay Watcher
  • The great magmaman adventurer. I do it for hugs.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8346 on: October 29, 2015, 06:56:10 pm »

In System.Collections.Generic you have List<T> where T is any Object you can find in C#

Used thusly:

Code: [Select]
List<Int32> myList = new List<Int32>();
myList.Add(3);

It expands as necessary.
Logged
In the wells of livestock vans with shells and garden sands /
Iron mixed with oxygen as per the laws of chemistry and chance /
A shape was roughly human, it was only roughly human /
Apparition eyes / Apparition eyes / Knock, apparition, knock / Eyes, apparition eyes /

Dutrius

  • Bay Watcher
  • No longer extremely unavailable!
    • View Profile
    • Arcanus Technica
Re: if self.isCoder(): post() #Programming Thread
« Reply #8347 on: October 29, 2015, 06:59:29 pm »

There's also a resize method under the Array type.

Code: [Select]
//Example.
Array.Resize<arrayType>(ref myArray, newSize);
Logged
No longer extremely unavailable!
Sig text
ArcTech: Incursus. On hold indefinitely.

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8348 on: October 29, 2015, 08:22:22 pm »

Finally getting to professionally build a WebAPI project after a year of doing pure client-side work (It's for the client-side work, but still).

Oh pure-problem of Restful APIs, why did I ever leave you? This is so much nicer than constantly having to worry about "Yeah, but will it work in Internet Explorer?" :)

Oh, yes, the joy of IE 8 and older browsers.  One of the best parts of the work I do right now is that it's all corporate intranet, so we only have to get things to work in Chrome.  That speeds up development quite a lot.

Then, of course, half of the upper management decide that, hey, Macs are cool.  Why don't things work in Safari?

Ugh.

It's still not as bad as the public facing websites we have to build and maintain on occasion.  IE 6 finally dropped off the radar in usage, but IE 8 is still frustratingly common.
Logged
Through pain, I find wisdom.

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8349 on: October 29, 2015, 08:57:30 pm »

So.  I have this code.
It throws a NullPointerException that I cannot locate the source of.
Any help would be appreciated.
Note: console is a Scanner, studentlist is an ArrayList, both are global variables.
Code: [Select]

    public static void newStudent(){
        String cmd;
        String[] name = new String[2];
        boolean gotName = false;
        int SID = 0;
        boolean gotSID = false;
        ArrayList<Integer> testScores = new ArrayList();
        boolean gotTestScores = false;
        boolean doContinue = true;
        while(true){
            if(!gotName){
                System.out.println("Please input the student's name, first and last in that order.  This system does not support middle names.");
                name[0]=console.next();
                System.out.println(name[0]);
                name[1]=console.next();
                System.out.println(name[1]);
                gotName=true;
            }
            if(!gotSID){
                gotSID = true;
                System.out.println("Please input the student ID number.  Do not prefix it with #.");
                try{
                    cmd = console.next();
                    SID = Integer.parseInt(cmd);
                    System.out.println(SID);
                }
                catch(IllegalArgumentException | NullPointerException ex){
                    gotSID = false;
                    doContinue = false;
                    System.out.println("Please input a number, and only a number.");
                }
            }
            if(!gotTestScores && doContinue){
                gotTestScores = true;
                System.out.println("Please input any test scores.  Input anything that is not a number to finish inputting test scores or skip this input.");
                while(true){
                    if(console.hasNextInt()){
                    cmd = console.next();
                        testScores.add(Integer.parseInt(cmd));
                    }
                    else break;
                }
            }
        if(doContinue){
            if(testScores.isEmpty())testScores.add(0);
            studentlist.add(new Student(name[0],name[1],SID,testScores));
            break;
        }
        doContinue = true;
        }
    }
The behavior of Scanner precludes name[] from being null (and I'm not giving it null input anyway), and testScores specifically has an if statement devoted to making sure it isn't actually null, but the exception is thrown whether I give the portion of the code responsible for testScores input or not.  The NPE is thrown when I do
Code: [Select]
studentlist.add(new Student(...))So...What's actually null here, and how the heck am I supposed to fix it?


E: Stacktrace.
Code: [Select]
Exception in thread "main" java.lang.NullPointerException
at studentmanager.StudentManager.newStudent(StudentManager.java:104)
at studentmanager.StudentManager.main(StudentManager.java:26)
Java Result: 1
« Last Edit: October 29, 2015, 08:59:44 pm by TheBiggerFish »
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8350 on: October 29, 2015, 09:02:38 pm »

debugging 101, break up the offending line into smaller components, see which sub part triggers the crash.

put if statements for each component to see which one is null, and get some output. e.g. are you sure StudentList itself isn't null?
« Last Edit: October 29, 2015, 09:04:55 pm by Reelya »
Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8351 on: October 29, 2015, 09:05:32 pm »

debugging 101, break up the offending line into smaller components, see which sub part triggers the crash.
Thing is, I need to...Oh, or I could just try swapping in hardcoding.  Derp.
Be riiight back.


What the FLYING SKELETAL CARP.
I replaced EVERY SINGLE PARAMETER with hard-coded stuff and it's still throwing a NPE.
Code: [Select]
            //...

            ArrayList<Integer> thisistemporary = new ArrayList();
            thisistemporary.add(1);
            studentlist.add(new Student("first","last",42,thisistemporary));
            break;




E: ...Dang it, it WAS studentlist.  Wasn't properly made an ArrayList.  Thanks!
« Last Edit: October 29, 2015, 09:13:48 pm by TheBiggerFish »
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

jaked122

  • Bay Watcher
  • [PREFSTRING:Lurker tendancies]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8352 on: October 29, 2015, 09:37:42 pm »

Do any of you know whether or not there's any support for including the STL tree structure and using that directly instead?

I've spent the last two days working on writing a self-balancing tree in C++ and have decided that I'd rather rip my own arms off than fix some of the issues that are resisting my attempts to fix them.


I noticed, however, that when I was consulting the STL tree implentation to avoid verifying that my structure made sense, that it essentially composed the basis of the map and set types. The set more directly than the map, but still.


Is there any guarantee that I could make using the underlying implementation? Or should I put the thought of using stl_tree.h out of my head?

jaked122

  • Bay Watcher
  • [PREFSTRING:Lurker tendancies]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8353 on: October 29, 2015, 09:50:23 pm »

Well... It's not in the STL, not the part you're meant to work with, but I spy with my locate utility, a file named stl_tree.h in the include directory. And lo and behold, it's also included in the underlying implementation in the map file.


You aren't meant to play with it, but it is an implementation of a red-black tree, and it defines just enough functionality for me to hang myself from it. So to speak, it's not nearly long enough to actually hang myself with it, even transcribed onto string.

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #8354 on: October 30, 2015, 12:01:50 am »

Well, I just spent most of today trying to implement 2d collision detection RESOLUTION, and I think I severely burnt myself out on it. It seems like there's no middle ground between implementing this stuff yourself and using a super-complex physics library like Box2d that has way more than you need and is awful at actual platformer physics.

After hours reading mathematics, physics, and programming articles about collision resolution, I still can't find a good way to solve that "seam" problem where a bounding box gets stuck on the corner of adjacent rectangles it's colliding with. I even know what causes the problem, yet I'm still no closer to an answer.

Collision resolution isn't that bad when it's tile-based, but I just had to decide I was going to use arbitrary-sized Tiled objects for collision.
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU
Pages: 1 ... 555 556 [557] 558 559 ... 796