Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Avoid using malloc? (http://www.velocityreviews.com/forums/t441601-avoid-using-malloc.html)

Johs32 03-02-2006 06:45 PM

Avoid using malloc?
 
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, but it still works and
the function print_me() prints the correct text.

Why does it work eventhough I do not use malloc?

johs

Richard Heathfield 03-02-2006 07:21 PM

Re: Avoid using malloc?
 
Johs32 said:

> 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


In which code?

--
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)

Michael Mair 03-02-2006 07:22 PM

Re: Avoid using malloc?
 
Johs32 schrieb:
> 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, but it still works and
> the function print_me() prints the correct text.
>
> Why does it work eventhough I do not use malloc?


Please provide compiling code, ideally stripped down to the
situation you are referring to -- otherwise there is always
the danger of misunderstandings.

Example 1: The following is legal:

void foo (long *bar);
void baz (void);

int main (void)
{
baz();
return 0;
}

void baz (void)
{
long qux = 17;
long *quux = &qux;
foo(quux);
}

void foo (long *bar)
{
*bar /= 2;
}

Example 2: The following is not legal:

long *foo (void);
void baz (void);

int main (void)
{
baz();
return 0;
}

void baz (void)
{
long *quux = foo();
*quux -= 2;
}

void foo (long *bar)
{
long bar = 42;
long *qux = &bar;
return qux;
}

I did not test either example.
Even if you are in the situation of example 2, it is
perfectly possible that it works -- now. This can
change when compiling it on another operating system,
platform or with another compiler or even when just
adding another function to your code.

Example 3: malloc()-Version of Example 2; legal

#include <stdlib.h>

long *foo (void);
void baz (void);

int main (void)
{
baz();
return 0;
}

void baz (void)
{
long *quux = foo();
if (quux != NULL) {
*quux -= 2;
}
free(quux);
}

void foo (long *bar)
{
long *qux = malloc(sizeof *qux);
if (qux != NULL) {
*qux = 42;
}
return qux;
}


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.

Kevin Handy 03-02-2006 07:50 PM

Re: Avoid using malloc?
 
Johs32 wrote:
> 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, but it still works and
> the function print_me() prints the correct text.
>
> Why does it work eventhough I do not use malloc?


Blind stupid luck is the most likely answer.

If that is the level of reliability you are looking
for in your programs, then go ahead and use it.

btw: What code?

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Keith Thompson 03-02-2006 08:15 PM

Re: Avoid using malloc?
 
Johs32 <dfgdg@dsf.com> writes:
> 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, but it still works and
> the function print_me() prints the correct text.
>
> Why does it work eventhough I do not use malloc?


Why does *what* work? How do you "make" the pointer?

Show us some code, preferably a complete program.

But first, take a look at questions 7.5a and 7.5b in the comp.lang.c
FAQ, <http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Johs32 03-02-2006 08:16 PM

Re: Avoid using malloc?
 
ups forgot the code:


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

void print_me(char * string)
{


printf("%s\n",string);
}


int main()
{
char *string = "bopla";
print_me(string);
return 0;
}


Pedro Graca 03-02-2006 08:17 PM

Re: Avoid using malloc?
 
Johs32 wrote:
> Why does it work eventhough I do not use malloc?


Undefined behaviour includes doing what the programmer expects.

--
If you're posting through Google read <http://cfaj.freeshell.org/google>

Richard Heathfield 03-02-2006 08:27 PM

Re: Avoid using malloc?
 
Johs32 said:

> #include<stdio.h>
> #include<stdlib.h>
>
> void print_me(char * string)
> {
> printf("%s\n",string);
> }
>
>
> int main()
> {
> char *string = "bopla";


This pointer points to a string literal, which has static storage duration
and therefore exists for the duration of the program. Nothing wrong with
this code (although I'd make it const char * if you're pointing at a string
literal).

--
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)

Johs32 03-02-2006 08:30 PM

Re: Avoid using malloc?
 
Richard Heathfield wrote:

> Johs32 said:
>
>> #include<stdio.h>
>> #include<stdlib.h>
>>
>> void print_me(char * string)
>> {
>> printf("%s\n",string);
>> }
>>
>>
>> int main()
>> {
>> char *string = "bopla";

>
> This pointer points to a string literal, which has static storage duration
> and therefore exists for the duration of the program. Nothing wrong with
> this code (although I'd make it const char * if you're pointing at a
> string literal).
>



Ok but what is *string was a pointer to a struct or something that is not a
"literal", can I still omit malloc?

Richard Heathfield 03-02-2006 08:42 PM

Re: Avoid using malloc?
 
Johs32 said:

> Ok but what is *string was a pointer to a struct or something that is not
> a "literal", can I still omit malloc?


If you need to store data, you need somewhere to store it.

String literals are sorted out for you. If you wish to capture and store a
string at runtime, you will need to allocate storage for it. The malloc
function offers one way to do this.


--
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)


All times are GMT. The time now is 06:03 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.