malloc modifying a passed string

S

Scott Taylor

I've searched through the FAQ but I can't find this problem, which seems
like it should be a newbie one. Here is a following code sample,
without the necessary testing of malloc, including of standard
libraries, etc...

main() {
char *ptr;
ptr = malloc(4);
strcpy(ptr, "abc");
some_func(ptr);
...
}

void some_func(char *ptr) {
char *ptr2;
ptr2 = malloc(5);
...
}

When I get to some_func, and malloc any value (as I here did
ptr2=malloc(5) ), some value of *ptr becomes modified. For instance,
*(ptr+3), which was previously equally to '\0', is now equal to char
value 23 ('\023'), and *(ptr+4) now is '\0'. This value isn't
consistently 23. Some times it a '#' character...etc. But it always
seems to be just an addition to the string of one char. Why does this
happen?

When the second malloc is called in some_func, is there any reason why
that the original pointer should be modified. Using a debugger (gdb),
even printing out malloc(1) modifies the buffer.

If there is an easy solution I would love to hear it, or be redirected
to a previous post, the FAQ, or whatever is most applicable.

Thank you in advance,

Scott Taylor
 
C

Chris Hulbert

Scott said:
I've searched through the FAQ but I can't find this problem, which seems
like it should be a newbie one. Here is a following code sample,
without the necessary testing of malloc, including of standard
libraries, etc...

main() {
char *ptr;
ptr = malloc(4);
strcpy(ptr, "abc");
some_func(ptr);
...
}

void some_func(char *ptr) {
char *ptr2;
ptr2 = malloc(5);
...
}

When I get to some_func, and malloc any value (as I here did
ptr2=malloc(5) ), some value of *ptr becomes modified. For instance,
*(ptr+3), which was previously equally to '\0', is now equal to char
value 23 ('\023'), and *(ptr+4) now is '\0'. This value isn't
consistently 23. Some times it a '#' character...etc. But it always
seems to be just an addition to the string of one char. Why does this
happen?

When the second malloc is called in some_func, is there any reason why
that the original pointer should be modified. Using a debugger (gdb),
even printing out malloc(1) modifies the buffer.

If there is an easy solution I would love to hear it, or be redirected
to a previous post, the FAQ, or whatever is most applicable.

Thank you in advance,

Scott Taylor

Since in some_func you don't use ptr in your example, it's not being
modified by malloc. I would look for memory problems elsewhere. As
far as *(ptr+4), that's outside of your allocated space.
 
G

Grumble

Scott said:
I've searched through the FAQ but I can't find this problem, which seems
like it should be a newbie one. Here is a following code sample,
without the necessary testing of malloc, including of standard
libraries, etc...

main() {
char *ptr;
ptr = malloc(4);
strcpy(ptr, "abc");
some_func(ptr);
...
}

void some_func(char *ptr) {
char *ptr2;
ptr2 = malloc(5);
...
}

Could you provide a minimal example that compiles correctly and
exhibits the behavior that you describe?

$ cat foo.c
#include <stdlib.h>
extern int printf(const char *format, ...);
extern char *strcpy(char *dest, const char *src);

void some_func(char *ptr) {
char *ptr2;
ptr2 = malloc(5);
free(ptr2);
}

int main(void) {
char *ptr;
ptr = malloc(4);
strcpy(ptr, "abc");
printf("%s\n", ptr);
some_func(ptr);
printf("%s\n", ptr);
free(ptr);
return 0;
}

$ gcc-3.4.4 -std=c89 -pedantic -Wall -Wextra -O1 foo.c
foo.c:5: warning: unused parameter 'ptr'

$ ./a.out
abc
abc
 
S

Sarin

Hi,

If you want to modify the pointer in your function, u need to pass the
pointer to that pointer. I tried the program below and it worked for
me.

Regards,
Sarin

#include <stdio.h>
#define STRM "My Main String"
#define STRF "My Function String"

void change_ptr(char **ptr);

int main()
{
char *ptr1;
char *ptr2;

ptr1=malloc(sizeof(STRM)+1);
strcpy(ptr1,STRM);

ptr2=ptr1;

printf("ptr1: %s ptr2: %s \n",ptr1,ptr2);

change_ptr(&ptr1);

printf("ptr1: %s ptr2: %s \n",ptr1,ptr2);

return 0;
}

void change_ptr(char **ptr)
{

*ptr=malloc(sizeof(STRF)+1);
strcpy(*ptr,STRF);

}
 
C

CBFalconer

Scott said:
I've searched through the FAQ but I can't find this problem, which
seems like it should be a newbie one. Here is a following code
sample, without the necessary testing of malloc, including of
standard libraries, etc...

main() {

That's int main(void), BTW. Post a complete, compilable version
which displays the problem. The crystal balls are on strike.
 

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,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top