Huh ... C behavior

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);
}
 
C

Chuck F.

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
.... snip lots ...

It is working correctly according to the C standard. Undefined
behaviour allows anything whatsoever to happen, including "working"
(whatever that may be).

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top