class SomeClass
{
private:
char * str;
public:
void SetString(char * s){str=s:}
char * GetString(){return str;}
}
...
SomeClass sc;
sc.SetString("something");
sc.GetString();
Strange/stupid error
I've encountered something very strange - when running my program i get an access violation in a very simple function in which something like this just couldn't happen. Take a look:
I'ts impossible to have an acces violation in the GetString() function - and in some places of the program it works fine.
So i wonder if it could be a compiler bug (I'm using VC++ 6.0)?
You need to allocate memory for the string and use memncpy to copy the contents. edit: also it should be const char* as the contents of the string cannot be modified.
Or, better, use std::string
Or, better, use std::string
As it stands right now, he's correct. Those functions alone won't introduce an access violation. Only if he tries to alter the value returned by GetString()
Quote:You need to allocate memory for the string and use memncpy to copy the contents. edit: also it should be const char* as the contents of the string cannot be modified.
Or, better, use std::string
Ehm, i get the access violation INSIDE the function, not when i try to do something with it's result, besides it should work either.
Quote:Original post by GothmogQuote:You need to allocate memory for the string and use memncpy to copy the contents. edit: also it should be const char* as the contents of the string cannot be modified.
Or, better, use std::string
Ehm, i get the access violation INSIDE the function, not when i try to do something with it's result, besides it should work either.
class SomeClass{private: char * str;public: void SetString(char * s){str=s:} char * GetString(){return str;}};int main() { SomeClass sc; sc.SetString("something"); sc.GetString(); return 0;}
Are you telling me this crashes?
With something this simple, you need to show us the real code. It's not going to be a compiler error. Ahem.
I have two classes in which i get that error:
First, a loist template class, i get the error in the get_beginning() function.
And the second, the error occurs in he GetString() func:
First, a loist template class, i get the error in the get_beginning() function.
template<class Type>class list{ ... public: class iterator { private: _node<Type> * node; bool error; public: iterator():node(NULL){} iterator(_node<Type>*n):node(n),error(false){} ... }; ... iterator get_beginning(){return iterator(base);} ... };
And the second, the error occurs in he GetString() func:
class GBFdata:public GBFfield{ friend class GBFfile;public: GBFdataType GetType(){return type;} int GetInt(){if(type!=T_INT){error=true;return 0;}return data.intv;} byte GetByte(){if(type!=T_BYTE){error=true;return 0;}return data.bytev;} DWORD GetDword(){if(type!=T_DWORD){error=true;return 0;}return data.dwordv;} WORD GetWord(){if(type!=T_WORD){error=true;return 0;}return data.wordv;} char GetChar(){if(type!=T_CHAR){error=true;return 0;}return data.bytev;} float GetFloat(){if(type!=T_FLOAT){error=true;return 0.0f;}return data.floatv;} char * GetString(){return data.stringv;} uint32 GetDataBlockSize(){if(type!=T_DATA_BLOCK){error=true;return 0;}return data.dataBlockv.size;} void * GetDataBlock(){if(type!=T_DATA_BLOCK){error=true;return NULL;}return data.dataBlockv.data;} void Set(int v){type=T_INT;data.intv=v;} void Set(byte v){type=T_BYTE;data.bytev=v;} void Set(DWORD v){type=T_DWORD;data.dwordv=v;} void Set(WORD v){type=T_WORD;data.wordv=v;} void Set(char v){type=T_CHAR;data.charv=v;} void Set(char* v){type=T_STRING;data.stringv=v;} void Set(float v){type=T_FLOAT;data.floatv=v;} void Set(uint32 s,void*v){type=T_DATA_BLOCK;data.dataBlockv.size=s;data.dataBlockv.data=v;} void DeInit(){};private: union { int intv; byte bytev; DWORD dwordv; WORD wordv; char charv; float floatv; char * stringv; struct { uint32 size; void * data; }dataBlockv; }data; GBFdataType type;};
You'll get an access violation if "this == 0", ie: you called GetString on a null pointer to an object.
Yes, i know this, but this happens when i call the function directly from a object, using the "." operator so "this" should be valid.
I get a debugger message: "Unhandled exception in GTestvc.exe: 0xC0000005: Access Violation." But i get only the asm listing of the function in the debugger's window and it looks like that
?get_beginning@?$list@U_path_list_entry@@@@QAE?AViterator@1@XZ:00404640 push ebp00404641 mov ebp,esp00404643 sub esp,0Ch00404646 mov dword ptr [ebp-0Ch],0CCCCCCCCh0040464D mov dword ptr [ebp-8],0CCCCCCCCh00404654 mov dword ptr [ebp-4],0CCCCCCCCh0040465B mov dword ptr [ebp-0Ch],ecx0040465E mov eax,dword ptr [ebp-0Ch]00404661 mov ecx,dword ptr [eax]00404663 push ecx00404664 lea ecx,[ebp-8]00404667 call list<_path_list_entry>::iterator::iterator (00404780)0040466C mov edx,dword ptr [eax]0040466E mov eax,dword ptr [eax+4]00404671 mov ecx,dword ptr [ebp+8]00404674 mov dword ptr [ecx],edx00404676 mov dword ptr [ecx+4],eax00404679 mov eax,dword ptr [ebp+8]0040467C add esp,0Ch0040467F cmp ebp,esp00404681 call __chkesp (00410ab0)00404686 mov esp,ebp00404688 pop ebp00404689 ret 40040468C int 30040468D int 30040468E int 30040468F int 3
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement