Newbie Question on C Pointers
-
Hello, I try to understand memory management and pointers in C on a simple level right now. Here is the task: concatenate 2 buffers (in this case buffA & buffB to buffAB). The values of the following code-snipets are small for debugging purposes ... imagine the size of each source buffer is much larger: how can I propper clean up the "mess" after creating buffAB?
// buffA char buffA[] = {'a'}; char* p_buffA = &buffA[0]; // buffB char buffB[] = {'b','b'}; char* p_buffB = &buffB[0]; // buffAB char buffAB[] = {'x','x','x'}; char* p_buffAB = &buffAB[0]; // cpy memcpy(p_buffAB, p_buffA, 1); // copy buffA to buffAB memcpy(p_buffAB+1, p_buffB, 2); // copy buffB to buffAB // buffAB is the only thing we want ... how to clean up the rest?
I am thankfull for all suggesting to improve the code. -
Hello, I try to understand memory management and pointers in C on a simple level right now. Here is the task: concatenate 2 buffers (in this case buffA & buffB to buffAB). The values of the following code-snipets are small for debugging purposes ... imagine the size of each source buffer is much larger: how can I propper clean up the "mess" after creating buffAB?
// buffA char buffA[] = {'a'}; char* p_buffA = &buffA[0]; // buffB char buffB[] = {'b','b'}; char* p_buffB = &buffB[0]; // buffAB char buffAB[] = {'x','x','x'}; char* p_buffAB = &buffAB[0]; // cpy memcpy(p_buffAB, p_buffA, 1); // copy buffA to buffAB memcpy(p_buffAB+1, p_buffB, 2); // copy buffB to buffAB // buffAB is the only thing we want ... how to clean up the rest?
I am thankfull for all suggesting to improve the code.since you didn't dynamically allocate anything (with new or malloc, etc), there's nothing to clean up.
-
Hello, I try to understand memory management and pointers in C on a simple level right now. Here is the task: concatenate 2 buffers (in this case buffA & buffB to buffAB). The values of the following code-snipets are small for debugging purposes ... imagine the size of each source buffer is much larger: how can I propper clean up the "mess" after creating buffAB?
// buffA char buffA[] = {'a'}; char* p_buffA = &buffA[0]; // buffB char buffB[] = {'b','b'}; char* p_buffB = &buffB[0]; // buffAB char buffAB[] = {'x','x','x'}; char* p_buffAB = &buffAB[0]; // cpy memcpy(p_buffAB, p_buffA, 1); // copy buffA to buffAB memcpy(p_buffAB+1, p_buffB, 2); // copy buffB to buffAB // buffAB is the only thing we want ... how to clean up the rest?
I am thankfull for all suggesting to improve the code._NielsB wrote:
// buffAB is the only thing we want ... how to clean up the rest?
clean up is called
free
inC
language and works on dynamic allocated memory, i.e. it should pair a previous call tomalloc
(or another allocation function, likecalloc
). For instance:char * pB = malloc(2);
pB[0]='b';
pB[1]='b';
/* or *pb='b'; *(pb+1)='b'; */
/* or memset(b, (int)'b', 2); */
char * pA = malloc(1);
pA[0]='a'; /* same options b has */
char * PAB = malloc(3);
memcpy(pAB, pA, 1 );
memcpy(pAB+1, pB, 2);
free(pA);
free(pB);Note that I've omitted (to keep code compact)
malloc
return value check, but you must always do it in real programming, i.e.char *p=malloc(10);
if ( ! p )
{
/* handle error */
}
/* allocation was fine, go on:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles] -
_NielsB wrote:
// buffAB is the only thing we want ... how to clean up the rest?
clean up is called
free
inC
language and works on dynamic allocated memory, i.e. it should pair a previous call tomalloc
(or another allocation function, likecalloc
). For instance:char * pB = malloc(2);
pB[0]='b';
pB[1]='b';
/* or *pb='b'; *(pb+1)='b'; */
/* or memset(b, (int)'b', 2); */
char * pA = malloc(1);
pA[0]='a'; /* same options b has */
char * PAB = malloc(3);
memcpy(pAB, pA, 1 );
memcpy(pAB+1, pB, 2);
free(pA);
free(pB);Note that I've omitted (to keep code compact)
malloc
return value check, but you must always do it in real programming, i.e.char *p=malloc(10);
if ( ! p )
{
/* handle error */
}
/* allocation was fine, go on:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles]Hello again! Thank you for your adwises :) The real task I want to test is to append buffB to buffA. Asume there is a function-call that gives me 2 pointers to buffers (p_buffA and p_buffB) and the size of each of them (n_A and n_B). The function should retur the new pointer to buffA and the new size (f.e. saved in a struct). Now I am using c++ new / delete to allocate and free memory.
// buffA int n_A = 30; char* p_buffA = new char[n_A]; memset(p_buffA, (int)'a', n_A); // buffB int n_B = 50; char* p_buffB = new char[n_B]; memset(p_buffB, (int)'b', n_B); // append buffB to buffA, use temp buffAB int n_AB = n_A + n_B; char* p_buffAB = new char[n_AB]; // cpy memcpy(p_buffAB, p_buffA, n_A); // copy buffA to buffAB memcpy(p_buffAB+n_A, p_buffB, n_B); // copy buffB to buffAB // buffA and buffB are no longer neaded -> delete delete [] p_buffA; delete [] p_buffB; // set buffA to buffAB p_buffA = p_buffAB; n_A = n_AB;
Is this snipet ok? Are there better ways to solve this issue? -
Hello again! Thank you for your adwises :) The real task I want to test is to append buffB to buffA. Asume there is a function-call that gives me 2 pointers to buffers (p_buffA and p_buffB) and the size of each of them (n_A and n_B). The function should retur the new pointer to buffA and the new size (f.e. saved in a struct). Now I am using c++ new / delete to allocate and free memory.
// buffA int n_A = 30; char* p_buffA = new char[n_A]; memset(p_buffA, (int)'a', n_A); // buffB int n_B = 50; char* p_buffB = new char[n_B]; memset(p_buffB, (int)'b', n_B); // append buffB to buffA, use temp buffAB int n_AB = n_A + n_B; char* p_buffAB = new char[n_AB]; // cpy memcpy(p_buffAB, p_buffA, n_A); // copy buffA to buffAB memcpy(p_buffAB+n_A, p_buffB, n_B); // copy buffB to buffAB // buffA and buffB are no longer neaded -> delete delete [] p_buffA; delete [] p_buffB; // set buffA to buffAB p_buffA = p_buffAB; n_A = n_AB;
Is this snipet ok? Are there better ways to solve this issue?It is ok. But you have always check for pointer validity, for instance
char* p_buffA = new char[n_A];
if ( !p_buffA )
{
// handle error
}
// go on with program
...You have also the option to use
realloc
:// buffA
int n_A = 30;
char* p_buffA = (char*) malloc(n_A);
memset(p_buffA, (int)'a', n_A);// buffB
int n_B = 50;
char* p_buffB = (char*) malloc(n_B);
memset(p_buffB, (int)'b', n_B);// append buffB to buffA, without use temporary
char* p_buffA = realloc[p_BuffA, n_A + n_B];// cpy
memcpy(p_buffA + n_A, p_buffB, n_B); // copy buffB to new buffA// buffB is no longer neaded, delete
free(p_buffB);(as stated above you've always to check return value of allocation function) :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles] -
It is ok. But you have always check for pointer validity, for instance
char* p_buffA = new char[n_A];
if ( !p_buffA )
{
// handle error
}
// go on with program
...You have also the option to use
realloc
:// buffA
int n_A = 30;
char* p_buffA = (char*) malloc(n_A);
memset(p_buffA, (int)'a', n_A);// buffB
int n_B = 50;
char* p_buffB = (char*) malloc(n_B);
memset(p_buffB, (int)'b', n_B);// append buffB to buffA, without use temporary
char* p_buffA = realloc[p_BuffA, n_A + n_B];// cpy
memcpy(p_buffA + n_A, p_buffB, n_B); // copy buffB to new buffA// buffB is no longer neaded, delete
free(p_buffB);(as stated above you've always to check return value of allocation function) :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles]