strncpy(,,) , char* pointer ,char array;

Hello i have a function where i pass a char* buffer, int bufferSize, int blockSize.

They are both allocated new char[SOME_SIZE];
My qustion is this.

Is this a valid use of strncpy() ?

strncpy(tmpBuff, &buffer[X], blockSize);

What am trying to do is take a part (blockSize) of the buffer and move it to tmpBuff. I will do this in a loop here is a snipp from my code.

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
BuffSplit::BuffSplit(char* inBuffer, int inSize , int inBlockSize)
{
	Size = inSize;
	buffer = new char[Size];
	buffer = inBuffer;
	blockSize = inBlockSize;

	char* tmpBuff;
	tmpBuff = new char[blockSize];
	
	int res = 1;
	int X = 0;
	for (; X < Size; )
	{
		if (X >= Size)
			break;

		strncpy(tmpBuff, &buffer[X], blockSize);
		X = ( X + blockSize);
                
                //....
                // Passing the block of..... Doing somthing bla bla..
                //....

		res++;
		totalUpRes = res;
	}


Cheers
WetCode
Last edited on
> Is this a valid use of strncpy() ?
> strncpy(tmpBuff, &buffer[X], blockSize);
It is syntactically correct, but
man wrote:
char *strncpy(char *dest, const char *src, size_t n);
Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated
If you don't need a copy, then don't make a copy.

By the way, you've got a memory leak in line 5.
Also, ¿what's the point of line 15?
Last edited on
Thanks for your reply i did some modifications to my code but the more i look at this the less i understand at this point. I probeboly seem stupid for asking this but how is line 5 above a memory leak, ? I put some comments in the code to try to bether explain what am doing. (Trying to do.)

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
30
BuffSplit::BuffSplit(char* inBuffer, int inSize , int inBlockSize)
{					// complete buffer	// total Size of buffer	// size of the parts of buffer

	Size = inSize; // sets the class veriable total buffer size
	buffer = new char[Size]; // allocate memory for the complete buffer
	buffer = inBuffer; // sett class buffer to == inBuffer (I dont see the leak they are the same size and inBuffer is null terminated.)
	blockSize = inBlockSize; // the size of equal part to devide the complete buffer in.

	char* tmpBuff; // buffer for the "sub string" parts of the complete buffer
	tmpBuff = new char[blockSize];
	
	int res = 1; // down the method not relevant
	int X = 0;
	for (; (X + blockSize) < fileSize; )
	{
		// So this is suposed to take a substring of size blockSize and place in 
		// ttmpBuff go in to some methods jump x+blockSize so i start to take the new sub string
		// from X + blockSize +1 so i dont get the last char of the last sub string first in the next one.
		strncpy(tmpBuff, &buffer[X], blockSize);

		X = ( X + blockSize + 1); // next location to start sub stringing with size BlockSize
		tmpBuff[blockSize + 1] = '\0';

                //....methods()

		res++;
		totalUpRes = res;
	}
	totalUpRes = res;
}


buffer = inBuffer; touches the pointer, not the content.
So you are discarding what was just allocated (leak).
I guess you want `strcpy()' or `memcpy()' there.

1
2
3
tmpBuff = new char[blockSize];
//...
tmpBuff[blockSize + 1] = '\0'; //out of bounds. the last valid index is blockSize-1 


> from X + blockSize +1 so i dont get the last char of the last sub string first in the next one.
make a desk test. Suppose that blockSize=4
You start at X=0 and copy 4 characters, so you copy buffer[0], buffer[1], buffer[2] and buffer[3]
In the next iteration you set X=0+4+1=5, missing buffer[4]

As you don't care about '\0' in the source, maybe you want `memcpy()' there.


As you are using c++, take a look at `std::string'
Thank you for your replys i solved my problem now.
I did as you sad and wrote a desk test of this and thats what is working now. So i stile got the uncertenty of implimenting it in my project. But am an optimist and since it prodcues the results i want am gona look for the buf somewhere else in my application. Anyways..
Turns out the blockSize had some HUGE impact on the stabilaty in my application.

I solved this by using the following if statment.
1
2
3
4
5
6
	if ( blockSize < (Size * 0.12))
	{
		blockSize = (Size * 0.12);
		cout << "Unstable or to small block Size recalculating." << endl;
		cout << "Block Size set to: " << blockSize << endl;
	} // Got a stable blockSize 
If anyone have any idea way please inlighten me.

Here is the full snip:
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
30
31
32
33
34
35
36
37
38
39
40
41
void splitBuffer(char* Buffer,int Size,int blockSize)
{
	// Is the blockSize stable?
	if ( blockSize < (Size * 0.12))
	{
		blockSize = (Size * 0.12);
		cout << "Unstable or to small block Size recalculating." << endl;
		cout << "Block Size set to: " << blockSize << endl;
	} // Got a stable blockSize

	char *tmpBuff = new char[blockSize];

	int buffX = 0;
	int resX = 1;
	while ( buffX < Size )
	{
		if ((buffX + blockSize) > (Size))
		{
			while ( (buffX + blockSize) != Size )
			{
				blockSize--;
			}
			// This WILL produce a +1 on the size
			// BUT the for loop should take care of this < blockSize or ?
			// Thus not going out of bounce
			blockSize = blockSize - 1; // Adjusted offset i huese not
		}
		char *tmpBuff = new char[blockSize];

		for (int i = 0; i < blockSize; i++)
		{
			tmpBuff[i] = Buffer[buffX];
			buffX++;
		}

                // Methods... and somewhere akong the way.
		resX++;
                // Methods... and somewhere akong the way.

		buffX++;
	}


Edit: Implimentation fails. Back to the drawing board.
Last edited on
Topic archived. No new replies allowed.