C
cerr
Hi,
I wrote two functions to push() a character array onto an array and
to
pop() the first element[0] of that array again.
The array holding the elements should be dynamically extendible. So
my
this is my code, the push () function seems to work well but i have
questions for my pop() implementation.
int push(char **list, char *str, int curlen){
/*******************************************************
-Description-
This function allocates memory on an array for character
arrays to carry an array of strings. This function is
intended to be used with the pop function for a FIFO
(First In-First Out) data structure that stands for
the std::queue that is availabld in C++.
-Parameters-
char **list - char double pointer to the list carrying
the strings
char *str - char array pointing to the string that
should be added to the array list
int len - integer (global variable from the caller)
that carries the number of elements that
list already carries (required for offset
calculation)
-Return value-
integer - the int value returned by the function
represents the number of elements in list.
This will basically be curlen + 1.
*******************************************************/
char **temp;
temp = realloc(list,(curlen+1)*sizeof(*list));
if (temp==NULL){
printf("push(): Error reallocating memory for msglist\n");
for (i=Len;i>=0;i--) {
free(msglist);
Len--;
}
free(list);
return -1;
}
msglist=temp;
msglist[curlen]=malloc (strlen(str)+1);
strcpy(msglist[curlen],str);
return ++curlen;
}
//-------------------------------------------------------
int pop (char ** list, char *outstr, int curlen)
/*******************************************************
-Description-
This function pops the first value of an array
carrying character arrays. This function can be used
with the push function for a FIFO (First In-First Out)
data structure that stands for the std::queue
that is availabld in C++.
-Parameters-
char **list - char double pointer to the list carrying
the strings
char *outstr- char array pointing to a pre allocated
string where the first value of the array
will be copied to.
int len - integer (global variable from the caller)
that carries the number of elements that
list carries
-Return value-
integer - the int value returned by the function
represents the number of elements in list.
This will basically be curlen - 1.
*******************************************************/
{
int j=0;
int i=0;
char **temp;
if (curlen==0) {
printf("pop(): No element left in list\n");
outstr="";
return 0;
}
/* WARNING!!! Why does it seem to still work fine if there's fewer
bytes allocated to outstr than list[0] is long?*/
strcpy(outstr, list[0]);
for (j=1; j<curlen; j++){
temp = realloc(list,(curlen)*sizeof(*list));
if (temp==NULL){
printf("pop(): Error reallocating temp memory for msglist\n");
for (i=curlen;i>=0;i--) {
free(temp);
curlen--;
}
free(list);
return -1;
}
temp[j-1]=malloc (strlen(list[j])+1);
strcpy(temp[j-1],list[j]);
}
list=temp;
return --curlen;
}
Also, when i call the pop() function as below, what happens if "res"
isn't "long" enough?
char res[1024]={};
int Len;
Len=pop(msglist,res,Len);
Thanks for hints and suggestions!
I wrote two functions to push() a character array onto an array and
to
pop() the first element[0] of that array again.
The array holding the elements should be dynamically extendible. So
my
this is my code, the push () function seems to work well but i have
questions for my pop() implementation.
int push(char **list, char *str, int curlen){
/*******************************************************
-Description-
This function allocates memory on an array for character
arrays to carry an array of strings. This function is
intended to be used with the pop function for a FIFO
(First In-First Out) data structure that stands for
the std::queue that is availabld in C++.
-Parameters-
char **list - char double pointer to the list carrying
the strings
char *str - char array pointing to the string that
should be added to the array list
int len - integer (global variable from the caller)
that carries the number of elements that
list already carries (required for offset
calculation)
-Return value-
integer - the int value returned by the function
represents the number of elements in list.
This will basically be curlen + 1.
*******************************************************/
char **temp;
temp = realloc(list,(curlen+1)*sizeof(*list));
if (temp==NULL){
printf("push(): Error reallocating memory for msglist\n");
for (i=Len;i>=0;i--) {
free(msglist);
Len--;
}
free(list);
return -1;
}
msglist=temp;
msglist[curlen]=malloc (strlen(str)+1);
strcpy(msglist[curlen],str);
return ++curlen;
}
//-------------------------------------------------------
int pop (char ** list, char *outstr, int curlen)
/*******************************************************
-Description-
This function pops the first value of an array
carrying character arrays. This function can be used
with the push function for a FIFO (First In-First Out)
data structure that stands for the std::queue
that is availabld in C++.
-Parameters-
char **list - char double pointer to the list carrying
the strings
char *outstr- char array pointing to a pre allocated
string where the first value of the array
will be copied to.
int len - integer (global variable from the caller)
that carries the number of elements that
list carries
-Return value-
integer - the int value returned by the function
represents the number of elements in list.
This will basically be curlen - 1.
*******************************************************/
{
int j=0;
int i=0;
char **temp;
if (curlen==0) {
printf("pop(): No element left in list\n");
outstr="";
return 0;
}
/* WARNING!!! Why does it seem to still work fine if there's fewer
bytes allocated to outstr than list[0] is long?*/
strcpy(outstr, list[0]);
for (j=1; j<curlen; j++){
temp = realloc(list,(curlen)*sizeof(*list));
if (temp==NULL){
printf("pop(): Error reallocating temp memory for msglist\n");
for (i=curlen;i>=0;i--) {
free(temp);
curlen--;
}
free(list);
return -1;
}
temp[j-1]=malloc (strlen(list[j])+1);
strcpy(temp[j-1],list[j]);
}
list=temp;
return --curlen;
}
Also, when i call the pop() function as below, what happens if "res"
isn't "long" enough?
char res[1024]={};
int Len;
Len=pop(msglist,res,Len);
Thanks for hints and suggestions!