C/C++ :: How Does Casting Work (under The Surface)

Mar 14, 2014

So, I've used int to float cast before. And it makes sense that it preserves the int value just converts it. I don't really need the answer, I'm just interested, and a resources could suffice.

1. Why in my test program it seems to preserve the int value, I expect that, but why for 0x8000 is it registering bit negation also. I know that is the negative bit for float, but it seems wrong. Is this an error in the gcc compiler conversion code?

2. What is the documentation on these type cast on how they actual work.

3. I know like no assembly, I'm wondering if some of the built in routines to handle or is it all c side code.

4. Can I convert type and preserve the bits. Maybe use void* casting ? I've never really bothered with void* so I don't all that I can do. Except be a pointer that doesn't know the type, obviously. I tested that out in the second code, the output doesn't seem correct, except -0.000. Is it working and my test numbers just are improper float format? It can't be that I test 0x3E20000 = 0.15625 from SingleWiki , but I got 1.328e-36 so the int to void* to float doesn't seem to work in the code below:

#include <cstdio>
int main(){
float flout;
unsigned int num = 1;
int ant;
printf("Int Shift float
");
for(unsigned int shift=0;shift<32;shift++){

[Code] ....

View 5 Replies


ADVERTISEMENT

C++ :: SDL User-made Surface Class?

Feb 13, 2014

I want to make an encapsulated surface class that keeps track of important aspects about a surface. I have it started, except when I test the .onDraw() function, nothing appears on the screen. My Code is below:

#include "NSurface.h"
NSurface::NSurface() {
surface = NULL;
}
NSurface::~NSurface() {
SDL_FreeSurface(surface);

[code]....

why nothing is blitting to the screen when I test that class.onDraw() and SDL_Flip(screen)?

View 1 Replies View Related

C/C++ :: Applying Heat Flux On A Surface

Apr 21, 2014

I am trying to write udf for heat flux on X-Y plane in Fluent having the formula

Q(Y) = (0.304)/(deltaY + deltaY/2)^-3
Where deltaY = 50cm and
Y = 10m

but I am unable to do it. Can write udf for heat flux? Y is the length on Y-axis and X whixh is equal to 5m is length on X-axis. Q is heat flux goes through X-Y plane in Z direction. Geometry is a cube with dimension 10*5*0.005 all are in meters.

View 1 Replies View Related

C++ :: Game Freezes When Applying Surface From One Object Onto Another?

May 28, 2013

Been trying to figure out why my program freezes. I know exactly what line of code is causing it, but I can't figure out WHY it's causing it. It compiles fine, there are no errors returned, and then the game just stalls and I have to ctrl+alt+del to kill it.

Anyway, what I have set up is something like this:

main.cpp
#include "SDL.h"
#include "SDL_gfxPrimitives.h"
#include "SDL_image.h"
#include "SDL_rotozoom.h"

[Code]....

This is the line of code that's freezing the program. Simply put, so that you don't really have to go through it piece by piece, what I have done is:

*each player gets their own drawing surface called PlayScreen
*each player gets an array of 360 drawing surfaces called ShipPic. These are to keep the game from having to render the rotation pics of the ship on the fly.
*Get_Ship clips the requested ship picture out of the ship sprite sheet and puts it in ShipPic[0] for the original angle.
*the original picture is rotated by 1 degree and put into the 360 ShipPic slots.
*when the player rotates their ship, the angle changes, and it calls the ShipPic with the same number as the player's angle and places it on the screen.

All of this works perfectly.

Then, in Player::draw_screen(), I have it set up so that each player looks at all the other players and gets their distance. If they're within range, it takes the other player's picture rotated by the other player's angle and puts it on the current player's PlayScreen. This is where it freezes.

I've checked for NULL pictures, I've checked to be sure the angle is between 0 and 359, nothing makes any difference. I know it's reading the other player's information since I can output all of the player's X & Y coordinates, angles, the width/height of their pictures, etc. on each other's screens. So they're definitely talking.

To test the code, I've changed it from

Apply_Surface(ShipX, ShipY, p[i].ShipPic[(int)p[i].angle], PlayScreen);
to
Apply_Surface(ShipX, ShipY, p[ID].ShipPic[(int)p[ID].angle], PlayScreen);

And it works perfectly, placing the player's OWN picture in for the other players. So the function works. It's just when I try to take another player's picture and place it on the current player's screen that it freezes.

I've tried quite a few different ideas, such as creating a temp drawing surface to blit the other player's picture onto, but again, it freezes as soon as I try using the other player's pictures.

View 5 Replies View Related

C++ :: How To Calculate Total Surface Covered - Overlapping Squares

May 24, 2013

How would i calculate a surface that up to 10^5 squares cover. The info you get is xpos,ypos and width for each square.

How would you calculate the total surface covered (if any overlap only one counts).

Cannot use a big array of bools as the memory limit is 512 MiB.

View 2 Replies View Related

C :: How To Calculate Surface Area And Volume For Rectangular Prism

Jan 24, 2015

So for my assignment, I have to write code to calculate the surface area and volume for a rectangular prism that involves the use of functions. I made the program without functions and it works perfectly but as I'm putting in the required functions, it conversely made it non-functional, ironically. How to call functions correctly and the online book we're using now is confusing me even more.

Code:

#include <stdio.h>
#include <math.h>
int main(void) {
double x1, x2, x3, x4, x5, x6, x7, x8 = 0;
double y1, y2, y3, y4, y5, y6, y7, y8 = 0;
double z1, z2, z3, z4, z5, z6, z7, z8 = 0;
double length, width, height=0;

[Code]....

And here is the version I'm using to test it (without the need to input the individual coordinates).

Code:

#include <stdio.h>
#include <math.h>
int main(void) {
double x1, x2, x3, x4, x5, x6, x7, x8 = 0;
double y1, y2, y3, y4, y5, y6, y7, y8 = 0;
double z1, z2, z3, z4, z5, z6, z7, z8 = 0;
double volumePrism, surfaceArea =0;

[Code]...

View 3 Replies View Related

C++ :: Calculate Surface Area Of Various Shapes - Symbolic Constant For The Value Of PI

Mar 2, 2013

This error keeps coming.

******************************************************************
CSCI 240 Program 5 Part 2 Spring 2013

Programmer:
Section: 1
Date Due: 3/1/13

Purpose: This program uses functions to calculate the surface area of various shapes. It is an exercise in learning to write functions.
******************************************************************/

#include <iostream>
#include <iomanip>
using namespace std;
#define PI 3.14
//Symbolic constant for the value of PI

int Menu();
int getValue( string prompt, int lowerBound, int upperBound );

[Code] .....

View 10 Replies View Related

C++ :: Getting Surprising Behavior When Casting

Mar 18, 2014

I'm trying to cast a float to an unsigned int and getting some surprising behavior.

Code:
float x = -1.0;
unsigned int y = (unsigned int)x;
printf("y = %d
", y);

The output of this code changes depending on which compiler I use. Sometimes I get -1 and sometimes I get 0. Not really sure why though.

View 5 Replies View Related

C++ :: Casting A Logical Boolean As Int?

Jun 8, 2013

Let's say I have a product that needs service after 500 hours at 100 RPM. I can dsiplay the remaining hours only as an integer and we only have integer math. Management wants it to display 500 for the first hour, not 499 after the first minute is ticked off. But after a few minutes fullHours will be 499 and partialHours will have some value. I have 3 ways to determine displayHours to suit management and I want to know if the first is ligit.

short fullHours = 500;
short partialHours = 0;
short displayedHours = 0;

// Method 1 - Is is Kosher to cast a boolean as an int? Is TRUE always a 1 or is that a bad assumption?

displayedHours = fullHours + (short) (partialHours != 0);

//Method 2 - Works but some have disdain for the ternary conditional

displayHours = fullHours + (partialHours ? 1 : 0);

//Method 3 - seems a bit pedantic

displayHours = fullHours;
if (partialHours != 0) {
displayHours++;
}

View 19 Replies View Related

C++ :: How To Use Type Casting In Programs

Apr 12, 2013

I find type casting to be very hard to grasp, I am not sure why. I understand how it works I suppose, but I find it hard to grasp why it would be needed in programming. So my question is, how often is type casting used in general programs? Is there an easier way I could be trying to teach myself about it? I am just using the tutorials provided by this site.

View 2 Replies View Related

C/C++ :: Casting Error Char To Int

Apr 30, 2014

I'm writing a small piece of code that increments through a string of numbers. For every 5 numbers a product is produced. However i'm having an issue understanding an error that keeps occurring.

#include <iostream>
#include <string>
#include <cstdlib>

[Code].....

if you look at the code i've got a string with 5 numbers 1-5 for testing. As the loop increments through the string it takes the first 5 characters and converts them to integers and then a product is returned. The issue is that instead of storing the single letter at position J its storing some combination from the string and as a result gives me a huge product. I tried debugging. I checked what the string[j] value was with a simple cout << and it returned the right number. I even returned the char value before it was converted into an integer and it was the right number.

View 3 Replies View Related

C# :: Working With Different Types And Casting?

Jan 3, 2015

double a = 1.0 + 1 + 1.0f; //everynumber will be added up as a double even the last one which is a float. All 3 numbers will turn into 3.0 as a double.

int x = (int)(7 + 3.0 / 4.0 * 2); //the variable will do the math bracket first. then the va type will still be an int because int was never changed.

Console.WriteLine((1 + 1) / 2 * 3); // 1 + 1 will be done first then 1 / 2 then * by 3
Console.WriteLine(x);
Console.ReadKey();

I THINK THATS ALL WRONG ^ =/ like the comments

double a = 1.0 + 1 + 1.0f;

In this equation, everything is using addition, so we start working left to right. 1.0 + 1 is the first step. These two representations of 1 aren't the same type though. In fact, none of the three are.

The first is a double, the second is an int, and the last is a float.

So in order to do 1.0 + 1, we need to convert one type to another. Since the double type is "wider" than the int type, we'll move things up to the double type. We'll convert the int version to a double, and to 1 + 1 using double types, resulting in 2.0 as a double.

Next we do the other addition. This has the same problem, though, because we'll be adding our result from the first step (a 2 as a double) to a float. So again, the float gets converted up to a double and we do the addition using doubles.

We now have a value of 3 that is the double type, which we can simply store in our a variable without any conversions at all, since our value is already a double type.

View 4 Replies View Related

C++ :: Casting Pointers In C Style?

Sep 5, 2014

Casting Pointers in C Programming. I don't want to move onto implicit casts until I have this down pat. I'm failing to understand how casting pointers works.

The line
int *mnt = (int*)&flt;
if I read this correctly passes the address of flt which has been converted to an int to the pointer mnt.

1 - When I output mnt I get a garbage value, probably because the address of flt is then converted to a pointer and passed onto mnt as a value and then reinterpreted as a memory address. (that is the first part I don't understand)

2 - - What exactly does the (int*) cast say? Does this mean that a pointer will be returned or an address will be returned. What does the fact that the * is inside the parenthesis mean?

Code:
#include <iostream>
using namespace std;
int main() {
float flt= 6.5;
int *mnt = (int*)&flt;
cout << mnt << endl; // outputs hex memory address
cout << *mnt << endl; // outputs garbage value
}

View 9 Replies View Related

C :: Utilizing Type Casting Construct

Apr 13, 2014

This code i made, utilizing the type casting construct, isn't outputting what i wanted. The output for 'Dollars' and 'Cents' are returning '0' for both. instead all i want it to do is seperate the two. for example changing the float value of amount to an integer, giving a dollar value.

Code:

#include <stdio.h>
int main()
{
}

[code]....

View 1 Replies View Related

C :: Significance Of Int Casting In Program With Array Of Structures

Jun 18, 2013

I am doing an exercise which has to do with International country codes.The user must give a code and the programm will display the corresponding country.

Code:
#include <stdio.h>
#define COUNTRY_COUNT
((int) (sizeof(country_codes) / sizeof(country_codes[0])))
[code]....

View 14 Replies View Related

C :: Unsigned Char - Pointer Type Casting

Dec 2, 2013

I came across some code and it's not clear why it is casting an unsigned char * to another pointer type only to free it right after. Here are the relevant structures:

Code:
struct _Edje_Message {
Edje *edje;
Edje_Queue queue;
Edje_Message_Type type;
int id;
unsigned char *msg;

[Code] .....

As you can see, _Edge_Message has a *msg field, but in the function below, they cast it to the other two structure types inside the case blocks of the switch statement only to free it. What is the point or advantage of doing this?

Code:
void
_edje_message_free(Edje_Message *em) {
if (em->msg) {
int i;
switch (em->type) {

[Code] ......

View 14 Replies View Related

C++ :: Building Multicast Packet - Memcpy Casting

Aug 19, 2014

I am just getting back in to C++ after 10 years not doing any, contributing to an open source project. I'm adding in some functionality and am hitting a road block.

I need to send a multicast packet out on the network that is structured in a certain way. I have the definition, and know what data is going in each byte. I can successfully send a message using multicast, I now just need to send the right message.

I have used a char array to hold the message, as each char represents 1 byte, and I can transmit the array.

I am having trouble putting all of the data in the right place though. If my source data is a string, then I seem to be able to convert it, but if it is a short or int, then I keep getting errors when compiling. Similarly, two of the lines, (version and type) i initially tried using char arrays with a length of one.

Should I be using memcpy or a different function, or even be doing this in a totally different way altogether? This is the code that I am using, along with the packet structure:

//Construct a Zone Query packet
// 4 bytes - Signature "Ohz " = 0x6f, 0x68, 0x7a, 0x20
// 1 bytes - Version = 1
// 1 bytes - Type (0 = Zone Query, 1 = Zone Uri)
// 2 bytes - Entire message length = 12 + zone length
// 4 bytes - Length in bytes of the zone ID
// n bytes - Zone ID to query

[Code] ....

The errors that I get are:

error: invalid conversion from ‘short int’ to ‘const void*’ [-fpermissive]
memcpy(buffer + 6, packetLength, sizeof(packetLength));
^
[Code] ....

View 9 Replies View Related

C++ :: Vector Of 8bit Variables - Casting Between Bitsets

Mar 7, 2012

So I have a vector of 8bit variables. Is it possible to cast that to a vector of 6bit variables like...

8bit: 10011011 01100011 ===>
6bit: 100110 110110 0011XX

View 7 Replies View Related

C++ :: Validity Of New / Delete Pair (or Malloc / Free) After Pointer Casting

Dec 22, 2012

Goal: To allocate some memory as a char*, read in some binary data, re-interpret it as a float* and then free the memory.

My code looks like:

void someFunction(float* &result) {
char * tmp = new char[1000];
//...Fill the char buffer here...
result = (float*)tmp; //Reinterpret binary data as floats

[Code] ....

Is the cast back to char* necessary on the red line (or could I have validly left it as float*)? Would it be different if I had written char * tmp = (char*)malloc(sizeof(char)*1000); on the blue line (and correspondingly used free (char*)floatData on the red line?

View 9 Replies View Related

C++ :: Debug Error When Taking A String And Casting It To Float With File Stream

May 9, 2014

Debug Error

ProjectsFinal ProjectGrItemDebugGrItem.exe

R6010 - abort() has been called

I was going over this with a friend and it seems as though getline() is not reading anything in and thus throwing the abort error. I'm not sure why this is because I've included the textfile, with the correct name of course, in both the regular file location and the debug folder. I ask for user input and the user then inputs the name of the file they want, I do some required things behind the scenes and display the results for them in a cmd window. I've included pastebin files for both my header and cpp files because it is far to large for one post I shall, however, post the full code in the comments.

Quick Code

The problem occurs on line 159. I'm assuming once this line is fixed, line 163 will have the same problem.

// Read regular price
getline(nameFile, input, '$');
vectorList[count].regPrice = stof(input.c_str());// Casts string to a float
// Read sale price
getline(nameFile, input, '#');
vectorList[count].salePrice = stof(input.c_str());

Pastebin Links : [URL] ....

View 2 Replies View Related

C :: Parsing Binary Data File By Casting A Buffer - Accessing Double From Struct

Jan 4, 2014

I am parsing a binary data file by casting a buffer to a struct. It seems to work really well apart from this one double which is always being accessed two bytes off, despite being in the correct place.

Code:

typedef struct InvoiceRow {
uint INVOICE_NUMBER;
...
double GROSS;
...
char VAT_NUMBER[31];
} InvoiceRow;

If I attempt to print GROSS using printf("%f", row->GROSS) I get 0.0000. However, if I change the type of GROSS to char[8] and then use the following code, I am presented with the correct number...

Code:

typedef struct example {
double d;
}

example;
example *blah = (example*)row->GROSS;
printf("%f", blah->d);

View 7 Replies View Related

C++ :: Casting Non-const Variable To Const

Jun 19, 2013

Is there any way to cast a non-const variable to const one?

I want to read variable n from file and then use it to declare array "int arr[n]", but because n is non-const, the compiler doesn't allow me to do that.

View 6 Replies View Related

C++ :: Getline Won't Work

Nov 14, 2013

I've just started learning from "Jumping into C++". Great book. Unfortunately, I've also encountered my first snag. Practice problem number 3 from chapter 4 tells me to make a small calculator that takes one of the four arithmetic operations and its two arguments as input and give the result as output. Here's what my newbie mind came up with:

Code:
#include <iostream>
#include <string>
using namespace std;
int main()

[Code] .....

The thing that I don't get is why doesn't the function "getline" work. It's worked in previous programs. The program seems to work if I simply replace getline with a simple "cin". I could easily use that as a cheap fix but I am interested in knowing why "getline" refuses to work anymore....

View 3 Replies View Related

C :: Cannot Get A Division To Work

Feb 7, 2014

what I do I cannot get a division to work:

Code:

//END RANGE INPUT
long double End;
printf("
Please enter the start of the range (Lower Bound):
");

[Code]...

No matter what I input for the values of 'Start', 'End' and 'Interval', the value of 'SizeL' always seems to be -2.

View 8 Replies View Related

C :: Can't Get Tic-tac-toe Win Algorithm To Work

Apr 9, 2014

I am writing a simple console-based tic-tac-toe game. I am trying to write a function to check whether someone has won the game.

The board data is saved in an array called board: Code: int board[3][3] with each element corresponding either to an empty spot, an X, or an O, using the numbers 0, 1, and 2, respectively. For clarity:

Code:
#define EMPTY 0
#define X 1
#define O 2 Here is my function: Code: int check_state(int board[3][3]) {
int winner = 0;

[Code].....

View 1 Replies View Related

C :: Calendar Doesn't Work

Sep 24, 2013

I just dont see what the issue is here. I have stared at this thing forever. Im trying to make a calendar from scratch so I can be prepared for my second test on Friday.

Code:
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i, n, s;

[Code]....

View 10 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved