• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

soultron

Banned
I'm trying to get back into C++ and I grabbed Alex Allain's Jumping Into C++ (I also have Koenig & Moo's Accelerated C++ for after) and wanted to know if anyone here has experience with the book and think it's an OK resource?

Also, what do you folks think I should be doing in addition to this? Beginner programmer daily exercises on some website, if that kind of thing exists?

My programming background: I took some of intro Java, C++, and C# courses back in college/university but I've been working in an unrelated field for the past 5 years.
 

Somnid

Member
I can't figure this out... Javascript/Handlebars/Handlebars Express issues

Why wont my page update until I refresh?

Ok so the base page should be running this select query, and populating my table. And it does that. But when I insert a new row into the table, I have to manually refresh the / base page before it shows up. I want the table to update automatically without the user having to refresh. How do I accomplish that? Is there a way to force the page to run its selection query again without having to manually refresh?

The easiest way is to use forms normally. When you click, let it POST to route "/" (POST is the http verb for inserting data so you should do that anyway for clarity). Insert the record and then do the exact same thing you would have done for GET.

If you want it to be more ajaxy, then you should get a front-end version of handlebars and template client side. After your page "shell" loads it fires off a GET ajax request to /workouts which returns the list of objects, when it comes back template you HTML with that object and insert in DOM. When you insert it's the same thing except you insert the POST data and then return the query result, or less efficient but more simply, have the client code GET the workouts endpoint again once an insert completes.

If you want even more efficiency then just get the id of the last insert and return that, have the client code add it to the existing array of workouts and re-template. And then if you feel like you really need a challenge figure out how to partially template so you aren't thrashing the DOM (but most websites will never go this far).
 

Bollocks

Member
Does anyone have the link to the joke site where they compared how to add 2 numbers in different languages?

c for example was
int foo = 3+5;

and the java example was ridiculously over engineered, with their own datatypes and spread across multiple files.

would like to find that article again for a java friend :)
 
The easiest way is to use forms normally. When you click, let it POST to route "/" (POST is the http verb for inserting data so you should do that anyway for clarity). Insert the record and then do the exact same thing you would have done for GET.

So give form a method of post?

See, I tried that but then it kept forcing the page to redirect to wherever I posted.

Or maybe I don't know how to handle POSTs properly. I'd need to change my /insert portion of my code as well, right? Would I also still be adding the event listener to the form's submit button?
 

Somnid

Member
So give form a method of post?

See, I tried that but then it kept forcing the page to redirect to wherever I posted.

Or maybe I don't know how to handle POSTs properly. I'd need to change my /insert portion of my code as well, right? Would I also still be adding the event listener to the form's submit button?

POST and GET if used in a form will always cause the page to redirect to that route. So if you POSTed via form to /insert the final route will be /insert. However the same route can be used by multiple HTTP verbs. You can have GET / and POST / do different things and this way the route doesn't change when you POST. This is why in scenario two I changed the route where the data comes from to /workouts (and assume the index.html is served from / or something) because GET would return all workouts and POST inserts a workout (PUT would update a workout and DELETE would delete one for completeness). This is a RESTful API.

Note that if you want POST to serve the same page you still have to do everything GET does (serve the templated HTML), they are different endpoints and the browser will not render anything you don't specifically tell it to.
 

Two Words

Member
EDIT- NEVERMIND! Ugh, 30 minutes of a headache all because I missed a comma on the predicate definition on the third to last line.....


Anybody here ever use SWI-Prolog? I'm getting a weird "predicate undefined" error and I don't get why. It says that shiftNeg(List, N, Result2) is undefined even though it is clearly defined. The point of the predicate is to shift cyclically by the value of N. A negative N means the direction is from right to left. The syntax highlighting is completely ignoring everything in the shiftNeg predicate.

Code:
shift(List,N,Result):-
    (N > 0) ->  
    	shiftPos(List, N, Result1),
    	Result is Result1
    ;   
    	shiftNeg(List, N, Result2),
        Result is Result2.

shiftPos(List, N, Result):-
    length(List, Len),
    Front is N mod Len,
    sublist(List, Sublist1, 0, Front),
    sublist(List, SubList2, Front + 1, Len),
    append(SubList2, Sublist1, Result).

shiftNeg(List, N, Result):-  
    length(List, Len),       
    N1 is N * -1,			
    N2 is N1 mod Len,        
    Front is Len - N2,      
    sublist(List, Sublist1 0, Front),
    sublist(List, Sublist2, Front + 1, Len),
    append(Sublist2, Sublist1, Result).
 

Kieli

Member
Sorry guys, I'm completely lost for one of my assignments.

Basically, I have one function that allocates space in dynamic memory. I have another function that deletes that chunk of memory, and then attempts to access one aspect of it using a pointer.

Obviously this is a dangling pointer.

So my solution is to create a local variable which gets assigned the value of that thing I tried to access, then delete the chunk of memory, and then finally return the temp variable.

Except I get the error "function returns address of local variable" which confuses me because I don't want to return an address. I want to return the value itself.

I'm working in C, by the way.
 

Two Words

Member
Sorry guys, I'm completely lost for one of my assignments.

Basically, I have one function that allocates space in dynamic memory. I have another function that deletes that chunk of memory, and then attempts to access one aspect of it using a pointer.

Obviously this is a dangling pointer.

So my solution is to create a local variable which gets assigned the value of that thing I tried to access, then delete the chunk of memory, and then finally return the temp variable.

Except I get the error "function returns address of local variable" which confuses me because I don't want to return an address. I want to return the value itself.

I'm working in C, by the way.
Can you post the code?
 

poweld

Member
Sorry guys, I'm completely lost for one of my assignments.

Basically, I have one function that allocates space in dynamic memory. I have another function that deletes that chunk of memory, and then attempts to access one aspect of it using a pointer.

Obviously this is a dangling pointer.

So my solution is to create a local variable which gets assigned the value of that thing I tried to access, then delete the chunk of memory, and then finally return the temp variable.

Except I get the error "function returns address of local variable" which confuses me because I don't want to return an address. I want to return the value itself.

I'm working in C, by the way.

Change your return type from a pointer (e.g. int*) to a value (e.g. int), and dereference the thing you're returning by prepending it with a * (e.g. *retVal).

That, or allocate the memory that you want to return on the heap with malloc/calloc so that you can return the reference without issue.
 

Kieli

Member
Can you post the code?

Unfortunately, assignment policy means I can't share the code. :\

As an aside, I'm trying to understand the stack-smash attack.

It apparently involves flooding a function with a very large string such that it overrides some buffer in the most recent activation frame. It involves slamming the stack with a bunch of addresses (of what and where?), followed by a nop slide, and finally by the viral code.

Apparently our goal is to approximate the location of the return address by hoping that it lands somewhere in the nop slide. Why the nop slide in particular? How do we determine how long we smash with addresses before we convert to the nop slide?

Is it probabilistic/random? Apparently if the stack were allocated downward, i.e. the most recent activation frame at higher addresses, then it can provide protection against this sort of attack (another obvious protection is bounds checking on the buffer for the input)

Change your return type from a pointer (e.g. int*) to a value (e.g. int), and dereference the thing you're returning by prepending it with a * (e.g. *retVal).

That, or allocate the memory that you want to return on the heap with malloc/calloc so that you can return the reference without issue.

Unfortunately, if we just malloc our answer and return it, we'll run into a memory leak. So we have to simultaneously avoid that and the dangling pointer.
 
Hrmm...

Code:
'<a href="/editrow?id='+id+'"><input type="button" value="Edit"></a>';

Is there a way to get rid of the href, and instead make this button take the user to /editrow?id=id on an onclick event?

edit: nevermind, I got it.
 
Hrmm...

Code:
'<a href="/editrow?id='+id+'"><input type="button" value="Edit"></a>';

Is there a way to get rid of the href, and instead make this button take the user to /editrow?id=id on an onclick event?

edit: nevermind, I got it.

You may want to post your solution in case someone has the same problem and finds your post.

Code:
<button onclick="window.location.href='http://www.google.com/'">
 
You may want to post your solution in case someone has the same problem and finds your post.

Code:
<button onclick="window.location.href='http://www.google.com/'">

Actually, I'm having an issue.

So my buttons look like this:

Code:
<input type="button" value="Edit" onclick="EditInfo({{this.id}})"></td>

I can confirm that the proper ID is getting pinned to this.

EditInfo function looks like:

Code:
function EditInfo(rowID){
	console.log("IN EDIT");
	window.location("/updateInfo?id="+rowID+""); 
	return false;
}

Clicking the button on my page does get "IN EDIT" logged to the console. However the redirect that should happen with windows.location never happens. And I can't figure out why. Any ideas?
 

Somnid

Member
Actually, I'm having an issue.

So my buttons look like this:

Code:
<input type="button" value="Edit" onclick="EditInfo({{this.id}})"></td>

I can confirm that the proper ID is getting pinned to this.

EditInfo function looks like:

Code:
function EditInfo(rowID){
	console.log("IN EDIT");
	window.location("/updateInfo?id="+rowID+""); 
	return false;
}

Clicking the button on my page does get "IN EDIT" logged to the console. However the redirect that should happen with windows.location never happens. And I can't figure out why. Any ideas?

window.location isn't a function. You really want window.location.href = "http://my.new.url/"

Also, where possible try not to use inline event handlers, they're bad practice. Instead try to setup the click event from javascript. Note that if you need to add information to a DOM element you can create your own attributes by prefixing them with "data-{key}" and access them with .dataset[key].

Code:
<button data-id="4">My button</button>

var id = document.querySelector("button").dataset["id"];
 

D4Danger

Unconfirmed Member
can I ask why you don't want to use an <a> in this situation?

using a button and wiring it up with js is needlessly complicated.
 
can I ask why you don't want to use an <a> in this situation?

using a button and wiring it up with js is needlessly complicated.

Assignment restriction. :(

Ok so now I'm having a different problem.

I took Sominid's advice and am not using an event handler. I'm changing it to onclick.

I have:
Code:
var editBtnCell = document.createElement('td');
		editBtnCell.innerHTML = '<input type="button" value="Edit" onclick="window.location.href="updateWorkout?id='+id+'>';
	    newRow.appendChild(editBtnCell);

Such that when a user generates a new row in the table (via a form), it will append that edit button with the correct functionality. Only... it doesn't work.

However, when I refresh the page the edit buttons works. And I've figured out why. On a refresh my page generates the rows using:

Code:
<td><input type="button" value="Edit" onclick="window.location.href='editForm?id={{this.id}}'"></td>

this.id generates an integer. The html code for the button on page refresh is something like:
Code:
<input value="Edit" onclick="window.location.href=" editForm?id=14 type="button">

While the code when a row is generated by the user submitting a form is like:
Code:
<input value="Edit" onclick="window.location.href=" editForm?id="14" type="button">

In this case a string 14. Not an integer.

How do I change
Code:
var editBtnCell = document.createElement('td');
		editBtnCell.innerHTML = '<input type="button" value="Edit" onclick="window.location.href="updateWorkout?id='+id+'>';
	    newRow.appendChild(editBtnCell);
such that it appends an integer to the string that will be interpreted as an integer?
 

D4Danger

Unconfirmed Member
Assignment restriction. :(

is the restriction that you have to use a button?

if you have to make a GET request with a button you could use a form

Code:
<form action="/updateInfo" method="get">
    <input type="hidden" name="id" value="ID_NUMBER_GOES_HERE"/>
    <input type="submit" value="Edit"/>
</form>
 
is the restriction that you have to use a button?

if you have to make a GET request with a button you could use a form

Code:
<form action="/updateInfo" method="get">
    <input type="hidden" name="id" value="ID_NUMBER_GOES_HERE"/>
    <input type="submit" value="Edit"/>
</form>

A button must be used.

We're creating a site that allows users to store journal entries in a database.
I have a form that lets them fill in things like name, page number, date, etc...
When they visit the page a select query gets made, and a table is built. That's the code with {[this.id}}.

However, when they hit submit on the form I need to generate that new table row right away. I can generate it, but the Edit link doesn't work. I almost have it, but I can't get the ID to append in javascript as an integer. It just gets interpreted as a string. So the edit query cannot be properly made on a new row.

Once I refresh the page the table is rebuilt so that newly added row gets the proper edit button with {{this.id}}. I need to figure out how to get the edit button to work on a fresh row before I refresh the page.
 

D4Danger

Unconfirmed Member
okay. well the reason this isn't working

Code:
editBtnCell.innerHTML = '<input type="button" value="Edit" onclick="window.location.href="updateWorkout?id='+id+'>';

is because of the way the quotes around the attributes are being escaped. you've got stringception going on here.

Code:
editBtnCell.innerHTML = '<input type="button" value="Edit" onclick="window.location.href=\'updateWorkout?id='+id+'\';">';
                                                                                         ^^                       ^^
                                                                                         ^^                       ^^
will give you

Code:
<input type="button" value="Edit" onclick="window.location.href='updateWorkout?id=999';">
 
okay. well the reason this isn't working

Code:
editBtnCell.innerHTML = '<input type="button" value="Edit" onclick="window.location.href="updateWorkout?id='+id+'>';

is because of the way the quotes around the attributes are being escaped. you've got stringception going on here.

Code:
editBtnCell.innerHTML = '<input type="button" value="Edit" onclick="window.location.href=\'updateWorkout?id='+id+'\';">';
                                                                                         ^^                       ^^
                                                                                         ^^                       ^^
will give you

Code:
<input type="button" value="Edit" onclick="window.location.href='updateWorkout?id=999';">

Ahh shit. Thanks dude.
 

Makai

Member
A lot of inheritance and singletons, which I can forgive because I didn't know any better. But there's DRY violations everywhere and I'm neglecting to use critical language features:
Code:
List<Vector3> verts = new List<Vector3>();
verts.Add(point);
verts.Add(polygon.Vertices[side]);
if (side == polygon.Sides - 1)
{
    verts.Add(polygon.Vertices[0]);
}
else
{
    verts.Add(polygon.Vertices[side + 1]);
}

Quick refactor:
Code:
var verts = new[]
{
    point,
    polygon.Vertices[side],
    polygon.Vertices[side == polygon.Sides - 1 ? 0 : side + 1]
};
 
A lot of inheritance and singletons, which I can forgive because I didn't know any better. But there's DRY violations everywhere and I'm neglecting to use critical language features:
Code:
List<Vector3> verts = new List<Vector3>();
verts.Add(point);
verts.Add(polygon.Vertices[side]);
if (side == polygon.Sides - 1)
{
    verts.Add(polygon.Vertices[0]);
}
else
{
    verts.Add(polygon.Vertices[side + 1]);
}

Quick refactor:
Code:
var verts = new[]
{
    point,
    polygon.Vertices[side],
    polygon.Vertices[side == polygon.Sides - 1 ? 0 : side + 1]
};

Also:

Code:
var verts = new[]
{
    point,
    polygon.Vertices[side],
    polygon.Vertices[(side + 1) % polygon.Sides]
};
 
Code:
		editBtnCell.innerHTML = '<input type="button" value="Edit" onclick="window.location.href="updateWorkout?id='+id+'>';
	    newRow.appendChild(editBtnCell);
such that it appends an integer to the string that will be interpreted as an integer?

Note that you most likely won't want to do it like that. Instead you should create actual elements and use web API's such as "addEventListener" to add your click handlers and such.

For example:

Code:
var input = document.createElement("input");
input.type = "button";
input.value = "foo";

input.addEventListener("click", function () {
  alert("Hi");
});

var foo = document.getElementById("foo");
foo.innerHTML = "";
foo.appendChild(input);

Try it out in this fiddle https://jsfiddle.net/xgLzpt4b/
 

JeTmAn81

Member
BTW, that onclick event is looking pretty unwieldy. It would be better to create a custom function and call that instead. Perhaps something like this:

onclick="RedirectToEditLink(this);"

function RedirectToEditLink(clickedButton)
{
}


I put the this in there so it will pass a reference to the button that was clicked in case you need that. Not sure where the id value is coming from but that kind of thing can usually be retrieved if you have a handy reference to the calling element. Or you can probably just add it as a function parameter.
 

Skinpop

Member
Because it has a complicated UI. Tbh I don't remember the last time disk space was actually a real concern for anyone.

9 gigabytes of ui... that would be something.

obviously it installs a lot of useless crap that I don't want and can't not install. like windows phone stuff, F# thingies and all kinds of frameworks and who knows what. All I want is c++ and win32 but apparently that's impossible.
Disk space being cheap is not an excuse for writing bad software, that's like saying programmers shouldn't worry about performance since computers are so fast these days anyway. The only thing we got from that kind of thinking is slow, bloated and shitty software.
And while size on disk might not matter all that much it makes install/updates incredibly slow and the fact that I can't just quickly zip the whole thing and bring it to another system or upload it to my dropbox adds another layer of shittyness.
 

Somnid

Member
If VS Code had the same level of debugging support as Visual Studio, I'd drop Visual Studio in a heartbeat. At least internally they realized VS is a monster.
 

Rur0ni

Member
The next version I think is broken up a bit so the install doesn't take up so much space. I'm not really concerned with it. I also use VS Code.
 

Skinpop

Member
Still, I'm not sure I understand how UI can take gigabytes?
It doesn't, it's because of stuff like this(minimal install):
- AzureTools.Notifications
- Behaviors SDK (Windows Phone) for Visual Studio 2013
- Behaviors SDK (Windows) for Visual Studio 2013
- Blend for Visual Studio SDK for .NET 4.5
- Blend for Visual Studio SDK for Silverlight 5
- Microsoft .NET Framework 4.5.1 Multi-Targeting Pack
- Microsoft .NET Framework 4.5.1 Multi-Targeting Pack (ENU)
- Microsoft .NET Framework 4.5.1 RC Multi-Targeting Pack for Windows Store Apps
- Microsoft .NET Framework 4.5.1 RC Multi-Targeting Pack for Windows Store Apps (ENU)
- Microsoft Advertising SDK for Windows 8.1 - ENU
- Microsoft Advertising SDK for Windows Phone 8.1 XAML - ENU
- Microsoft Advertising Service Extension for Visual Studio
- Microsoft Azure Mobile Services SDK
- Microsoft Azure Mobile Services Tools for Visual Studio - v1.2
- Microsoft Azure Shared Components for Visual Studio 2013 - v1.2
- Microsoft C++ Azure Mobile SDK for Visual Studio 2013
- Microsoft C++ REST SDK for Visual Studio 2013
- Microsoft Expression Blend SDK for .NET 4
- Microsoft NuGet - Visual Studio 2013
- Microsoft Report Viewer Add-On for Visual Studio 2013
- Microsoft Team Foundation Server 2013 Update 3 Object Model (x64)
- Microsoft Team Foundation Server 2013 Update 3 Object Model Language Pack (x64) - ENU
- Microsoft Visual Studio 2010 Tools for Office Runtime (x64)
- Microsoft Visual Studio 2013 Add-in for Windows Phone
- Microsoft Visual Studio 2013 Add-in for Windows Phone - ENU Language Pack
- Microsoft Visual Studio 2013 XAML UI Designer
- Microsoft Visual Studio 2013 XAML UI Designer - ENU
- Python Tools Redirection Template
- SharePoint Client Components
- Team Explorer for Microsoft Visual Studio 2013
- TypeScript Power Tool
- TypeScript Tools for Microsoft Visual Studio 2013
- Visual F# 3.1 SDK
- Visual F# 3.1 VS
- Visual Studio Extensions for Windows Library for JavaScript
- Windows App Certification Kit Native Components
- Windows Phone 8.1 SDK - ARM
- Windows Phone 8.1 SDK - Desktop
- Windows Phone 8.1 SDK - x64
- Windows Phone 8.1 SDK - x86
- Windows Phone 8.1 Tools for Visual Studio 2013
- Windows Phone 8.1 Tools for Visual Studio 2013 - ENU
- Windows Phone 8.1 Tools for Visual Studio Professional 2013
- Windows Phone 8.1 Tools for Visual Studio Professionald 2013 - ENU
- Windows Phone SDK 8.0 Assemblies
 

JeTmAn81

Member
Because it has a complicated UI. Tbh I don't remember the last time disk space was actually a real concern for anyone.

Yeah the real issue with VS 2015 is memory. I'm running anywhere from 5-10 instances all the time and those suckers can push a gig a piece of RAM. It's a lot higher than older versions and I have no idea why. I had to have my workplace double my RAM to compensate.
 

Skinpop

Member
have you tried the new visual studio installer they're testing?

I'd love a 322 mb installation. What I'd really like is an alternative to the visual studio debugger so I can drop the whole thing but it looks like we are a few years away from that.

edit: it's still 2.5 gig with c++.
 
that's the most Microsoft thing I've seen for a while.

There's like 3 different version numbers in play, and two of them turned out to be very similar. For example, with VS 2015, you have:

Code:
C:\Users\Me>devenv /version

Microsoft Visual Studio 2015 Version 14.0.25123.0.
Copyright (C) Microsoft Corp. All rights reserved.

So you've got the product name, which is "Visual Studio 2015", and the product version, which is "14.0.25123.0".

To make matters worse you've still got the compiler version.

Code:
C:\Users\Me>cl /version
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23918 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

Due to an unfortunate coincidence when they started branding the products with a year, the last 2 digits of the year ended up being 1 number higher than the product version, so people would always be confused. If you said "Visual Studio 12" were you talking about 2012 (version 11.0) or version 12 (VS 2013).

They haven't publicly said what the deal is with "Visual Studio 15", but my guess is that they want to remove this confusion. Going forward the product branding "Visual Studio 15" will match the product version "version 15.0.xxxxx".

Of course you've still got the compiler version, so you'll still have to write stupid shit like this:

Code:
#if _MSC_VER >= 2000
// Visual Studio 15 or higher
#endif

Anyway, enough history.

I'd love a 322 mb installation. What I'd really like is an alternative to the visual studio debugger so I can drop the whole thing but it looks like we are a few years away from that.

WinDbg?
 

Tacitus_

Member
Uh, any hybrid gurus here? I'm doing an Ionic2 app and got a tiny problem with my images. I've got images that display just fine when running via ionic serve, but not when running on a device. The thing that puzzles me the most is that they show when running with the --livereload option.
Using Chrome inspector, it throws a
Code:
Failed to load resource: net::ERR_FILE_NOT_FOUND
for the images.
 
Uh, any hybrid gurus here? I'm doing an Ionic2 app and got a tiny problem with my images. I've got images that display just fine when running via ionic serve, but not when running on a device. The thing that puzzles me the most is that they show when running with the --livereload option.
Using Chrome inspector, it throws a
Code:
Failed to load resource: net::ERR_FILE_NOT_FOUND
for the images.

Are you using relative paths for the images? Make sure that they relative to `index.html`
 

Tacitus_

Member
Are you using relative paths for the images? Make sure that they relative to `index.html`

I should be, they work in the live page after all

Code:
'./build/assets/textures/bridge2/img_name.jpg'

This is the output after transpiling:
4xJSdWE.png
 
I should be, they work in the live page after all

Code:
'./build/assets/textures/bridge2/img_name.jpg'

This is the output after transpiling:
4xJSdWE.png

That should be `<img src="assets/textures/bridge2/img_name.jpg">` right? Index is at `build/index.html` and assets are at `build/assets`?
 

Tacitus_

Member
That should be `<img src="assets/textures/bridge2/img_name.jpg">` right? Index is at `build/index.html` and assets are at `build/assets`?

Oh, right. But why'd it work in the browser... bah. Well, the situation didn't change

Code:
Failed to load resource: net::ERR_FILE_NOT_FOUND file:///android_asset/www/assets/textures/bridge2/posx.jpg

and with the new filepath, it fails in the browser

Code:
http://localhost:8100/assets/textures/bridge2/posy.jpg Failed to load resource: the server responded with a status of 404 (Not Found)
(don't get hang up on the exact filename, there's a posx, posy and posz with neg counterparts)
 

Regiruler

Member
I'm stuck learning fsharp so I can add a block to a file that nobody else at the company knows how to write or maintain.

This is hell.
 
Top Bottom