473,388 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

malloc modifying a passed string


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
Nov 15 '05 #1
4 1712
Scott Taylor wrote:
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.

Nov 15 '05 #2
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);

}

Nov 15 '05 #3
Scott Taylor wrote:
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

--
Regards, Grumble
Nov 15 '05 #4
Scott Taylor wrote:

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.

--
"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
Nov 15 '05 #5

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

Similar topics

11
by: Mannequin* | last post by:
Hi all, I'm working on a quick program to bring the Bible into memory from a text file. Anyway, I have three questions to ask. First, is my implementation of malloc () correct in the program to...
33
by: hermit_crab67 | last post by:
Can someone explain to a C newbie why this doesn't work as I expect it to work? (expectations clearly outlined in the printf statement in main routine) OS: Linux 2.4.26 GCC: 2.95.4 void...
20
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
11
by: lohith.matad | last post by:
Hi all, Though the purpose of both malloc() and calloc() is the same, and as we also know that calloc() initializes the alloacted locations to 'zero', and also that malloc() is used for bytes...
24
by: Hrv'uljak | last post by:
Anybody has a better solution? How to avoid memory allocation in main function? Thanks! -------- #include <stdio.h> #include <conio.h> #include <string.h> #include <malloc.h>
17
by: Johs32 | last post by:
When I make a pointer I have read that if I would like to use it in another function, I need to malloc it first else it will disapear after the function returns. In this code I do not use malloc,...
4
by: Silas Silva | last post by:
Hello all! I'm learning C and I started to get into malloc() and va_* functions of stdarg.h (va_list, va_arg, va_start, etc). So I wrote the following program that has a funcion that...
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
25
by: jbholman | last post by:
I am pretty new to C and doing my first project in C. I actually read almost the entire FAQ, but can't seem to figure out this problem. I have a structure. I have a list of these structures. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...

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.