30 Mar 2005 mchirico   » (Journeyer)

C++ Tip

I picked up a copy of ``C++ Common Knowledge: Essential Intermediate Programming'' by Stephen C. Dewhurst, 2005. I like his example (Item 6: Array Formal Arguments), dealing with array formal arguments. In fact, there are no array formal arguments. Arrays decay to a first element pointers. What does this mean? It's not possible to find the size of an array, that is passed to a function with ``sizeof(array)/sizeof(Array[0])'', because sizeof(array) finds the size of the pointer, and not the array. Yes, when you create the array, you can find the size of it; but, when it decays to a pointer, which it does when you pass an array to a function, you cannot use this method. Don't take my word for it. Try it.

Dewhurst gives an interesting way around this - I've added initialization of the array as well.

#include <iostream>
#include <string>
using namespace std;

template <typename T,int n, typename T2> int give_size_init(T (&array)[n],T2 init_val) { int i; for(i=0; i<n; i++) array[i]=init_val; return n; }

int main(void) { int a[13]; string s[15]; string s_init; cout << "Size of array is " << give_size_init(a,5) << endl; cout << "Size of array is " << give_size_init(s,"ta da") << endl; s_init="more"; cout << "Size of array is " << give_size_init(s,s_init) << endl; return 0; }

OpenSSH and PuTTY This article was updated that details connecting beyond a firewall. In addition, I rarely see people using the config option with openSSH, which is peculiar, since it's convenient and powerful. Also the RemoteForward option for presenting remote websites through multiple firewalls, is my favorite option.

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!