Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Huh ... C behavior

Reply
Thread Tools

Huh ... C behavior

 
 
sanjaymeher@gmail.com
Guest
Posts: n/a
 
      12-29-2005
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);
}

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      12-29-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) said:

<lots of stuff about dynamic allocation, which I've snipped>

It appears that the behaviour you want is as follows:

Accept a pointer to a char *, and a size.

If the pointer is NULL, return an error code. Otherwise, if the pointer
whose address was passed is NULL, allocate the supplied number of bytes.
Otherwise, assume it points to a string; find the length of that string,
assume that it represents the current allocation, and increase the
allocation by the supplied amount.

For a number of reasons, this isn't a good design, but let's run with it
anyway. Here is a simple implementation which does what I think you are
trying to do. I commend it to you for study and edification. Note
particularly the clarity and simplicity of the code, compared to your own
version.

Incidentally, I invite you to consider /why/ I said it isn't a good design.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define INCSC_NO_ERROR 0
#define INCSC_BAD_ARG 1
#define INCSC_NO_MEM 2

int IncreaseStringCapacity(char **pp, size_t bytes)
{
int rc = INCSC_NO_ERROR;
char *p = NULL;
if(NULL == pp)
{
rc = INCSC_BAD_ARG;
}
else
{
if(NULL == *pp)
{
*pp = malloc(bytes);
if(NULL == *pp)
{
rc = INCSC_NO_MEM;
}
}
else
{
size_t len = strlen(*pp);
p = realloc(*pp, len + bytes + 1); /* 1 for null terminator */
if(p != NULL)
{
*pp = p;
}
else
{
rc = INCSC_NO_MEM;
}
}
}
return rc;
}

int main(int argc, char **argv)
{
char *test = NULL;
while(argc-- > 0)
{
size_t len = strlen(argv[argc]) + 2;
if(INCSC_NO_ERROR == IncreaseStringCapacity(&test, len))
{
strcat(test, "*");
strcat(test, argv[argc]);
fprintf(stdout, "String is now %s*\n", test);
}
else
{
fprintf(stderr, "Memory allocation failure.\n");
}
}
free(test);
return 0;
}

Sample run:

~/scratch> ./foo these are some command line arguments.
String is now *arguments.*
String is now *arguments.*line*
String is now *arguments.*line*command*
String is now *arguments.*line*command*some*
String is now *arguments.*line*command*some*are*
String is now *arguments.*line*command*some*are*these*
String is now *arguments.*line*command*some*are*these*./foo*


--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
 
 
 
sanjaymeher@gmail.com
Guest
Posts: n/a
 
      12-29-2005
Just changed the if .. else block .. Looks good to me. can u please
point out the bad design issues..

Thanks in advance,
Sanjay

int IncreaseStringCapacity(char **pp, size_t bytes)
{
int rc = INCSC_NO_ERROR;
char *p = NULL;
size_t len = -1;

/* if ptr2ptr string is null */
if(NULL == pp)
{
rc = INCSC_BAD_ARG;
return rc;
}

/* if str ptr has only initialized */
if(NULL == *pp)
{
*pp = malloc(bytes);
if(NULL == *pp)
{
rc = INCSC_NO_MEM;
}
return rc;
}

/* re allocating extra memory memory */
len = strlen(*pp);
p = realloc(*pp, len + bytes + 1); /* 1 for null terminator */
if(p != NULL)
{
*pp = p;
}
else
{
rc = INCSC_NO_MEM;
}

return rc;
}

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-29-2005
(E-Mail Removed) said:

> Just changed the if .. else block ..


Congratulations. You managed to make the code harder to read and harder to
maintain, for no benefit that I can see.

> Looks good to me.


It looked better before.

> can u please point out the bad design issues..


The code suffers from two separate, serious efficiency problems, both of
which stem directly from the design. That's all you get. Now THINK about
it, and see if you can work them out for yourself.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Huh ... C behavior sanjaymeher@gmail.com C Programming 1 12-29-2005 12:33 PM
What about COLA lately, huh? Linønut Computer Support 1 01-30-2005 05:34 PM
Weirdness... huh? (specified web server not running ASP.NET version 1.1) VB Programmer ASP .Net 1 06-12-2004 12:14 AM
doc types! huh? some dude somewhere HTML 6 01-03-2004 10:32 PM
Output parameters undefined? Huh? Jim Owen ASP .Net 1 10-22-2003 03:35 PM



Advertisments