Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Avoid using malloc?

Reply
Thread Tools

Avoid using malloc?

 
 
Johs32
Guest
Posts: n/a
 
      03-02-2006
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
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      03-02-2006
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)
 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      03-02-2006
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.
 
Reply With Quote
 
Kevin Handy
Guest
Posts: n/a
 
      03-02-2006
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 =----
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-02-2006
Johs32 <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Johs32
Guest
Posts: n/a
 
      03-02-2006
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;
}

 
Reply With Quote
 
Pedro Graca
Guest
Posts: n/a
 
      03-02-2006
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>
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-02-2006
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)
 
Reply With Quote
 
Johs32
Guest
Posts: n/a
 
      03-02-2006
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?
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-02-2006
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using enums to avoid using switch/if aks_java Java 30 03-25-2010 11:48 AM
Avoid having a SQL express for web parts and avoid personalization Roger23 ASP .Net 2 10-12-2006 10:54 PM
using TagPrefix to avoid having @ Register directives on pages using custom controls Shan McArthur ASP .Net Building Controls 2 06-30-2005 02:37 PM
Using Command object to avoid dynamic sql Bob Barrows [MVP] ASP .Net 1 04-05-2005 12:57 PM
Avoid wasting time or how to avoid initialization Alexander Malkis C++ 8 04-13-2004 11:23 PM



Advertisments