Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Huh ... C behavior (http://www.velocityreviews.com/forums/t440689-huh-c-behavior.html)

sanjaymeher@gmail.com 12-29-2005 09:58 AM

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


Chuck F. 12-29-2005 12:33 PM

Re: Huh ... C behavior
 
sanjaymeher@gmail.com wrote:
>
> 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/>


All times are GMT. The time now is 05:34 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57