473,399 Members | 2,278 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,399 software developers and data experts.

malloc Q

This is probably pretty silly for those in the know, but it's been a while
since I've muddled about with malloc....

I have the following snippet of code:

char *letters_array[6];

......

int fill_letters(char *letterString,char *buffer) {
letterString = malloc(strlen(buffer)+1);
sprintf(letterString,"%s",buffer);
printf("fill: >%s<, %d\n",buffer,strlen(buffer)+1);
}

int load_default_charset() {
printf("in load_default_charset\n");
fill_letters(letters_array[0],"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
printf("filled: >%s<,\n",letters_array[0]);
...
return TRUE;
}

It seems that the memory allocated in fill_letters is being released as
soon as that function returns (the printf in load_default_charset shows
(NULL) for letters_array[0]). I would have thought that since I'm passing
pointers the malloc would be "sticky" and not be freed until a call to
free().

So, how do I rewrite the code to insure that the memory remains allocated?
I could pass just the index for letters_array, but for maintenance and
readability I would prefer to pass the pointer itself.

--Kamus
Nov 14 '05 #1
8 1217
Kamus of Kadizhar <ya*@nsoesipnaemr.com> spoke thus:
This is probably pretty silly for those in the know, but it's been a while
since I've muddled about with malloc.... I have the following snippet of code: char *letters_array[6]; ..... int fill_letters( char *letterString, char *buffer ) {
letterString=malloc( strlen(buffer)+1 ); ^^^^^^^^^^^^ What if malloc() fails? Kablooie? sprintf( letterString, "%s", buffer );
printf( "fill: >%s<, %d\n", buffer, strlen(buffer)+1 );
} int load_default_charset() {
printf( "in load_default_charset\n" );
fill_letters( letters_array[0], "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
printf( "filled: >%s<,\n", letters_array[0] );
...
return TRUE; ^^^^ presumably #define'd appropriately? } It seems that the memory allocated in fill_letters is being released as
soon as that function returns (the printf in load_default_charset shows
(NULL) for letters_array[0]). I would have thought that since I'm passing
pointers the malloc would be "sticky" and not be freed until a call to
free().


(I've taken the liberty of applying my code-spacing paradigm to yours.)

malloc() is behaving the way you think it should - the mistake you're
making is in how you're passing its return value around. No matter
what happens in fill_letters, letters_array[0] will be unchanged:
parameters are passed by value in C, so assigning to letterString has
no meaning outside fill_letters(). What you want is something like

int fill_letters( char **letterString, char *buffer ) {
*letterString=malloc( strlen(buffer) );
...
}

and

int load_default_charset() {
...
fill_letters( &letters_array[0], ... );
...
}

See how this works?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #2
Christopher Benson-Manica <at***@nospam.cyberspace.org> spoke thus:
int fill_letters( char **letterString, char *buffer ) {
*letterString=malloc( strlen(buffer) ); strlen(buffer)+1, of course, as in the
original! *sigh* ...
}


--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3
On Thu, 19 Feb 2004, Kamus of Kadizhar wrote:
This is probably pretty silly for those in the know, but it's been a while
since I've muddled about with malloc....

I have the following snippet of code:

char *letters_array[6];

.....

int fill_letters(char *letterString,char *buffer) {
letterString = malloc(strlen(buffer)+1);
sprintf(letterString,"%s",buffer);
printf("fill: >%s<, %d\n",buffer,strlen(buffer)+1);
}
The problem is in this function. The variable letterString is a COPY of
letters[0]. The result of malloc is being stored in the copy and not the
original. When you leave the scope of fill_letters the result of malloc is
lost. One possible solution is to change fill_letters to take a char**,
malloc to *letterString and pass in &letters_array[0].
int load_default_charset() {
printf("in load_default_charset\n");
fill_letters(letters_array[0],"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
printf("filled: >%s<,\n",letters_array[0]);
...
return TRUE;
}

It seems that the memory allocated in fill_letters is being released as
soon as that function returns (the printf in load_default_charset shows
(NULL) for letters_array[0]). I would have thought that since I'm passing
pointers the malloc would be "sticky" and not be freed until a call to
free().

So, how do I rewrite the code to insure that the memory remains allocated?
I could pass just the index for letters_array, but for maintenance and
readability I would prefer to pass the pointer itself.

--Kamus


--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #4


Kamus of Kadizhar wrote:
This is probably pretty silly for those in the know, but it's been a while
since I've muddled about with malloc....

I have the following snippet of code:

char *letters_array[6];

.....

int fill_letters(char *letterString,char *buffer) {
letterString = malloc(strlen(buffer)+1);
sprintf(letterString,"%s",buffer);
printf("fill: >%s<, %d\n",buffer,strlen(buffer)+1);
}

int load_default_charset() {
printf("in load_default_charset\n");
fill_letters(letters_array[0],"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
printf("filled: >%s<,\n",letters_array[0]);
...
return TRUE;
}

It seems that the memory allocated in fill_letters is being released as
soon as that function returns (the printf in load_default_charset shows
(NULL) for letters_array[0]). I would have thought that since I'm passing
pointers the malloc would be "sticky" and not be freed until a call to
free().

So, how do I rewrite the code to insure that the memory remains allocated?
I could pass just the index for letters_array, but for maintenance and
readability I would prefer to pass the pointer itself.


The problem here is not that the memory was not allocated, it is that once you

exit the function fill_letters, the value returned by malloc, representing a
pointer
to the space is lost. Although it may impair your readibility, you should
redesign
the function. For example, define the function as:
int fill_letters(char **letterString,char *buffer) {
*letterString = malloc(strlen(buffer)+1);
if(*letterString == NULL) return 0;
sprintf(*letterString,"%s",buffer);
printf("fill: >%s<, %d\n",buffer,strlen(buffer)+1);
return 1;
}

Then modify the function load_default_charset to:

void load_default_charset(char **p) {
printf("in load_default_charset\n");
if(fill_letters( &p[0],"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
printf("filled: >%s<,\n",p[0]);
else puts("Failure to store the charset");
}

And to use the functions:

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

/* Prototypes */
int fill_letters(char **letterString,char *buffer);
void load_default_charset(char **p);

int main(void)
{
char *letters_array[6];

load_default_charset(letters_array);
if(letters_array[0])
printf("\nletters_array[0] = \"%s\"\n",
letters_array[0]);
free(letters_array[0]);
return 0;
}

int fill_letters(char **letterString,char *buffer) {
*letterString = malloc(strlen(buffer)+1);
if(*letterString == NULL) return 0;
sprintf(*letterString,"%s",buffer);
printf("fill: >%s<, %d\n",buffer,strlen(buffer)+1);
return 1;
}

void load_default_charset(char **p) {
printf("in load_default_charset\n");
if(fill_letters( &p[0],"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
printf("filled: >%s<,\n",p[0]);
else puts("Failure to store the charset");
}

Nov 14 '05 #5
On Thu, 19 Feb 2004 13:54:53 +0000, Christopher Benson-Manica wrote:
Kamus of Kadizhar <ya*@nsoesipnaemr.com> spoke thus:
This is probably pretty silly for those in the know, but it's been a
while since I've muddled about with malloc....

I have the following snippet of code:

char *letters_array[6];

.....

int fill_letters( char *letterString, char *buffer ) {
letterString=malloc( strlen(buffer)+1 );

^^^^^^^^^^^^ What if malloc() fails? Kablooie?


Well, actually, the call to malloc in the original is a wrapper to the
that handles that. I didn't want to muddy the waters....
sprintf( letterString, "%s", buffer ); printf( "fill: >%s<, %d\n",
buffer, strlen(buffer)+1 ); }

int load_default_charset() {
printf( "in load_default_charset\n" ); fill_letters(
letters_array[0], "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ); printf( "filled:
>%s<,\n", letters_array[0] ); ...

return TRUE;

^^^^ presumably #define'd appropriately?
}

It seems that the memory allocated in fill_letters is being released as
soon as that function returns (the printf in load_default_charset shows
(NULL) for letters_array[0]). I would have thought that since I'm
passing pointers the malloc would be "sticky" and not be freed until a
call to free().


(I've taken the liberty of applying my code-spacing paradigm to yours.)

malloc() is behaving the way you think it should - the mistake you're
making is in how you're passing its return value around. No matter what
happens in fill_letters, letters_array[0] will be unchanged: parameters
are passed by value in C, so assigning to letterString has no meaning
outside fill_letters(). What you want is something like

int fill_letters( char **letterString, char *buffer ) {
*letterString=malloc( strlen(buffer) ); ...
}

and

int load_default_charset() {
...
fill_letters( &letters_array[0], ... ); ...
}

See how this works?


But wait a minute - letters_array is an array of pointers to char. So
wouldn't I be passing a pointer by passing letters_array[0]? And wouldn't
&letters_array[0] be the address of the array itself (and thus a char **)?
That was my original thought.

God that makes my brain hurt. And to think I used to teach this
stuff.... Senility is a terrible thing. :-)

--Kamus
Nov 14 '05 #6


Kamus of Kadizhar wrote:
On Thu, 19 Feb 2004 13:54:53 +0000, Christopher Benson-Manica wrote:
Kamus of Kadizhar <ya*@nsoesipnaemr.com> spoke thus:
This is probably pretty silly for those in the know, but it's been a
while since I've muddled about with malloc....

I have the following snippet of code:

char *letters_array[6];

.....

int fill_letters( char *letterString, char *buffer ) {
letterString=malloc( strlen(buffer)+1 );

^^^^^^^^^^^^ What if malloc() fails? Kablooie?


Well, actually, the call to malloc in the original is a wrapper to the
that handles that. I didn't want to muddy the waters....
sprintf( letterString, "%s", buffer ); printf( "fill: >%s<, %d\n",
buffer, strlen(buffer)+1 ); }

int load_default_charset() {
printf( "in load_default_charset\n" ); fill_letters(
letters_array[0], "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ); printf( "filled:
>%s<,\n", letters_array[0] ); ...
return TRUE;

^^^^ presumably #define'd appropriately?
}

It seems that the memory allocated in fill_letters is being released as
soon as that function returns (the printf in load_default_charset shows
(NULL) for letters_array[0]). I would have thought that since I'm
passing pointers the malloc would be "sticky" and not be freed until a
call to free().


(I've taken the liberty of applying my code-spacing paradigm to yours.)

malloc() is behaving the way you think it should - the mistake you're
making is in how you're passing its return value around. No matter what
happens in fill_letters, letters_array[0] will be unchanged: parameters
are passed by value in C, so assigning to letterString has no meaning
outside fill_letters(). What you want is something like

int fill_letters( char **letterString, char *buffer ) {
*letterString=malloc( strlen(buffer) ); ...
}

and

int load_default_charset() {
...
fill_letters( &letters_array[0], ... ); ...
}

See how this works?


But wait a minute - letters_array is an array of pointers to char. So
wouldn't I be passing a pointer by passing letters_array[0]? And wouldn't
&letters_array[0] be the address of the array itself (and thus a char **)?
That was my original thought.


Read faq: http://www.eskimo.com/~scs/C-faq/q4.8.html

You designed your function to accept a char * argument.
You call it with the argument letters_array[0]. This argument is a
copy of the value. You then modify that value with function malloc. The
copy is changed and then promply lost when you exit the function. The
original value in letters_array[0] remain unchanged.
Nov 14 '05 #7
On Thu, 19 Feb 2004 09:16:20 -0600, Lewis Bowers wrote:

Read faq: http://www.eskimo.com/~scs/C-faq/q4.8.html

You designed your function to accept a char * argument.
You call it with the argument letters_array[0]. This argument is a
copy of the value. You then modify that value with function malloc. The
copy is changed and then promply lost when you exit the function. The
original value in letters_array[0] remain unchanged.


OK, a light begins to dawn....

Thanks,

--Kamus
Nov 14 '05 #8
Kamus of Kadizhar wrote:

This is probably pretty silly for those in the know, but it's been a while
since I've muddled about with malloc....

This is a FAQ:
http://www.eskimo.com/~scs/C-faq/q4.8.html


Brian Rodenborn
Nov 14 '05 #9

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

Similar topics

19
by: john smith | last post by:
Can someone please explain to me what is happening when I do a malloc(0). This is what I did. int* p = (int*)malloc(0); Then I printed the value of p and of course it was non-null. But...
34
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
20
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
15
by: Martin Jørgensen | last post by:
Hi, I have a (bigger) program with about 15-30 malloc's in it (too big to post it here)... The last thing I tried today was to add yet another malloc **two_dimensional_data. But I found out that...
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
40
by: Why Tea | last post by:
What happens to the pointer below? SomeStruct *p; p = malloc(100*sizeof(SomeStruct)); /* without a cast */ return((void *)(p+1)); /* will the returned pointer point to the 2nd...
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...
23
by: raphfrk | last post by:
I am having an issue with malloc and gcc. Is there something wrong with my code or is this a compiler bug ? I am running this program: #include <stdio.h> #include <stdlib.h> typedef...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.