473,657 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

realloc

Roy
Hi all :
My code below :

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

char *cat(char *s, const char *t)
{
char *tmp;

if (s == NULL)
tmp = realloc(s,strle n(t) + 1);
else
tmp = realloc(s,strle n(s) + strlen(t) + 1);
if (tmp == NULL) {
fputs("realloc error",stderr);
exit(1);
}
while (*tmp++); // search for '\0' and stop
tmp--; //the position of '\0'
while (*tmp++ = *t++) ;
s = tmp;
return s;
}

int main()
{
char *tmp;
char *s;

s = NULL;
tmp = cat(s,"oh");
printf("%s\n",t mp);
tmp = cat(s,"haha");
printf("%s\n",t mp);
return 0;
}
my problem is when I run the program the reslut is some blanks. I just
wrote a small routine like strcat and check the realloc and tmp pointer
carefully but found nothing.

Nov 14 '05 #1
36 2811

Roy wrote:
Hi all :
My code below :

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

char *cat(char *s, const char *t)
{
char *tmp;

if (s == NULL)
tmp = realloc(s,strle n(t) + 1);
else
tmp = realloc(s,strle n(s) + strlen(t) + 1);
if (tmp == NULL) {
fputs("realloc error",stderr);
exit(1);
}
while (*tmp++); // search for '\0' and stop
the above statement is not right, when s = null, what do you expect the
*tmp is?
tmp--; //the position of '\0'
while (*tmp++ = *t++) ;
s = tmp;
return s;
}

int main()
{
char *tmp;
char *s;

s = NULL;
tmp = cat(s,"oh");
printf("%s\n",t mp);
tmp = cat(s,"haha");
printf("%s\n",t mp);
return 0;
}
my problem is when I run the program the reslut is some blanks. I just wrote a small routine like strcat and check the realloc and tmp pointer carefully but found nothing.


Nov 14 '05 #2
Roy
baumann@pan wrote:
Roy wrote:
Hi all :
My code below :

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

char *cat(char *s, const char *t)
{
char *tmp;
correct here:
if (s == NULL) {
tmp = realloc(s,strle n(t) + 1);
while(*tmp++ = *t++); // copy *t to *tmp.
} else { tmp = realloc(s,strle n(s) + strlen(t) + 1);
if (tmp == NULL) {
fputs("realloc error",stderr);
exit(1);
}
while (*tmp++); // search for '\0' and stop

the above statement is not right, when s = null, what do you expect
the
*tmp is?
Now handled the s=null but still blanks...
tmp--; //the position of '\0'
while (*tmp++ = *t++) ;
}

s = tmp;
return s;
}

int main()
{
char *tmp;
char *s;

s = NULL;
tmp = cat(s,"oh");
printf("%s\n",t mp);
tmp = cat(s,"haha");
printf("%s\n",t mp);
return 0;
}
my problem is when I run the program the reslut is some blanks. I

just
wrote a small routine like strcat and check the realloc and tmp

pointer
carefully but found nothing.


Nov 14 '05 #3
Roy wrote:

Hi all :
My code below :

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

char *cat(char *s, const char *t)
{
char *tmp;

if (s == NULL)
tmp = realloc(s,strle n(t) + 1);
else
tmp = realloc(s,strle n(s) + strlen(t) + 1);
if (tmp == NULL) {
fputs("realloc error",stderr);
exit(1);
}
while (*tmp++); // search for '\0' and stop
tmp--; //the position of '\0'
while (*tmp++ = *t++) ;
s = tmp;
return s;
}

int main()
{
char *tmp;
char *s;

s = NULL;
tmp = cat(s,"oh");
printf("%s\n",t mp);
tmp = cat(s,"haha");
printf("%s\n",t mp);
return 0;
}


char *cat(char *s, const char *t)
{
char *tmp;

if (s == NULL) {
tmp = realloc(s, strlen(t) + 1);
*tmp = '\0';
} else {
tmp = realloc(s, strlen(s) + strlen(t) + 1);
}
if (tmp == NULL) {
fputs("realloc error",stderr);
exit(EXIT_FAILU RE);
}
s = tmp;
while (*tmp++ != '\0') {
;
}
tmp--;
while ((*tmp++ = *t++) != '\0') {
;
}
return s;
}

--
pete
Nov 14 '05 #4


pete wrote:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char *tmp;
char *s;

s = NULL;
tmp = cat(s,"oh");
printf("%s\n",t mp);
tmp = cat(s,"haha");
printf("%s\n",t mp);
return 0;
}

char *cat(char *s, const char *t)
{
char *tmp;

if (s == NULL) {
tmp = realloc(s, strlen(t) + 1);
*tmp = '\0';


Here is a slight flaw. You do not want to assign to *tmp
BEFORE you have checked tmp value for a possible NULL.
} else {
tmp = realloc(s, strlen(s) + strlen(t) + 1);
}
if (tmp == NULL) {
fputs("realloc error",stderr);
exit(EXIT_FAILU RE);
}
s = tmp;
while (*tmp++ != '\0') {
;
}
tmp--;
while ((*tmp++ = *t++) != '\0') {
;
}
return s;
}


This definition will abort the program if dynamic allocation
fails. Another less virulent act on the executable would be
give a signal to the caller, via the return value, or some
other means, that the function was not successful in the
operation.

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

int cat(char **s1, const char *s2);

int main(void)
{
char *s = NULL;

if(cat(&s,"Hell o"))
{
if(cat(&s," and Goodbye"))
printf("s = \"%s\"\n",s) ;
else puts("Memory allocation failure");
}
else puts("Memory allocation failure");
free(s);
return 0;
}

int cat(char **s1, const char *s2)
{
int ret = 0; /* value would indicate failure */

if(s1 && s2)
{
char *tmp;
size_t sz = *s1?strlen(*s1) +strlen(s2)+1:s trlen(s2)+1;
if((tmp = realloc(*s1,sz) ) != NULL)
{
if(!*s1) *tmp = '\0';
strcat(tmp,s2);
*s1 = tmp;
ret = 1; /* value would indicate success */
}
}
return ret;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #5
Al Bowers wrote:

pete wrote:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char *tmp;
char *s;

s = NULL;
tmp = cat(s,"oh");
printf("%s\n",t mp);
tmp = cat(s,"haha");
Probably would be a better demonstration with
tmp = cat(tmp, "haha");
printf("%s\n",t mp);
return 0;
}

char *cat(char *s, const char *t)
{
char *tmp;

if (s == NULL) {
tmp = realloc(s, strlen(t) + 1);
*tmp = '\0';


Here is a slight flaw. You do not want to assign to *tmp
BEFORE you have checked tmp value for a possible NULL.


Thank you.
if (tmp == NULL) {
fputs("realloc error",stderr);
exit(EXIT_FAILU RE);
}

This definition will abort the program if dynamic allocation
fails. Another less virulent act on the executable would be
give a signal to the caller, via the return value, or some
other means, that the function was not successful in the
operation.


/* I'm taking another shot. */

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

char *cat(char *s, const char *t);

int main(void)
{
char *s;

s = cat(NULL, "oh");
if (s != NULL) {
printf("%s\n", s);
}
s = cat(s, "haha");
if (s != NULL) {
printf("%s\n", s);
}
return 0;
}

char *cat(char *s, const char *t)
{
char *tmp;

if (s == NULL) {
tmp = calloc(1, strlen(t) + 1);
} else {
tmp = realloc(s, strlen(s) + strlen(t) + 1);
}
s = tmp;
if (tmp != NULL) {
while (*tmp++ != '\0') {
;
}
tmp--;
while ((*tmp++ = *t++) != '\0') {
;
}
}
return s;
}

--
pete
Nov 14 '05 #6
pete wrote:

Al Bowers wrote:

pete wrote:

/* I'm taking another shot. */

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

char *cat(char *s, const char *t);

int main(void)
{
char *s;

s = cat(NULL, "oh");
if (s != NULL) {
printf("%s\n", s);
}
s = cat(s, "haha");
if (s != NULL) {
printf("%s\n", s);
}
return 0;
}


/*
** I got it into my head that writing cat using strcat,
** might be cheating in some way, but ...
*/

char *cat(char *s, const char *t)
{
char *tmp;

if (s == NULL) {
tmp = calloc(1, strlen(t) + 1);
} else {
tmp = realloc(s, strlen(s) + strlen(t) + 1);
}
s = tmp;
if (s != NULL) {
strcat(s, t);
}
return s;
}

--
pete
Nov 14 '05 #7
Roy wrote:
Hi all :
My code below :

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

char *cat(char *s, const char *t)
{
char *tmp;

if (s == NULL)
tmp = realloc(s,strle n(t) + 1);
At this point, the memory pointed to by tmp will be in a random state.
Also you might as well use malloc since you know s is NULL.
else
tmp = realloc(s,strle n(s) + strlen(t) + 1);
if (tmp == NULL) {
fputs("realloc error",stderr);
exit(1);
}
while (*tmp++); // search for '\0' and stop
So the while loop above could do anything if s is NULL, as it is on your
first call.

Also, you have just had the string searched for a '\0' by calling
strlen, so scanning it again seems a bit silly to me.

Also, please don't use // comments on Usenet. They do not survive line
wrapping and, for your information, were not standard in C89 although
they are a common extension. They are standard in C99, but most
implementations that I am aware of are not conforming C99 implementations .
tmp--; //the position of '\0'
while (*tmp++ = *t++) ;
Again, you know the length and could use memcpy which *may* be more
efficient.
s = tmp;
return s;
}

int main()
{
char *tmp;
char *s;

s = NULL;
tmp = cat(s,"oh");
printf("%s\n",t mp);
tmp = cat(s,"haha");
Here you are using s, but s will still be NULL, and loosing the pointer
you obtained by the previous call to cat.
printf("%s\n",t mp);
return 0;
}
my problem is when I run the program the reslut is some blanks. I just
wrote a small routine like strcat and check the realloc and tmp pointer
carefully but found nothing.


Try the following modified version...

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

char *cat(char *s, const char *t)
{
char *tmp;
size_t slen;
size_t tlen = strlen(t);

if (s == NULL)
slen = 0;
else
slen = strlen(s);
tmp = realloc(s,slen + tlen + 1);

if (tmp == NULL) {
free(s); /* improve chances of the fputs not running out of memory */
fputs("realloc error",stderr);
exit(1);
}
memcpy(tmp+slen ,t,tlen);
tmp[slen+tlen]='\0';
return tmp;
}

int main(void)
{
char *tmp;
char *s = NULL;
tmp = cat(s,"oh");
printf("%s\n",t mp);
s = cat(tmp,"haha") ;
printf("%s\n",s );
return 0;
}
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #8
pete wrote:
pete wrote:

Al Bowers wrote:
>
> pete wrote:

/* I'm taking another shot. */

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

char *cat(char *s, const char *t);

int main(void)
{
char *s;

s = cat(NULL, "oh");
if (s != NULL) {
printf("%s\n", s);
}
s = cat(s, "haha");
if (s != NULL) {
printf("%s\n", s);
}
return 0;
}


/*
** I got it into my head that writing cat using strcat,
** might be cheating in some way, but ...
*/

char *cat(char *s, const char *t)
{
char *tmp;

if (s == NULL) {
tmp = calloc(1, strlen(t) + 1);
} else {
tmp = realloc(s, strlen(s) + strlen(t) + 1);
}
s = tmp;


Why reusing s here, s is only a copy of your s in the main function. So if
you want to change your "main" s you have to use a pointer to a pointer
like All did.
if (s != NULL) {
strcat(s, t);
Maybe the use of malloc/realoc and memcpy is a "little" bit faster than your
solution
}
return s;
}


This one should work

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

#if __STDC_VERSION_ _ < 199901L
# define restrict
#endif

char* concat (char **s, restrict const char *t)
{
char *tmp = NULL;

if (NULL == t) {
if (NULL != s) {
tmp = *s;
}
} else {
size_t length = 1u + strlen(t);

if (NULL == s || NULL == *s) {
tmp = malloc (length);
if (NULL != tmp) {
memcpy(tmp, t, length);
if (NULL != s) {
*s = tmp;
}
}
} else {
size_t old_len = strlen(*s);
tmp = realloc (*s, old_len + length);
if (NULL != tmp) {
memcpy (tmp + old_len, t, length);
*s = tmp;
}
}
}
return tmp;
}

int main(void)
{
char *string = NULL;
char *tmp;

concat(&string, "oh");
printf ("*%p == %s\n", string, string);
tmp = string;
do {
concat(&string, "haha");
} while (tmp == string);
printf ("*%p (%zd) == %s\n", string, strlen(string), string);

free (string);

return EXIT_SUCCESS;
}
--
Michael Knaup
Nov 14 '05 #9
And true you should check against NULL after calling concat
int main(void)
{
char *string = NULL;
char *tmp;

concat(&string, "oh");
if (NULL == string)
return EXIT_FAILURE;
printf ("*%p == %s\n", string, string);
tmp = string;
do { if (NULL == concat(&string, "haha"))
break;
} while (tmp == string);
printf ("*%p (%zd) == %s\n", string, strlen(string), string);

free (string);

return EXIT_SUCCESS;
}


--
Michael Knaup
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2931
by: Michael | last post by:
How would I go about shrinking the buffer that was allocated with new, or expanding it in place? I basically need a realloc equivalent for new. Thanks in advance. Michael
9
2349
by: mordac | last post by:
Hi, writing a heap ADT, need to handle insertion into the heap when it is full. Attempting to use realloc to do this, but realloc is changing the contents of my heap! The following is my incHeapSize function, which is supposed to increase the malloced space for an integer array by 1: Heap incHeapSize(Heap aHeap) { int* tempHeap; aHeap->maxSize++; tempHeap = realloc(aHeap->heapArray, aHeap->maxSize);
7
2922
by: Marlene Stebbins | last post by:
The bigint struct defines a big integer and represents it as a string of characters: typedef struct bigint { int sign; int size; int initflag; char *number; } bigint;
86
4118
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory that was freed by realloc(), but the only way explicitly mentioned in the C89 standard to free memory via realloc() is to realloc() it down to 0 bytes. I had always assumed it would automatically free the previous memory, but is the behaviour...
28
3864
by: bwaichu | last post by:
Is it generally better to set-up a buffer (fixed sized array) and read and write to that buffer even if it is larger than what is being written to it? Or is it better to allocate memory and realloc it for the size of the what is being written each time? In other words, what is the decision factor between deciding to use a fixed size buffer or allocating memory space and reallocing the size? I don't think the code below is optimal...
19
5720
by: ivan.leben | last post by:
Let's say I have a piece of allocated memory which I want to expand and reuse if possible or allocate in a different part of RAM if resizing is not possible, however, in the latter case I don't care about the old data and I don't need to copy it to the new location. Is there a standard function that could do that? As far as I understand the description of the realloc() function, it _always_ copies the data to the new location, even if that...
3
11210
by: anirbid.banerjee | last post by:
#include <stdlib.h> #include <stdio.h> int main(){ char *ptr = "hello"; ptr = (char *)realloc (ptr,(size_t) 10 * sizeof (char )); printf ("\n %s", ptr); return 0; } ___________________________________ The above program while execution dumps a stack trace and exits. This
4
3500
by: Kenneth Brody | last post by:
I looked at my copy of n1124, and I didn't see anything about this particular situation... What happens if you realloc() to a size of zero? Implementations are allowed to return NULL on malloc(0), and realloc() says it reutrns NULL on failure. (And, on failure, the old pointer has not been freed.) Is it possible for an implementation to return NULL for realloc(ptr,0)
9
3796
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
35
5654
by: Bill Cunningham | last post by:
My string.h headers declares two functions I have been using called memfrob and strfry. They are encryption types functions. My man pages say they are standard to linux c and gnu c. They sure aren't in my C books. Interesting functions by they're OT here but this raises to me a question. If a function returns a pointer to a void and and as it's first parameter a pointer to a void. Then should that function's first parameter accept a string...
0
8421
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.