Having problems with char data type variable

Hey,
I have two char variables, m_GPSOffset[13] and m_FileName[100]. When m_GPSOffset has a value assigned to it, say for instance +11:25:30. The first entry of the value, in this case +, is always stored in m_FileName. I am clueless on why this is occurring. Can anyone tell me why this is happening?

Thanks In Advance
Can you post some of the code?, it looks like there is some memory over writing going on, as if m_GPOffset does not have any space reserved
It could be just random data. Have you tried to set the content of each array when you declare them:

1
2
char m_GPSOffset[13] = {0};
char m_FileName[100] = {0};

ajh32,
I am trying to set the content of each array in a clear() method that is called by the constructor but it isn't allowing the follwing code:
1
2
3

m_GPSOffset = {0};


I declared in the header file.
Instead of bla...bla...bla... show how you assign a value to m_GPSOffset
vlad from moscow,
The following is how I am assigning a value:
1
2
3
4
5
6
7

m_pResource->SetGPSOffSet(m_strGPSOffsetTm.GetBuffer(m_strGPSOffsetTm.GetLength()))

void SetGPSOffset(char *time)
{
      strcpy(m_GPSOffSet, time);
}


m_strGPSOffsetTm is a CString
AFAIK member function GetLength returns the number of characters in a CString object does not counting the null-terminating character. So it can be that the returned buffer is not null-terminated.

Try to use

m_strGPSOffsetTm.GetLength() + 1

Last edited on
vlad from moscow,
Problem still exist whwn adding 1.
Below is the code for m_FileName:
1
2
3

strcpy(m_pResource->m_FileName, m_InitFileName.GetBuffer(100));
Early you were using m_strGPSOffsetTm but in the last post you are using m_InitFileName.
So it looks like that you do not understand what you are doing.

Class CString has method operator const char *. So you can write simply

strcpy(m_pResource->m_FileName, static_cast<const char *>( m_strGPSOffsetTm ) );

Or you can write even the following way

strcpy(m_pResource->m_FileName, m_strGPSOffsetTm );


Otherwise it seems that m_FileName has no enough memory that to store all characters from m_strGPSOffsetTm.
Last edited on
vlad from moscow,
Please read the first post about this issue and then you will understand what I am doing.
Instead of you I understand the problem correctly. It is you who can not understand what he is doing.:)
Simple CTest class with corresponding CResource class to attempt to demonstrate what you are attempting to do. But in the CResource class I have initialised the char arrays in the constructor and also check that we don't overrun the buffer when we set one of these arrays. Does that help?

headers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class CResource
{
public:
	CResource();
	virtual ~CResource();

	void SetGPSOffSet(char* time);

private:
	char m_GPSOffset[13];
	char m_FileName[100];
};


class CTest
{
public:
	CTest();
	virtual ~CTest();

private:
	CResource* m_pResource;
	CString m_strGPSOffsetTm;
};


source:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
CResource::CResource()
{
	memset(m_GPSOffset, 0, 13);
	memset(m_FileName, 0, 100);
}

CResource::~CResource()
{

}

void CResource::SetGPSOffSet(char* time)
{
	strcpy_s(m_GPSOffset, 13, time);
}


CTest::CTest()
	: m_strGPSOffsetTm("+11:25:30")
{
	m_pResource = new CResource();
	m_pResource->SetGPSOffSet(m_strGPSOffsetTm.GetBuffer());
	m_strGPSOffsetTm.ReleaseBuffer();
}

CTest::~CTest()
{
	delete m_pResource;
}
Here is the code that demonstrates your problem

1
2
3
4
5
6
7
8
CString s = _T( "Hello World" );
std::wcout << static_cast<const wchar_t *>( s ) << std::endl;
std::wcout << "Number of characters is " << s.GetLength() << std::endl;

wchar_t t[12];

wcscpy( t, s.GetBuffer( 2 ) );
if ( t[0] == L'H' && t[1] == L'e' ) wcout << "There is no the terminating zero" << std::endl; 



As you see when the size of the buffer is equal to or less then the value returned by GetLength then the terminating zero is not present. So the function strcpy has undefined behaviour. I showed you two methods how you can copy a CString object or you can use strncpy function instead of strcpy and append the terminating zero yourself.
Yes, and strcpy_s will check that you don't exceed the buffer size, as I demonstrated above. If the string being copied is 1 char less than you have declared the array into which you are copying all will be fine, but if it is the same or larger an assertion will be thrown.
ajh32,
Thanks for replying. I am still having the same issue even after changing my code to exactly what you have. Did you have the same issue I had? Once again thanks!!
Thanks for replying. I am still having the same issue even after changing my code to exactly what you have. Did you have the same issue I had? Once again thanks!!


No. Did you initialise your char arrays first? Are you using a safe string copy?
Last edited on
Topic archived. No new replies allowed.