Hello all
I'm pretty new to C, so please accept my apologies in advance :-)
I'm trying to allocate space for an array of pointers to strings
(which are accepted as ellipses) inside a while loop, and after the
allocation, when i "assert" the allocation, the assertion fails!!!
void printStrings(s1 , ...){ //ellipses function
....
....
void *myList;
int count=1;
char *pArr=s1;
va_start(myList , s1);
while( myList ){
pArr = (char *) realloc(pArr, ++count * sizeoff(char*)) ;
assert(pArr);
pArr[count-1] = va_arg(myList, char*);
}
However, if i take the 2 lines of the allocation and assertion out of
he while, it works!!!
i.e. - the following is OK :
void *myList;
int count=1;
char *pArr=s1;
va_start(myList , s1);
pArr = (char *) realloc(pArr, ++count * sizeoff(char*)) ;
assert(pArr); //THIS IS OK
even if i put these two lines in a for loop that run just once, it
fails!!!
int count=1;
char *pArr=s1;
va_start(myList , s1);
for(i =0; i <1; i++){ // LOOP RUNS JUST ONCE
pArr = (char *) realloc(pArr, ++count * sizeoff(char*)) ;
assert(pArr); //ASSERTION FAILS HERE!!!
}
what Am i doing wrong and why is the allocation failing inside the
while/for loops
Thanks a lot
Shiron
I'm pretty new to C, so please accept my apologies in advance :-)
I'm trying to allocate space for an array of pointers to strings
(which are accepted as ellipses) inside a while loop, and after the
allocation, when i "assert" the allocation, the assertion fails!!!
void printStrings(s1 , ...){ //ellipses function
....
....
void *myList;
int count=1;
char *pArr=s1;
va_start(myList , s1);
while( myList ){
pArr = (char *) realloc(pArr, ++count * sizeoff(char*)) ;
assert(pArr);
pArr[count-1] = va_arg(myList, char*);
}
However, if i take the 2 lines of the allocation and assertion out of
he while, it works!!!
i.e. - the following is OK :
void *myList;
int count=1;
char *pArr=s1;
va_start(myList , s1);
pArr = (char *) realloc(pArr, ++count * sizeoff(char*)) ;
assert(pArr); //THIS IS OK
even if i put these two lines in a for loop that run just once, it
fails!!!
int count=1;
char *pArr=s1;
va_start(myList , s1);
for(i =0; i <1; i++){ // LOOP RUNS JUST ONCE
pArr = (char *) realloc(pArr, ++count * sizeoff(char*)) ;
assert(pArr); //ASSERTION FAILS HERE!!!
}
what Am i doing wrong and why is the allocation failing inside the
while/for loops
Thanks a lot
Shiron
Comment