P
Piotrek
Hi,
I have no idea why my reallocation function (myadd) is breaking up my
array. I'm trying to solve it and it took me few hours already. Maybe
you can look at my code and explain me what is wrong. (I still have
problems with pointers).
I left my code as it is.
Thank you!
#include "stdafx.h" /* contains stdio.h */
#include <stdlib.h>
#include <string.h>
void *alloca(int n);
char *readr(void);
void myadd(char **A);
void myfree(void *A);
/* Main program */
int main(void){
/* Dynamically allocated array of pointers :*/
char **names;
int i,n=2;
names=(char **)alloca(n);
for(i=0;i<n;i++){
names=readr();
printf("txt: %s\n", names);
}
myadd(names);
myfree(names);
return 0;
}
/* Functions */
void myfree(void *AA){ /* Free all allocated memory */
int i;
void **A=(void **)AA;
for(i=0; A; i++) /* Finish freeing when A=NULL */
free(A);
free(AA);
}
void *alloca(int n){
void **A;
/* Avoid n==0:*/
if(n==0) n=1;
if((A=(void **)calloc(n+1, sizeof(*A)))==NULL) return NULL;
A[n]=NULL;
return A;
}
char *readr(void){
char *buf;
int i;
char c,BB[256]={'\0'};
fflush(stdin);
/* Read 255 characters max. Last char is '\0': */
for(i=0; ((c=getchar())!='\n')&&i<255; i++)
BB=c;
fflush(stdin);
/* i+1: enable myfree() to free all memory: */
if((buf=(char *)calloc(i+1,sizeof(char)))==NULL){
printf("Oops, can't allocate mem.!\n");
}
else{
strcpy(buf,BB);
return buf;
}
}
/* Should reallocate "names" and add one more entry to it :*/
void myadd(char **A){
int n;
/* Count rows of array :*/
for(n=0;A[n];n++);
/* It is supposed to reallocate "names" but it seems to break it */
if((A=(char **)realloc(A, (n+2)*sizeof(*A)))==NULL)
printf("Oops, can't reallocate mem.!\n");
A[n]=readr();
A[n+1]=NULL;
}
I have no idea why my reallocation function (myadd) is breaking up my
array. I'm trying to solve it and it took me few hours already. Maybe
you can look at my code and explain me what is wrong. (I still have
problems with pointers).
I left my code as it is.
Thank you!
#include "stdafx.h" /* contains stdio.h */
#include <stdlib.h>
#include <string.h>
void *alloca(int n);
char *readr(void);
void myadd(char **A);
void myfree(void *A);
/* Main program */
int main(void){
/* Dynamically allocated array of pointers :*/
char **names;
int i,n=2;
names=(char **)alloca(n);
for(i=0;i<n;i++){
names=readr();
printf("txt: %s\n", names);
}
myadd(names);
myfree(names);
return 0;
}
/* Functions */
void myfree(void *AA){ /* Free all allocated memory */
int i;
void **A=(void **)AA;
for(i=0; A; i++) /* Finish freeing when A=NULL */
free(A);
free(AA);
}
void *alloca(int n){
void **A;
/* Avoid n==0:*/
if(n==0) n=1;
if((A=(void **)calloc(n+1, sizeof(*A)))==NULL) return NULL;
A[n]=NULL;
return A;
}
char *readr(void){
char *buf;
int i;
char c,BB[256]={'\0'};
fflush(stdin);
/* Read 255 characters max. Last char is '\0': */
for(i=0; ((c=getchar())!='\n')&&i<255; i++)
BB=c;
fflush(stdin);
/* i+1: enable myfree() to free all memory: */
if((buf=(char *)calloc(i+1,sizeof(char)))==NULL){
printf("Oops, can't allocate mem.!\n");
}
else{
strcpy(buf,BB);
return buf;
}
}
/* Should reallocate "names" and add one more entry to it :*/
void myadd(char **A){
int n;
/* Count rows of array :*/
for(n=0;A[n];n++);
/* It is supposed to reallocate "names" but it seems to break it */
if((A=(char **)realloc(A, (n+2)*sizeof(*A)))==NULL)
printf("Oops, can't reallocate mem.!\n");
A[n]=readr();
A[n+1]=NULL;
}