Pass char* to be initialized in function

Hi,I am trying to pass char* buf to the function writeToBuffer,
where I want it to be initialized. It does so, but only for the local variable
myBuffer. buf in main continues to point to null after function returns.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void writeToBuffer(char* myBuffer)
{
	myBuffer = new char[4096];
	for(int i =0; i<4096; i++)
	{
		myBuffer[i]=i%127;
	}
}



int _tmain(int argc, _TCHAR* argv[])
{

	char* buf = NULL;
	writeToBuffer(buf);

	return 0;
}



Also, let's assume I have a class member two dimnsinal array
of type e.g. unsigned char** bytes that I would like to initialize dynamically(using for & new e.t.c.)
inside another function like before. How would I go about passing this as a parameter and what should the function arguements on method decleration be?

Thank you for your help!
Your code doesn't work as you expect.

- 'buf' in main remains unchanged. It will still be NULL even after writeToBuffer is called.

- you are callig new[], but you do not have a matching call to delete[], so you have a memory leak.

Plus, multidimentional arrays are evil: http://www.cplusplus.com/forum/articles/17108/
if you want a void function, then pass the address of the pointer (**)

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
void writeToBuffer(char** myBuffer)
{
	
        char* localPointer  = new char[4096];
	for(int i =0; i<4096; i++)
	{
	     localPointer[i]=i%127;
	}

       *myBuffer = localPointer
}



int _tmain(int argc, _TCHAR* argv[])
{

	char* buf = NULL;
	writeToBuffer(& buf); //pass a pointer to the buffer (char**)

        //use buffer

       delete [] buf;

	return 0;
}



=========================================
OR change the function to retun a char*

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
char* writeToBuffer() //takes no parameters
{
	
        char* myBuffer  = new char[4096];
	for(int i =0; i<4096; i++)
	{
		myBuffer[i]=i%127;
	}

       return myBuffer
}



int _tmain(int argc, _TCHAR* argv[])
{

	char* buf =  writeToBuffer(); //pass a pointer to the buffer (char**)
        
        //use buffer

       delete [] buf;

	return 0;
}
Last edited on
First of all, thanks for the help guys.

Im aware of the memory leak above just making a quick example, sorry.

What I really wanted to do was to initialize directly e.g. like


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
void writeToBuffer(char** myBuffer)
{
	*myBuffer = new char[4096];
	for(int i =0; i<4096; i++)
	{
		*myBuffer[i]=i%127;
	}
}



int _tmain(int argc, _TCHAR* argv[])
{

	//FileLogger myFL;
	//myFL.read_to_Terminal("C:\\Documents and Settings\\Neo_Geo\\Desktop\\blues.txt", 8);
	//printf("%s\n", myFL.read("C:\\Documents and Settings\\Neo_Geo\\Desktop\\blues.txt"));

	char* buf = NULL;
	writeToBuffer(&buf);
	if(buf!=NULL)
	{
		delete[] buf;
	}
	return 0;
}


but this gives a memory write violation, it's wrong :/

I'll have to return a pointer to the buffer and just delete,

or create a new char buffer and then assign mine to it like you showed me.


p.s. I'll definitely read the article you suggested and come back if I have trouble passing a 2d "evil" one as a parameter, unfortunately Im out if time for tonight :(
You get a memory write violation, because you have neglected to take note of operator
precedence.
You should have written:

(*myBuffer)[i]=i%127;

or you could have done:

myBuffer[0][i] = i%127;
Last edited on
Topic archived. No new replies allowed.