S
sanjaymeher
Copy paste and run the example now remove the comment /*test2();*/
in main function ... Its not working .. Whyyyy ?? Behavior is really
queer !!!! I know the method
"int addDynamicMemory1(char *ptr, int size)" is not the right context
of use here But I want to get the solution to this particular problem
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void test1();
void test2();
int addDynamicMemory1(char *ptr, int size);
int addDynamicMemory(char **ptr, int size);
int addDynamicMemory1(char *ptr, int size)
{
/* See and chek whether size memory is available or not */
int currSize;
if(ptr == NULL)
{
ptr = (char*) malloc(size * sizeof(char));
if(ptr != NULL)
{
printf("Initialized memory as null \n");
return -1;
}
else
{
printf("Can not Initialized memory as null \n");
return -1;
}
}
currSize = strlen(ptr);
size = currSize + size;
ptr = (char*) realloc(ptr, size*sizeof(char));
if(ptr != NULL)
{
printf(" re Allocation size is %d\n",size);
return 0;
}
printf(" re Allocation failed \n");
return -1;
}
int addDynamicMemory(char **ptr, int size)
{
/* See and chek whether size memory is available or not */
int currSize;
if(*ptr == NULL)
{
*ptr = (char*) malloc(size * sizeof(char));
if(*ptr != NULL)
{
printf("Initialized memory as null \n");
return -1;
}
else
{
printf("Can not Initialized memory as null \n");
return -1;
}
}
currSize = strlen(*ptr);
size = currSize + size;
*ptr = (char*) realloc(*ptr, size*sizeof(char));
if(ptr != NULL)
{
printf(" re Allocation size is %d\n",size);
return 0;
}
printf(" re Allocation failed \n");
return -1;
}
int main(void)
{
test1();
/*test2();*/
}
void test2()
{
char *test = NULL;
addDynamicMemory(&test, 40);
printf("At first test value is %s\n",test);
strcpy(test,"444444444");
printf("After allocation val is %s\n", test);
addDynamicMemory1(test, 60);
strcat(test,"666666666666");
printf("After allocation val is %s\n", test);
addDynamicMemory1(test, 60);
strcat(test,"888888888888888");
printf("After allocation val is %s\n", test);
}
void test1()
{
char *test = NULL;
addDynamicMemory(&test, 40);
printf("At first test value is %s\n",test);
strcpy(test,"444444444");
printf("After allocation val is %s\n", test);
addDynamicMemory(&test, 50);
strcat(test,"5555555555");
printf("After allocation val is %s\n", test);
addDynamicMemory1(test, 60);
strcat(test,"666666666666");
printf("After allocation val is %s\n", test);
addDynamicMemory1(test, 60);
strcat(test,"888888888888888");
printf("After allocation val is %s\n", test);
}
in main function ... Its not working .. Whyyyy ?? Behavior is really
queer !!!! I know the method
"int addDynamicMemory1(char *ptr, int size)" is not the right context
of use here But I want to get the solution to this particular problem
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void test1();
void test2();
int addDynamicMemory1(char *ptr, int size);
int addDynamicMemory(char **ptr, int size);
int addDynamicMemory1(char *ptr, int size)
{
/* See and chek whether size memory is available or not */
int currSize;
if(ptr == NULL)
{
ptr = (char*) malloc(size * sizeof(char));
if(ptr != NULL)
{
printf("Initialized memory as null \n");
return -1;
}
else
{
printf("Can not Initialized memory as null \n");
return -1;
}
}
currSize = strlen(ptr);
size = currSize + size;
ptr = (char*) realloc(ptr, size*sizeof(char));
if(ptr != NULL)
{
printf(" re Allocation size is %d\n",size);
return 0;
}
printf(" re Allocation failed \n");
return -1;
}
int addDynamicMemory(char **ptr, int size)
{
/* See and chek whether size memory is available or not */
int currSize;
if(*ptr == NULL)
{
*ptr = (char*) malloc(size * sizeof(char));
if(*ptr != NULL)
{
printf("Initialized memory as null \n");
return -1;
}
else
{
printf("Can not Initialized memory as null \n");
return -1;
}
}
currSize = strlen(*ptr);
size = currSize + size;
*ptr = (char*) realloc(*ptr, size*sizeof(char));
if(ptr != NULL)
{
printf(" re Allocation size is %d\n",size);
return 0;
}
printf(" re Allocation failed \n");
return -1;
}
int main(void)
{
test1();
/*test2();*/
}
void test2()
{
char *test = NULL;
addDynamicMemory(&test, 40);
printf("At first test value is %s\n",test);
strcpy(test,"444444444");
printf("After allocation val is %s\n", test);
addDynamicMemory1(test, 60);
strcat(test,"666666666666");
printf("After allocation val is %s\n", test);
addDynamicMemory1(test, 60);
strcat(test,"888888888888888");
printf("After allocation val is %s\n", test);
}
void test1()
{
char *test = NULL;
addDynamicMemory(&test, 40);
printf("At first test value is %s\n",test);
strcpy(test,"444444444");
printf("After allocation val is %s\n", test);
addDynamicMemory(&test, 50);
strcat(test,"5555555555");
printf("After allocation val is %s\n", test);
addDynamicMemory1(test, 60);
strcat(test,"666666666666");
printf("After allocation val is %s\n", test);
addDynamicMemory1(test, 60);
strcat(test,"888888888888888");
printf("After allocation val is %s\n", test);
}