Hottest Forum Q&A on CodeGuru - October 20th


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 send a char* as an out parameter? (top)


avi123 is a regular visitor to CodeGuru. He needs to send a char* as a parameter to a function but unfortunaly he does not know how to initialize a char so that it is accepted in the function.

I need to send a char * as an out parameter.
I have the function.
BuildString(char * myNewString)
    // build here the myNewString using std:string
    // and then cast to char *
I don't know the size of the string when I'm calling the
function, this is why I use char * and not char x[MAX_SIZE] also
the out value must be char * and not std:string I'm using
std:string inside the function for convinice.

Anyway my question is:
How do I define this char * before calling the function
I can't just define it char *myNewString
so I used "char *myNewString = NULL".

Is that legal?

Paul McKenzie is also a regular contributor on CodeGuru and suggests sending the pointer. The following code that Paul provided works without any complications and shows in a nice way to define a char* before calling a function.

#include <conio.h>
#include <cstring>
using namespace std;

void BuildString(char **myNewString)
     *myNewString = new char [10000];    // or whatever
     strcpy(*myNewString, "abc123");
     std::cout << *myNewString;
int main()
    char *MyString;
    BuildString( &MyString );
    // now MyString contains the new string "abc123"
    delete [] MyString;

    // wait for keystroke

Why should I avoid testing float and double variables? (top)


jiac does have problems with his application. His application does run well in the debug version but not in the release version.

This problem has been causing me pain for a while. I hope someone
can provide me help. I am seeing that for the below code, on the
debug build, it is fine, but on release build it fails. I am using
VC 7.0.
int index - 1;
float *Range;
float min = 1.50f;
float increment_step = 0.1;
Range = new float [steps];
Range[0] = min;

while (index<= steps) {
    Range[index] = Range[index-1] + increment_step;
Later I want to check the index of a given value in Range[].
for ( int i = 0; i < index; i++ ) {
    printf (" values %f %f \n", value, Range[i]);
    if (value == Range[i] ) {
        index = i;
The problem is, I can see in the for-loop, the values do match,
but it never jumps to the if condition.

Well, can you see the problem? The problem is that you should never compare:

  • float with float
  • double with double
  • double with float

Instead of comparing two values like this:

double a = 1.0;
double b = 1.0;

// ... Some math here

if(a == b)
  // May never be called, even if a *seems* equal to b

You should define a maximal acceptable difference for two doubles to be considered equal:

const double maxdiff = 0.000001;

if(fabs(a-b) <= maxdiff)
  // ...

How do I convert a double to string? (top)


MikeB want to convert a double to string—i.e: 25.875 to 25'-10 1/2". Although he has already found a solution, he still wants some suggestions regarding any improvement for the code.

I need to convert from a double value to an architectural
string format. EG: 25.875 ->> 25'-10 1/2"

To do this, I have written the following code, but I am not sure
about it although it seems to work.

I thought I would post the code for any possible improvement

Code Removed!
On second thought, I will attach a CEdit derived class I 
use that converts the value from a real (double) to a string 
(in imperial) as required.

Here is the CEdit derived class: Download

I don't see any requirement for a derived CEdit class to convert a double to string. Instead of using a derived class, it is better to make this a general C++ class.There is no need for any GUI involvement such as CEdit.

Something like this:

#include <string>
#include <sstream>
#include <iostream>

std::string ArchitectureFormat(double d)
    std::ostringstream strm;

   int numft      = (int)d;
   double frac    = d - (double)numft;
   double dInches = frac * 12.0;
   int nInches    = (int) dInches;

   // the fractional part for inches was not done.  This is a
   // little bit harder to do.  But here is the output.

   // code to convert
  strm << numft << "'-" << nInches << "\"";
  return strm.str();

int main()
    std::cout << ArchitectureFormat(25.875);

You then call this function whenever you want an architectural string, regardless of whether it is going to a CEdit, the console window, a file, or to the printer.

How much memory does my application consume while running? (top)


atif_ilm asked a very interesting question. He wants to know how much memory a Windows application takes.

What is the maximum memory a Windows application, can take?
Actually, I am developing a server which is going to consume huge
amounts of memory, so I want to know what max I can get in Windows.

And how can I judge how much memory , my server is consuming at a
particular time .

So if Current Consumed >= Total Possible Memory I will simply stop
accepting new clients.
What API might help me?

Normally, you should design the process of acceptinh a client in a commit-or-rollback fashion. Then, if during the process you exhaust the memory, you can roll back the process. Because most modern operating systems are multitasking, you cannot assume that a memory snapshot you take at the time T will still have any value at the time T+t. Thus, taking a snapshot and assuming that you will have enough memory is risky.

According to the (Address Windowing Extensions) AWE from MSDN, your application program can take up to 2 Gb of physical memory (if available). But, a 32-bit-type processor can address 4 Gb of virual memory. So, every application gets 4 Gb of address space for virtual memory. Usually, this memory divides 2 Gb for user address space + 2 Gb for kernel address space (3:1—another possible case).

How do I reference **ptr_xx to ptr_yy [xx][xx]? (top)


James Goh is working with a dimension array, but unfortunaly he get some errors in his code. Here is what he says:

I would like to know how to reference double dimension 
array with double dimension pointer. 
( I think this may sound silly to someone here )

I have tried to reference one dimension array with pointer without
a problem,
e.g. char xx[100]; char *yy;
yy = xx;
sprintf(yy, "You are a nice guy");
// Good result
However, if i try with 2 dimension pointer to 2 dimension array.
char xx[100][500];
char **yy;
yy = xx; 
// Not working - cannot compile
char *xx[100];
char **yy;
yy =xx;
// can compile - but it gives error during excution
Is there any method to hv the address of pointer same as array 
declared for 2 dimension memory. I do not/cant use "malloc" and 
"new" due to some reason, can anyone help to answer to my doubt?

First, look at "std::vector," which is much easier and safer to work with than raw C-style arrays. In any case, an array name always decays to a pointer to the first element of the array. Given your example, then:

char xx[100][500];

What is stored in this array? Well, another array, actually. That is, there are 100 elements in your array, each of which is an array of 500 chars. So, because the name "xx" decays to a pointer to the first element of the array as mentioned above, and that element is an array of 500 chars, you're looking at this:

char (*yy)[500] = xx;

"yy" is a pointer to an array of 500 chars. The syntax is brutal, but the underlying idea of an array that stores another array is elegant (and you can carry this idea to any number of dimensions). It takes a little practice to think about it this way (draw your elements out on paper to see), but it becomes self-evident after a short time.

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

  • Featuring Art Schoeller, VP and Principal Analyst, at Forrester Research Live Event Date: November 9, 2016 @ 10 AM PT / 1 PM ET Being "proactive" with your customers is not enough to survive in the market today. The truth is, organizations that understand how to engage customers on their preferred channels will create contextual, and relevant experiences for customers. Plus, they will see the financial impact of nurturing long-term customer loyalty. Join our guest speaker Art Schoeller, VP and Principal …

  • The open source cloud computing project OpenStack has come a long way since NASA and Rackspace launched it in 2010. Backed by leading technology infrastructure providers including Cisco, Dell, EMC, HP, IBM, Intel, and VMware, OpenStack underpins significant workloads at an increasingly diverse set of organizations, including BWM, CERN, Comcast, eBay, and Wal-Mart. For CIOs engaged in broader programs to win, serve, and retain customers -- and refocus business technology (BT) spend -- a planned and pragmatic …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date