Tuesday, August 23, 2005

Segmentation Fault - Need assistance resolving {Novice C++ Programmer}

Hello folks, I seem to have recieved a segfault within my function but
am unsure on how to resolve it. I understand that it means that
somewhere something is trying to access memory that is not prohibited
or not available.

The following function is an overloaded += operator that reads in
strings in a record format seperated by comma delimiters except for the
last data which terminates with a semicolon. I am adding these records
to an array of objects which stores the record. I hope the folowing
code is enough to identify the problem. The static array's are defined
as told in my assignment.

const char* Bank::operator+=(const char a[])
{

char stringTemp[316];
char accountTemp[16];
int balanceTemp;
const char *ptr = a;

sscanf(ptr, "%15[^,],%d,%315[^;];", accountTemp, &balanceTemp,
stringTemp);

savings[size].changeAccountNumber(accountTemp);
savings[size].changeBalance(balanceTemp);
strcat(stringTemp,";");
savings[size].changeCustomerInfo(stringTemp);
size++; //Core dump here

return a;
}
- AMT2K5

can you obtain the C++ source file from the machine code?

i would want to know whether a person can obtain the source code of a c++
program , once he has only the executable with him. ????is that possible
?? as the m/c code is target machine dependent .i really want ot know the
answer to this question.
- maadhuu

Storing objects in a union

I am writing a nested table structure to mimic data returned from a
database (actually, a heirarchical recordset).

I am representing the cells where the actual data is stored, by a union:

class Cell {
union {
char* s ; //std::string s ;
long l ;
double d ;
void *t ;//Table* t ;
}cell ;

......

};

I know objects (with a copy constructor) cannot be stored directly in a
union (anyone knows the *technical* reasons why?). I am "hacking" around
this by storing a pointer to the object instead - is there anything I am
overlooking (i.e. is this dangerous?, any gotcha's I should be aware
of?) - if yes, I would appreciate an alternative solution to allow me to
stor objects (or their pointers) in a union.
- Ann Huxtable

Saturday, August 20, 2005

std::ios::binary?

ยง27.4.2.1.4 Type ios_base::openmode
Says this about the std::ios::binary openmode flag:
*binary*: perform input and output in binary mode (as opposed to text mode)

And that is basically _all_ it says about it. What the heck does the binary
flag mean?
- Steven T. Hatton

Diff Reference b/w Pointer & Reference

What are the major diff betweeb refernce and pointer in C++..
- Radde

final methods/functions

I have a pure virtual f'n bool SaveData(). This f'n is called by
SaveDataWithWarn(). The idea being the implementing class must define
how data is saved, but the base class handles GUI operations, and user
interaction.

SaveDataWithWarn is called by a GUI event trigger. But it is possible
that an implementing class would like to manually call this function.
Thus it must be atleast public. But I don't want the base class to over
ride it. I believ in Java I can put 'final' as a modifier. Is there an
equiv in C++, besides a comment like this:
//over-riding this function will result in a pink-slip.
- Shea Martin

Testing Bandwidth usage of an application

I have a program that I wrote that connects to a remote server and
recieves data at random intervals. I'd like to measure the bandwidth
usage of this program. What is the best way to do this? Should I write
something into the app that can do this... if so, are there any useful
libraries for this purpose? Or, should I maybe look into using a third
party application... if so, can anyone here recommend a good one?
- Marcus

Friday, August 19, 2005

nested friend syntax?

Is this legal and sensible?

class Outer
{
friend struct Inner
{
...
};

...

};

Basically I want to define the struct Outer::Inner and simultaneously
make it a friend to give it access to private details of Outer. My
compiler (gcc) accepts this, but then it also gives Inner private access
to Outer always so I can't tell if it does what I want it to do.
- Mark P

Whats wrong with this copy assignment cstor ?

I've the ff code in cp assignmenent cstor:

PB& PB::operator=( const PB& b) {
if ( this != &b ) {
PB *pb = new PB( b ) ;
this = pb ; // <- Compiler barfs here
}
return *this ;

}

The error msg is : "'=' : left operand must be l-value". I seem to be
getting my references and pointers mixed up. if (this) is a pointer,
what is it pointing to ?. I rember reading somewhere that the (this)
pointer points to the current instance of the class or the "live"
object). If this indeed the case, then I see no reason why I cannot
assign (this) to another valid pointer.

Could someone please point out the correct way to implement this cp
assignment cstor?. Tks
- Alfonzo Morra

"<< hex" vs cout.setf(ios_base::hex)

What exactly is the difference between the hex manipulator and the
following statement: cout.setf(ios_base::hex)?

According to Stroustrup, Third Edition, Section 21.4.4, "once set, a
base is used until reset". For some reason, I interpreted this to
mean that "<< hex" manipulator would cause the base to be set for only the current statement, while the cout.setf(ios_base::hex) statement would cause the base to be set for multiple statements, up to the point where it is reset. The following snippet of code, which dumps an arbitrary data structure, seems to behave EXACTLY THE OPPOSITE WAY. Thanks, Gus ///////////////////////////////////////

#include <"iostream">
#include <"sys/types.h">


using namespace std;

#define DS_DUMP_LEN 10

struct point
{
int x;
int y;

};

struct rect
{
point p1;
point p2;

};

void
dumpMemory(void* ds, size_t size)
{
// Uncomment the next line for the desired behavior
// (i.e. restricting the hexadecimal base just to this function)
// ios_base::fmtflags oldFmtSettings = cout.setf(ios::showbase);
char *dsPtr = (char *)ds;
for(size_t i = 0; i < size; i++)
{
if(!(i%DS_DUMP_LEN))
cout << endl;
unsigned char ch = *(dsPtr+i);
cout << hex << int(ch) << " ";
}
cout << endl;

// Uncomment the next line for the desired behavior
// (i.e. restricting the hexadecimal base just to this function)
//cout.setf(oldFmtSettings);

}

////////////////////////////////////////////////////////////
// The intent is to have a hex base only in the dumpMemory()
// routine, and a decimal base everywhere else.
////////////////////////////////////////////////////////////
main()
{
int temp = 16;
rect myRect = {{3, 5}, {14, 47}};

// The following line seems to have no "lingering" effect
// The output that follows still has a decimal base
cout.setf(ios_base::hex);
cout << "Value @ location 1 is " << temp << endl;

dumpMemory(&myRect, sizeof(rect));

// We used the hex manipulator only in the dumpMemory function,
// but it seems to have left behind a lingering effect, and
// the output that follows continues to have a hexadecimal base
cout << "Value @ location 2 is " << temp << endl;
- Generic Usenet Account

Help on getting started Learning C++

I have been programming for a few years mostly in Visual Basic 6.0 and
Now .NET ( I am MCAD). I am familiar with C syntax from courses I've
taken and such, but I am nowhere near comfortable with writing anything
useful in C++. I have found myself running into a lot of walls due to a
lot of API's that are made for C++ only and I want to learn to program
in c++. Is there any books or any sites that anyone can point me
towards to help me out? Most books and sites I've found are too
rudimentary because they assume you know nothing about programming. I
need something that will sort of bridge the gap for me.
Thanks in advance
- SirPyros

Help for a newbie!

A few of the guys in my department are trying to come up with a menu-driven
program to average grades,among other functions. We are not programmers,
but I think we could muddle thru if we only had a general layout for the
program. We want it to:
Input grades
Print Grades
Print grade average
Print highest grade
Print lowest grade
Sort grades
Exit the program.
From what I have read, this could be implemented as a large "switch", and
each option could be done with a "function". Could someone help with a
gneral layout for the program? Thanks!
- junebug68

DLLs and CallWndRetProc and Timers

I'm using the CallWndRetProc in a DLL to monitor for Explorer Windows.

Basically I'm using this procedure to find the Explorer windows (checking for "ExploreWClass" and "CabinetWClass" classes) and adding their hwnd to an array. The timer (set to 1 second interval) goes through the entire array decrementing a timer for each hwnd. When the timer reaches zero, the window is minimized. The CallWndRetProc uses the WM_CREATE to add the hwnd,
WM_DESTROY to remove the hwnd, and checks the Active/Inactive state. If the hwnd becomes Active, the timer is reset and the window is ignored. When the hwnd becomes Inactive the timer starts decrementing the timer for that hwnd.

Although the DLL works, I'm wondering if I've gone about this the right way.
I've never put a Windows Hook procedure in a DLL before, but it's neccessary for system wide hooking of this kind.

Can anybody see a potential problem with this method?
(Asked by Lindsay)

Can this be done with cout?

#include <"iostream">
#include <"algorithm">
#include <"string">
#include <"fstream">

using namespace std;

/*
i would like to keep the numbers of the output : Power , step etc,
on the same place in the console. But this isnt going to work with
this code... So the idea is that the numbers behind the '=' signs
change without going down in the console. how do i do this ?
*/

int main()
{
int dummie;
cout << "Power = 45\n"; cout << "Step = 1\n";
cout << "Magnitude = 34\n"; cout << "press a digit";
scanf("%d",&dummie); // press key..
cout << "\r\r\r\rPower = 48\n";
// going back to first line and overwrite it.
cout << "Step = 2\n";
// overwrite second line with new data.
cout << "Magnitude = 29\n";
// overwrite last line with new data.
cout << "press a digit"; scanf("%d",&dummie);
// press key.. return(1);

}
// well this doesnt work but how do i change the code to make it work