Tag Archives: tip

Collapsing two arrays into one in C and C++

A quick tip – if you have two arrays and you want to merge/collapse/concatenate/append/combine (whatever you want to call it) them into one here is one way to do it in C and C++. The code also prints the combined array to check it’s correct.

Note the subtle differences between C and C++. One of the (innumerable!) problems with C++ is that you can write as much C as you want in it and leave the next developer wondering why you were writing C instead of C++ and whether you had specific justifiable reasons to do so in each instance. I personally also consider it bad practice. This is one of the reasons I dislike hybrid languages – they allow you to blend two languages together in arbitrary proportions making the end result unreadable and unmaintainable and just raising more questions than the problems that it solves.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {

   double a[4] = {1,2,3,4};
   double b[4] = {5,6,7,8};

   double* c = malloc(8 * sizeof(double));

   memcpy(c,     a, 4 * sizeof(double));
   memcpy(c + 4, b, 4 * sizeof(double));

   for (int i = 0; i < 8; i++) {
      printf("%d %fn", i, c[i]);




#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
int main () {
   double a[4] = {1,2,3,4};
   double b[4] = {5,6,7,8};
   double* c = new double[8];
   copy(a, a + 4, c); 
   copy(b, b + 4, c + 4); 
   for (int i = 0; i < 8; i++) {
      cout << i << " " << c[i] << endl;
   delete[] c;

Note that in the C++ program:

  • I’m not using includes from C
  • I’m not using memcpy from C
  • I’m not using printf from C
  • I’m not using malloc from C
  • I’m not using sizeof from C
  • I’m not using free from C

Compiling Java 1.6 projects using Maven on Mac OS X

The following maven pom file configuration works on Windows and Linux but not on Mac OS X.


The error that occurs is as follows.

Caused by: org.apache.maven.plugin.CompilationFailureException: Compilation failure
Failure executing javac, but could not parse the error:
javac: invalid target release: 1.6

The reason is that the following environment variable isn’t set by default.

export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home"

You can declare this either as a one time export in your current shell above, in /etc/profile as I normally do or in any other startup file of your choice. Afterwards be sure to refresh your shell before you try again. This can be done either by closing your current terminal window and opening a new one or by sourcing whichever file you’ve put the above information into.

source /etc/profile

In order to make sure that it has taken effect one can output it in the shell as follows.


If it shows nothing then the shell has not picked up your change.

If your tomcat had already been started prior to setting the environment variable then don’t forget to restart it in the new shell otherwise you will get the following error on startup when deploying your maven application.

Caused by: java.lang.UnsupportedClassVersionError: Bad version number in .class file (unable to load class service.ServiceImpl)
	at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1851)
	at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:890)
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1354)
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
	at org.springframework.util.ClassUtils.forName(ClassUtils.java:211)
	at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:385)
	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1138)
	... 40 more

Check that your compiler and loader are using the same version of Java.