Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-15-2005, 09:35 AM   #1 (permalink)
slashdot
Registered User
 
Join Date: Dec 2004
Posts: 43
slashdot is on a distinguished road
? About data types...

I notice that there are different data types. And I am assuming there fore storing data (obviously), but my question is, if you need to store a piece of data that doesn't require a short data integer (signed: -32768 to 32767
unsigned: 0 to 65535), but could be put into a char (signed: -128 to 127
unsigned: 0 to 255) which could take less space. Would the program be faster if you be more precise? Or is it just the calculations that make the program faster or slower? If it totaly flew by my head, and you know it, could you give me a better definition on how it works?
slashdot is offline   Reply With Quote
Old 04-15-2005, 10:38 AM   #2 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 734
DJMaze is on a distinguished road
it's memory.

char = 8 bit
short = 16 bit
int = 32 bit
int64 = 64 bit

So depending on how big the number can be you decide how much memory you want it to use.
If your app is small it has no real use to specificaly specify data.
However if you do huge calculations or have an array with millions of entries then you do notice the size differences.

Strings and characters are a bigger concern (like in wordprocessors) because each character is 8bit.

PHP Code:
int number 65535// 32 bits
char *string "65535"// 48 bits (including NULL terminator) 
DJMaze is offline   Reply With Quote
Old 04-15-2005, 10:57 AM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
Would the program be faster if you be more precise?
Most likely not. Most likely for standard PC's even slower. The standard didn't provide a formal definition for that. Typically for such questions, one needs to test. In almost every case, the answer is the same: test.

There is another issue.
If there are no special requirements to meet when it comes to saving memory, then don't do too much work. For example, if you implement a variable using unsigned char, the coder that depends on your code, might end up with nasty surprises because he expected a plain integer.

For your conveniance, assume the int is the standard integer type, and double is the default floating point precision type. The ISO says so by the way. Only when special requirement need to be met, deviate.

Examples.
- Salary is usual up to 2 decimals accurate (cents). float seems to suffice. Nevertheless, implement double:
Code:
double MyHourSalary = 12.04;
- Index for an array or matrix. Often these containers can hold quite a bit of data. A normal int often doesn't suffice. unsigned seems is ok, but might be problematic as well. To make sure we have type-wise a valid index, we should use std::size_t :
Code:
#include <cstdlib>

std::size_t Idx = 12;
Double TheArray[Idx];
//Fill the array here. Then we iterate:
for(std::size_t i = 0; i < Idx; ++i}
{
   cout<<TheArray[i]<<endl;
}
Was a good question.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Filling a datagrid with BLOB data vinny MS Technologies ( ASP, VB, C#, .NET ) 2 08-19-2004 12:06 AM
Application Scope in a Web App sde Java 5 08-17-2004 10:55 AM
dotnet Data Access reader vs adapter, and MySQL APIs vs SQL2k sde MS Technologies ( ASP, VB, C#, .NET ) 3 12-04-2003 07:45 AM
SQL 2k Text Data Types sde Windows 3 08-19-2003 06:38 PM
Would this data be of use too you? SiteTutor Lounge 3 08-02-2002 06:58 AM


All times are GMT -8. The time now is 08:29 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting