• Advertisement
Sign in to follow this  

Strange/stupid error

This topic is 4849 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
class SomeClass
{
private:
   char * str;

public:
   void SetString(char * s){str=s:}
   char * GetString(){return str;}
}

...

SomeClass sc;

sc.SetString("something");
sc.GetString();


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)?

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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()

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gothmog
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.



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.

Share this post


Link to post
Share on other sites
I have two classes in which i get that error:

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;
};

Share this post


Link to post
Share on other sites
Yes, i know this, but this happens when i call the function directly from a object, using the "." operator so "this" should be valid.

Share this post


Link to post
Share on other sites
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 ebp
00404641 mov ebp,esp
00404643 sub esp,0Ch
00404646 mov dword ptr [ebp-0Ch],0CCCCCCCCh
0040464D mov dword ptr [ebp-8],0CCCCCCCCh
00404654 mov dword ptr [ebp-4],0CCCCCCCCh
0040465B mov dword ptr [ebp-0Ch],ecx
0040465E mov eax,dword ptr [ebp-0Ch]
00404661 mov ecx,dword ptr [eax]
00404663 push ecx
00404664 lea ecx,[ebp-8]
00404667 call list&lt;_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],edx
00404676 mov dword ptr [ecx+4],eax
00404679 mov eax,dword ptr [ebp+8]
0040467C add esp,0Ch
0040467F cmp ebp,esp
00404681 call __chkesp (00410ab0)
00404686 mov esp,ebp
00404688 pop ebp
00404689 ret 4
0040468C int 3
0040468D int 3
0040468E int 3
0040468F int 3

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement