Como detectar erros obscuros com o new e delete do C++

O Adonai Silveira Canez, teve uma dúvida na lista cppbrasil sobre um erro obscuro, que ocorria as vezes, ou de vez em quando no programa dele. Read More »

  • Share/Bookmark

I keep running into long term c++ programmers who refuse to use exceptions

Original at: http://groups.google.com/group/comp.lang.c++.moderated/msg/49c4790c9fec5ce1 Read More »

  • Share/Bookmark

C: Comments on Declaration/Definition Syntax

From: C: An Advanced Introduction” (Narai Gehani) – 1984 – ISBN 0-88175-053-0

The syntax for declarations and definitions in C is similar to the syntax used for accessing values of these objects; that is, it is similar to the syntax used to represent object values in expressions. This is an important difference between C and languages like ALGOL 68, Pascal and Ada, where the syntax used for declaring object types reflects the structure of the type. Declarations and definitions in C are sometimes hard to read and understand, particularly if they involve compound-type expressions that contain the pointer dereferencing operator *. The primary reason for this difficulty is that the dereferrencing operator is a prefix operator, while all the other operators used in declarations and definitions are postfix operators. The reader unaccustomed to programming in C may at first find it hard to understand definitions and declarations such as


int *(*(*x)[6]()
char (*(*(y())[])(); /* take form ANDE80 */

where

1. x is defined to be a pointer to an array of 6 elements each of which is a pointer to a function returning a pointer to an integer object.
2. y is declared as a function that returns a pointer to an array of pointers to functions that return character values.

The reader will find it easier to undestand C declarations and definitions if he/she keeps in perspective that an object declaration or definition looks very much like the way the object will be referenced in an expression.

  • Share/Bookmark

Simple string manipulation in C++

The code bellow is a simple example, howto pass string to a function.


#include
#include
using namespace std;
void display(string a, string b, string comp){
cout << "The word \""<< a << "\" is " << comp << " the word \"" << b << "\"" << endl;
}
int main(int argc,char **argv){
if(argc < 3){
cout << "Please type two words after the program name." << endl;
}else{
string a = argv[1];
string b = argv[2];
if(a < b){
display(a,b,"less than");
}else if(a > b){
display(a,b,"greater than");
}else if(a == b){
display(a,b,"equal to");
}
}
return 0;
}

  • Share/Bookmark

Setting up to compile C++ on Linux

Firts, check if you have g++ instaled.
Run this on console:

g++ -v

If show version, g++ installed.

Now, create a simple shell script to compile you cpp file

#/bin/bash
# gpp compiller
g++ -pedantic-errors -ansi -Wall -o $1 $1.cpp

Now, to compile the str.cpp use this:

gpp str

  • Share/Bookmark

Generate a Certificate Signing Request (CSR) on CentOS

Follow this steps

openssl genrsa -des3 -out elmaqu.es.key 1024

Now, you have the RSA PRIVATE, let’s create de CSR…

openssl req -new -key elmaqu.es.key -out elmaqu.es.csr

Don’t type additional password. Yum must fill all fields correctly.

Now, cat elmaqu.es.csr and pest on website

  • Share/Bookmark

Install imagemagick with PHP imagick extension on CentOS

To install imagemagick with imagick extension on centos follow these steps:

yum install -y ImageMagick ImageMagick-devel imagick

After, include file for imagick.so module and restart apache:

echo “extension=imagick.so” > /etc/php.d/imagick.ini
/etc/init.d/httpd restart

  • Share/Bookmark

Install GeoIP on CentOS

This is a very simple step-by-step..

1.Install geoip via yum: yum install geoip geoip-devel

2.You also might want to download database with ip addresses from Maxmind website and place it in /usr/share/GeoIP (which is a default location of geoip upon installation).
3.Install PECL extension: pecl install geoip

4.Add extension=geoip.so to your /etc/php.ini

5.Restart: apachectl restart

  • Share/Bookmark

Using in memory caching on PHP. The easy way.

With memcached you can store array and other data in server memory. For example, you can use memcache to store data from a mysql_query in memory to expire some time in future. Using memcache you connect to database or other resource only when needed.

I’m using memcache to store mysql querys to reduce connection on database, but, you can use to store ldap, imap and any other data type. But careful with complex objects like XML, some times the object get corrupted, becouse XML use very complex in-memory data structures.

Here is two function use to simple put and get data in memcached daemon:

// connect to a memcache server on localhost
if( class_exists('Memcache') ){
	$memcache = new Memcache;
	if(!@$memcache->connect('localhost', 11211)){
		unset($memcache); // no valid memcache connection
	}else{
		define("memcache",true); // enable memcache use
	}
}
 
// store data on cache memory for five minutes (default ins 300 seconds)
function cache_set($key,&$data,$ttl=300){
	global $memcache;
        // here is the trick, set timestamp in the future.
	$ttl = strtotime("+{$ttl} seconds");
	if(!defined("memcache")) return false;
	return $memcache->Set($key, $data, false, $ttl);
}
// get pointer for data in memory, if expired return false
function &cache_get($key){
	global $memcache;
	if(!defined("memcache")) return false;
	return $memcache->Get($key);
}
?>

And, here how to use the code above:

<?php
 
$my_data = cache_get("my_key");
 
// if the cache has expired, fetch data from database
if( $my_data === false ){
      // connect to mysql and fetch data
      $sql = "select * from mysql.users";
      $tmp_resource = mysql_query($sql) or die(mysql_error());
      while( $tmp_row = mysql_fetch_assoc($tmp_resource) ){
            $tmp_data[] = $tmp_row;
      }
      // now, store tmp_data on memory cache for five minutes (300 seconds)
      cache_set("my_key",$tmp_data,300);
      // link tmp_data with my_data variable
      $my_data =& $tmp_data;
}else{
      // do noting, my_data has been loaded from memory cache
}
print_r($my_data);
?>

How to enable memcache on you phpinstalation.

First, you need to install the service, on Microsoft Windows, you can install the “memcached for Win32″, this port is easy to install and configure. On Linux Centos you can simple use yum, if you have online repository configure, use this command: yum install memcached or if not, you can install manual following tips on ”
Memcached: How do you install memcached? (CentOS 64 bit, Linux, Redhat, Fedora)”. After installation check if service is running, on Microsoft Windows check on taskmanager for the service memcached or on Linux simple type in console: ps ax | grep memcache and check the process.

After installing memcached on you system, you need to enable de php_memcached.dll extension on you Microsoft Windows installation or php_memcachd.so on Linux. Restart Apache and test function.

  • Share/Bookmark

Hello world!

I’m back, after long-long time out of bloging world…

  • Share/Bookmark