Hottest Forum Q&A on CodeGuru - December 29th


Lots of hot topics are covered in the Discussion Forums on CodeGuru. If you missed the forums this week, you missed some interesting ways to solve a problem. Some of the hot topics this week include:

How do I port VC++ code to Linux? (top)


vir_123 wrote some code in VC++. Now, he needs to convert that code for a Linux OS. How can he do that?

I have developed VC++ applications on the Windows platform.
I want to convert it to the Linux platform. Is any cross complier
method to convert it to the Linux platform? Please suggest an easy
method to do so.

Several questions come to mind before you can convert any VC++ code to a Linux OS.

  • Is your program graphical?
  • Do you use MFC?
  • Do you use ATL?
  • Do you use straight WIN32 functions?
  • On what system components does the program rely?
  • What does the program do?

Porting an application from VC++ can be easy, but you have to be careful that you use APIs that can be run on both OSes (Windows as well as Linux). If you still want to use Microsoft APIs, you can try WINE (a Windows emulator). Or, you should use wxWindows, a cross-platform GUI API. See the Web site for more info.

How can I use STL with Linux? (top)


This is an another Linux question. Here, KnNeeded needs to run his simple "hello world" program on a Linux system. But, unfortunately, he did not know how to use the STL in Linux. Do you?

I am trying to port my current C++ and C skills to Linux
programming. I have successfully created the standard Hello World
starting project in C for Linux, using both files and standard

I am trying to do something simular using the standard C++
libraries. I tried to use the iostream template class in the STL
library. I don't whether it is supported in Linux; however, I can
seem to use #include <iostream> without a linking error being
generated. The linker throughs an error "Undefined reference" when
I write the complete Hello World program:
#include <iostream>
using namespace std;

int main(void)
    cout << "\n Hello World";
I am at the moment thinking the problem is that I am not linking the
program with the required stl library. However, I have no idea how
to do this in Linux.

I have look through the standard included documentation but cannot
find a reference of the libraries included in Linux (Red Hat 8.0).
Does anyone know of a good reference on the C++ libraries in Linux?
Is this my problem or does Linux not support STL?

I am new to Linux programming so I am at the moment only really
looking for a good reference site. Thanks in advance.

You can download the STLport here for free. An installation instruction is included.

How do I make a function that allocates memory for you? (top)


yiannakop does have some problems with his function. He is trying to allocate memory in a function; it should not be that difficult. But, the problem is in passing the parameters to the function. Here is his problem:

Suppose I want to implement a function like this:
void function_name(int length, short *array)
  array = (short*)malloc(sizeof(short)*length);
  // do something
This, unfortunately, doesn't work. To make it work, I must place the
allocation line ( array = (short*)malloc(sizeof(short)*length ) in
my main(), just before calling the function. Why is this happening?

MrViggy presented the solution very well. Here is what he says about that problem:

If you are trying to get the function to allocate memory for you, you either need to pass the array pointer by value, or pass in a pointer to the pointer to the array. Currently, you are passing the array pointer by reference; therefore, the function will not be able to modify it.

Pass by value:

void function_name(int length, short* &array)
  array = (short*)malloc(sizeof(short)*length);

Or, pointer to pointer:

void function_name(int length, short **array)
  *array = (short*)malloc(sizeof(short)*length);

int main()
  short *myShort;
  function_name(3, &myShort);

How can I create a 3D vector? (top)


AfricanDude knows how to create a 2D vector, but unfortunately, he has some problems with creating a 3D vector.

I am trying to use the STL vector multi-dimenstional. I know how 
to do 2D, but I want to use 3D.

This is what I have tried:
vector < vector <vector <int> >> m(3, vector< vector<int>
I also tried the following:
vector < vector <vector <int> >> m(3, vector<int> (5, vector<int>
I get the similar error as shown above. Can you please shed some
light? Also, is there another way to avoid using a 3D vector if I
want to use it for the following?
int i,j,k;
float a[i][j][k]=value;
Of course, I would like to vary the size of array dynamically.
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\
vector(357): error C2664: 'std::vector<_Ty>::_Construct_n' :
cannot convert parameter 2 from 'int' to 'const std::vector<_Ty> &'

The answer was provided by Paul McKenzie:

Use typedefs:

#include <vector>
// To much work!!
std::vector < std::vector <std::vector <int> > >
m(3,std::vector< std::vector<int> >(4, std::vector<int>(4,0)));

// Easier to maintain
typedef std::vector<int> Int1D;
typedef std::vector<Int1D> Int2D;
typedef std::vector<Int2D> Int3D;

int main()
   Int3D My3DArray(10, Int2D(10, Int1D(10,0)));

The reason for the typedefs is that, at some point, you may need an iterator. When you define the iterator, what's easier?

std::vector< std::vector< std::vector<int> > >::iterator it;


Int3D::iterator it;

How do I return a vector from a function? (top)


halmark6Z is new to C++. His background is actually Java. But, now he needs to write some C++ code in which he wants to return a vector in a fuction.

I'm rather new to C++ (I'm changing from Java to C++), so please
bear with me. I have a question about STL and returning a vector
array from a static method.

How do I do it correctly, so that the function contructs a vector
that can be returned to the caller? I know if I create a local
object from a class and return its reference, it will point to null
(if I'm right) after the function call. So, the object should be
created with the new operator. But, how do I do it with a template
call? I just get errors when I try to compile.

It should go something like this (please, do correct my syntax.
Pointers and references are used very wrong, I think):
static &vector<MyClass> loadFromFile()
  vector<MyClass> *data = NULL;
    // load from file and populate the vector
  { }
  return &data;

The perfect answer is again provided by Paul McKenzie:

A vector has proper copy semantics, so there is no need to use references or pointers.

#include <vector>
class MyClass

static std::vector<MyClass> loadFromFile();    // if you really need
                                               // a static function

std::vector<MyClass> loadFromFile()
  std::vector<MyClass> data;
    // load from file and populate the vector
  { }
  return data;

int main()
   std::vector<MyClass> M;
   M = loadFromFile();

The code is much more easier than you thought it would be.

I know if I create a local object from a class and return its

Return it by value (as my code does above), not by reference.

It will point to null (if I'm right) after the function call. So,
the object should be created with the new operator.
You're right, you do come from a Java background. You do not need to create an object with "new" in C++.

About the Author

Sonu Kapoor

Sonu Kapoor is an ASP.NET MVP and MCAD. He is the owner of the popular .net website DotNetSlackers publishs the latest .net news and articles - it contains forums and blogs as well. His blog can be seen at:


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds