ganbree 7 Light Poster

I'm trying to advance my understanding of how computers and compilers work.

Given the C++ program bellow with exceptions and runtime typing turned off and no optimizing that removes main or makes it into a non-standard function.

int main() {return0;}

Produced an disassembly with the following code for main:

pushl	%ebp ; pushes the stack frame
xorl	%eax, %eax  ; sets the eax register to zero
movl	%esp, %ebp  ; empties the stack frame
leave
ret ; return with 0 in eax

1. What will the assembly that calls the main function look like?
2. What will be on the start before this code is run?
3. What will EBP (the stack frame pointer) be at the start? Garbage values?
4. What is in the accumulator at the start? Garbage values again?
5. What does the leave function do? I've read around for this but I can't find anything that makes much sense.
6. To what address will ret jump the instruction pointer to?
7. What does the assembly look like for the code that is executed the main function exits?
8. Another thing that is being getting at me is: why does C++ CDECL calling convention pass argument right to left? It seems counter intuitive to me as it rewards doing right to left as the order of evaluation of the parameters which is also counter intuitive to me.

Thanks in advance, any help would be much appreciated

ganbree 7 Light Poster

Okay,

using #include "anotherfile.cpp" does cause naming conflicts, because it will cause multiple files to contain copies of definitions for the same function, use header files to declare functions. Also build with separate compilation and linking stages.

If you want to compile from the command line look into makefiles or something similar.

ganbree 7 Light Poster
int main()
{
	system("dummy text afgfdhhgfhjjg");
}

1. Compile this
2. Searching the executable for the string literal and note the offset
3. Use a binary fstream to overwrite that section in the executable producing executable.
4. Hope that overwriting with a larger string won't go over anything important.
5. Either PROFIT or RUNTIME ERROR.

:p

ganbree 7 Light Poster

First, that is a scary amount of source to post as an example.

I wouldn't use DLL for this, I would use a scripting language such as Lua or Python (actually I wouldn't because I hate these languages with a vengeance). There should be one appropriate for the task to hand.

Now, your use of #define STLmap std::map I find particularly disturbing, what were you thinking? Maybe you've forgotten how to use that 'using' statement, defining the string almost could be defended for swapping between std::string and std::wstring; however; use typedef, seriously!

What's with prefixing your classes with YAMP_ learn to namespace. You give other people, if other will use this, the choice between typing YAMP a million times or not, your way may not be everyone's; if they really want to type YAMP_classname then they can use typedefs like you seem to like doing.

I'll try to get back on topic now...

If you want to go down the DLL path:
I'm not exactly sure what can cross the DLL barrier, but look into functors instead of the function pointers. With the memory allocated thing I think the DLLs belong to a separate process and you may need shared memory things, I never use DLLs if I can help it. Unless this is an exercise in creating a monstrous hybrid of interconnected DLLs and interprocess dynamic dispatch then I'd advise against pursuing this path. If that is the aim of this then forget …

ganbree 7 Light Poster

Try the compiler flag.

--enable-threads

I don't use gcc regularly so I have no idea what -D_GLIBXX_DEBUG does, this might or might not solve the problem.

Thanks in advance; in advance, taken. ;)

ganbree 7 Light Poster

so... what's so cool about ressurecting dead threads?

ganbree 7 Light Poster

So they're being invalidated.

My first thought was to create a 'smart' iterator that wouldnt be invalidated. But to do this it'd need to traverse the structure back from the stack allocated point; because these are vectors of objects containing vectors etc. that could be hard to write, the structure currently has theoretically unlimited depth, and the iterator would need to know what goes inside what, which I suppose could be done with some nasty RTTI.

I'm currently thinking that the solution is to use pointers, replace the vectors to objects with vectors to pointers to objects. Then instead of taking references I could copy the pointer value. Which wouldnt be moved around by vector. This'll probably lead to memory leaks.

Last option is changing the container type. I think std::list might be good but I need quick access by an index.

ganbree 7 Light Poster

Okay, I've got a serious problem with std::vector, the code I'm working with is huge and it's got vectors left, right and center. The problem is that one of the vectors is having to reallocate itself elsewhere. This is causing the references to point to random things.
For illustrative purposes:

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	vector<int> Vector1;
	Vector1.push_back(12);
	Vector1.reserve(1);

	std::vector<int>::reference Reference = Vector1[0];

	cout << Vector1.capacity() << " " << Reference << endl;

	vector<int> Vector2;
	Vector2.push_back(11);
	Vector1.reserve(2);

	cout << Vector1.capacity() << " " << Reference << endl;
	return 0;
}

Ive tried using int& , const int& , std::vector<int>::reference and the various iteratorts; none of them seem to be able to remain valid through the relocation. I cant think of any means to get around this, what should I do?

ganbree 7 Light Poster

Just... please no.

ganbree 7 Light Poster

You should check whether the program is in the same directory as the Input.txt file, if you're using an IDE, your executable is probably stored in another directory :)

No its definitely in the same directory as the executable, I said I've been using XCode twice now.

I can only say that I tried it on Windows and it did work perfectly :)

Which is why I said that it's not a problem with the code.

Anyway, I think I've found the solution to my problem here:

http://www.mail-archive.com/xerces-c-dev@xml.apache.org/msg15355.html

ganbree 7 Light Poster

OK....
1. The file definitely exists and is not empty.
2. It works when running with Xcode but not when executed through Finder. I don't think this is a problem with the code, but something else.
3. I'm opening the files using the constructor so adding a call to open() will fail.

ganbree 7 Light Poster

I'm trying to port some existing C++ code to OS X.
The problem is that it doesn't want to load files to read from them. The code bellow illustrates this problem; when run from Finder it outputs "Failed to open input file.", however when run from Xcode it runs correctly. Thanks in advance.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	cout << "Hello World!" << endl;
	ofstream Output("Output.txt");
	if(!Output.is_open())
	{
		cout << "Failed to open output file." << endl;
		return 1;
	}
	Output << "Output";
	Output.close();
	ifstream Input("Input.txt");
	if(!Input.is_open())
	{
		cout << "Failed to open input file." << endl;
		return 1;
	}
	
	string Line;
	while(!Input.eof())
	{
		getline(Input, Line);
		cout << Line << endl;
	}
    return 0;
}
ganbree 7 Light Poster

I'm trying to read a Unicode file using std::wifstream. On wintel.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	wifstream File("input.txt");
	wstring Line;

	while(!File.eof() )
	{
		getline(File, Line);

		wcout << Line << endl;
	}
	system("pause");
	return 0;
}

And this is my output

 ■A   s e q u e n c e   o f   l e x i c o n s   t h a t   m a k e   u p   a   s
e n t e n c e .
Press any key to continue . . .

Now the spaces between each character and the '■' thing at the start arn't in the text file. What am I doing wrong?

ganbree 7 Light Poster

At those points in the code you are sending a pointer to a pointer to an object when the function/method wants a pointer to an object.

This looks like you want to send a particular element of the array but you are passing a pointer to the entire thing.


(Arrays are pointers)

ganbree 7 Light Poster

Please note that you must create a copy constructor! Because else if you do Object = Object2 then they will share a critical section and will both try to dealocate it.

vijayan121 commented: right! +7
ganbree 7 Light Poster

It is almost guarantee that you do not have a virus. The fact is that you can send emails that look like they come from anyone you want, it's great fun. SMTP and POP have no authentication of senders, there are protocols that do but their use is not widespread. If the message is actually coming from your computer then you probably should delete the application data of the app that is sending it.

ganbree 7 Light Poster

Ok, on windows you can use GDI, DirectX and OpenGL to create games.

GDI should not be used for any games in my opinion, it is not hardware accelerated and on vista due to the software / hardware bliting it has become even slower! If you try to make a game in this you will quickly hit the wall. GDI is meant to be used for creating windows applications, not games.

DirectX is only availiable on Windows and XBox, DirectX isn't in my opinion as good as OpenGL however OpenGL has a horrible plugin system. OpenGL can do more but DirectX can do more nativly. The plugins are really inconveniant requiring differant versions for each graphics card manufacturer. OpenGl can be used on Linux, Mac, Wii, PS3 in addition to windows. So thats another advantage.

OpenGL is implemented by differant vendors, therefore some are open source and some are closed source. The capabilities of the card are of coarse only known when the program runs. GDI is completly seperate, however if you want so some reason you can hybrid DX and GDI but the hardware/software swap is slow.

ganbree 7 Light Poster

Your printbasket function seems to be fine.

This is not a solution but it would make it easier to read if you used indention to show scope.

The problem does not appear to be in printbasket.

Look at fish::printme. Are the different fish classes derived from the fish class? Calling printMe() from a Fish pointer will call the fish handler if no inhertitance is used. You obviously want it to call fishtype::printme()

This is what you need, if you don't already know about virtual methods and inheritance you should read about it.


class Fish
{
public:
virtual void PrintMe()=0;
};

class FishType : public Fish
{
virtual void PrintMe();
};

ganbree 7 Light Poster
cout << "Line1 in source"
       << "Line2 in source"
       << endl; // new line in output
ganbree 7 Light Poster

I'm trying to devise some base classes for a large system
So why doesn't this work?

#include <iostream>

using std::cout;
using std::endl;

#undef GetObject

class IObject
{
public:
	IObject()
	{
		References = 0;
	}
	unsigned int GetRefCount()
	{
		return References;
	}
private:
protected:
	unsigned int References;
	friend class CHandle;
};
class CHandle;
class CHandle
{
public:
	CHandle()
	{
		Reference = NULL;
	}
	CHandle(IObject* In)
	{
		Reference = In;
		// Incremnet the number of references
		(Reference->References)++;
	}
	CHandle(IObject& In)
	{
		Reference = &In;
		// Increment the number of references
		(Reference->References)++;
	}
	CHandle(CHandle& In)
	{
		// copy the pointer
		Reference = In.GetObject();
		// Increment the number of references
		(Reference->References)++;
	}
	CHandle& operator=(const CHandle& source)
	{
		if(Reference != NULL)
		{
			if(--(Reference->References) == 0)
			{
				cout << "Deleting Object" << __LINE__ << endl;
				delete Reference;
			}
		}
		Reference = source.GetObject();
		return *this;
	}
	~CHandle()
	{
		if(Reference != NULL)
		{
			// decrement References then test whether to delete object
			if (--(Reference->References) == 0)
			{
				cout << "Deleting Object" << __LINE__ << endl;
				delete Reference;
			}
		}
	}
	IObject* GetObject()
	{
		return Reference;
	}
protected:
private:
	IObject* Reference;
};

class Rectangle : public IObject
{
public:
	 Rectangle()
	{
		width = 0;
		height = 0;
	}
	 Rectangle(unsigned int Width, unsigned int Height)
	{
		width = Width;
		height = Height;
	}
	void SetSize(unsigned int Width, unsigned int Height)
	{
		width = Width;
		Height = Height;
		return;
	}
	unsigned int GetHeight()
	{
		return height;
	}
protected:
	unsigned int width, height;
private:
};


int main()
{
	CHandle * …
ganbree 7 Light Poster

you can not do MFC with the Express edition of the compiler because the libraries are not included in the Windows PSDK. You need the Standard or better version.

is CreateWindowEx() MFC? I always thought so; I might be wrong :$

ganbree 7 Light Poster

I think I've almost solved this one:-

I've added the /ENTRY switch as /ENTRY:WinMainCRTStartup.

Problem is that I don't want any CRT in my application, do you know if that value with add any? I'm looking for any native/MFC ones

ganbree 7 Light Poster

Sorry but it doesn't seem it be working that way, as that is what I have in effect done.

LINK : fatal error LNK1561: entry point must be defined

I need to define the entry point as inside the library.
Its probably cryptic but not complex.

ganbree 7 Light Poster

But in case you don't have a book...

return can only be passed one value. However it could be an array if they are of the same type.

ganbree 7 Light Poster

I'm using Visual C++ 2005 Express.

In my solution I have 2 projects, a static library project and a executable project.

The problem starts because I need WinMain() inside the static library to remove almost all the platform specific code from the executable project.

How you set the entry point on the executable project to the WinMain() inside the static library?

ganbree 7 Light Poster

All, i can say is that this is shamefull, it takes me 3 hours to find out that the device is lost in DX9 when windows are switched, I had searched everywhere! I found it on a Managed C# Forum and im writing in Native C++ even if the solution they posted was as good as useless the idea behind it I could embedd into my existing code!!!

ganbree 7 Light Poster

:sad: it seems i'm being ignored, I wonder what I'm doing wrong??

do you believe I am not doing any work myself?

do you not beleive that this is now homework of any sort!?

am I being discourtious? I havn't claimed my query was more important than anyone elses, although this seems to get a responce although yu dont seem to like it! I admit i didn't use Please or Ty :sad:

Is this thread too difficult? *hint* *hint*

have I given too little detail, too little code?

should this be moved into a differant forum?
I beleive that "Game Developement" may have been more appropriate but I did not know of its existance then.

(maybe the real reason I made this post has as a shameful BUMP post)

ganbree 7 Light Poster

Ive been writing a DirectX application for some time and there has been a bug that I have been unable to fix, but this is to do wth the Windowsx API.

I can run my DirectX application (Fullscreen) fine, but when I Minimise it then restore, fullscreen mode does not return.

This isnt homework btw!

#include "main.h"

// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    HWND hWnd;
    WNDCLASSEX wc;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.lpszClassName = "GameClass";

    RegisterClassEx(&wc);

    hWnd = CreateWindowEx(NULL,
                          "GameClass",
                          "GameDemo",
                          WS_EX_TOPMOST | WS_POPUP,
                          0, 0,
                          SCREEN_WIDTH, SCREEN_HEIGHT,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

    ShowWindow(hWnd, nCmdShow);

    G_Init(hWnd);


    MSG msg;

    while(TRUE)
    {
        DWORD starting_point = GetTickCount();

        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
                break;

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        G_Render();

        // if pressing ESCAPE send WM_DESTROY
        if(KEY_DOWN(VK_ESCAPE)) {
            PostMessage(hWnd, WM_DESTROY, 0, 0);
        }

        while ((GetTickCount() - starting_point) < 25);
    }

    G_Clean();

    return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            } break;
        case WM_SETFOCUS:
            {
// what to put???
            } break;
    }

    return DefWindowProc (hWnd, message, wParam, lParam);
}

Also note that the background colour of the window is WHITE a WHITE empty box appears in the top left covering a the same distance as SCREEN_WIDTH and …