C malloc

Get your foo on.

C malloc

Postby nadir » April 5th, 2012, 7:24 pm

It would probably suit better in a C forum, but they will a) give me the business straight away and b) and answer which would also suit to answer how to build an operating system.

I only want to know if it is the right direction, not if it makes perfect sense.
Here is the code:
Code: Select all
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void print_strings(char *original, char *copy);

int main(int argc, char *argv[])
{
   char *orig = (char *)malloc(strlen("hello")+1);
   if ( orig == NULL ) {
      puts("error: oom"); //oom: out of memory
      return 1;
   } else {
      strcpy(orig, "hello");
   }

   char *copy = (char *)malloc(strlen("h"));  //what the feck was that supposed to do??? probably something with realloc
   
   char *newcopy = (char *)malloc(sizeof(orig));
   if ( newcopy != NULL ) {
      copy = newcopy;
   } else {
      puts("error: out of memory");
      return 1;
   }


   strcpy(copy,orig);

   print_strings(orig, copy);  //dang, no error message, this way or that way
                //... grml

   return 0;
}

void print_strings(char *o, char *c)
{
   printf("original: %s\ncopy: %s\n", o, c);
}


My questions are the following:
If i want to put a string in a char-array, is the usage of malloc right:
char *orig = (char *)malloc(strlen("hello")+1);

If i want to make a second char-array, of the same size like an existing one, is this right:
char *newcopy = (char *)malloc(sizeof(orig));
(i mean using sizeof(the_char-array_size_i_want).

Last but not least: I could swear i have read that i need to check the return value of free().
If i try it fails. I searched the web, but ... well: search for something with "free", and you know what problems i got)

Any clear word on memory allocation are welcome (i think i am getting closer, but would not bet).
Thanks
Last edited by nadir on April 6th, 2012, 7:16 am, edited 1 time in total.
nadir
 
Posts: 1708
Joined: February 9th, 2011, 8:07 am

Re: C malloc

Postby MrJames » April 5th, 2012, 9:04 pm

1) Ask yourself is "do I need dynamic allocation in the first place"?
2) use strdup.
3) you do not need the casts.
This signature is not available in your country.
User avatar
MrJames
 
Posts: 567
Joined: February 9th, 2011, 8:27 pm

Re: C malloc

Postby nadir » April 6th, 2012, 7:14 am

What?

Can you make it look as if the answer would be somehow related to what i posted?
nadir
 
Posts: 1708
Joined: February 9th, 2011, 8:07 am

Re: C malloc

Postby MrJames » April 6th, 2012, 8:52 am

What?

How does my post not relate?

The first point was that since you are allocating a memory block of size known at compile time and not a calculated value based on some runtime generated variable, you can use static allocation instead (char array).
The second point was about using strdup. The strdup function takes a string and simultaneously dynamically allocates (mallocs) a new block of the correct size and then copies the contents of the input string into that block and returns a pointer to it (you then use 'free' on it as if you used 'malloc').
The third point is that in c you do not need to cast (the (char *) thing) the result of malloc. C is not like C++ in this regard.
This signature is not available in your country.
User avatar
MrJames
 
Posts: 567
Joined: February 9th, 2011, 8:27 pm

Re: C malloc

Postby nadir » April 6th, 2012, 9:07 am

ah, i see.
Well i know that it does not make much sense to use malloc here. The question was more about how to use malloc, not if it makes sense here.
I should have pointed that out more clearly.
It is not easy to find an example where it really would make sense (at my level, not in general). Right now i use this:
http://www.eskimo.com/~scs/cclass/notes/sx11c.html
To put it different: i simply malloc memory to all and everything, simply to understand it.

The third point ( the cast thing) means that i can go for a simple:
ip = malloc(blah)
instead of:
ip = (char *)malloc(blah)
You seem to have been very clear, but i don't fully understand the word "cast". I read it all the time, but nowhere is it explained in a way i could understand. Hence i ask if i really understood you correct.


PS: not that it would be idle to stress that one should do things if they make sense only. I got a hang to do things all the time, without that they would make sense (without that they would be needed).
nadir
 
Posts: 1708
Joined: February 9th, 2011, 8:07 am

Re: C malloc

Postby Beewolf » April 10th, 2012, 2:12 pm

Unless you're working with variable-length arrays, sizeof will just return the size of the pointer referring to the array. strlen() will tell you the length of a string, but the length doesn't include the null byte at the end, so to make enough room for a copy you need to malloc(strlen(orig) + 1).
nadir wrote:The third point ( the cast thing) means that i can go for a simple:
ip = malloc(blah)
instead of:
ip = (char *)malloc(blah)
You seem to have been very clear, but i don't fully understand the word "cast". I read it all the time, but nowhere is it explained in a way i could understand. Hence i ask if i really understood you correct.
"Casting" just refers to taking a pointer of one type(in this case void*) and using it as a pointer of another type(in this case char*). In C, void pointers should always be implicitly cast(eg, always just assign malloc's result to a correctly-typed pointer) since this maximizes the number of bugs the compiler can catch; the sole exception to this rule are files that are shared with C++ code, due to weaknesses in that language's design committee.
Llewellyn H. Rockwell Jr. wrote:Data points on their own convey no theory, suggest no conclusions, and offer no truths. To arrive at truth requires the most important step that we as human beings can ever take: thinking. Through this thinking, and with good teaching and reading, we can put together a coherent theoretical apparatus that helps us understand.
User avatar
Beewolf
 
Posts: 467
Joined: February 9th, 2011, 4:37 pm

Re: C malloc

Postby nadir » April 10th, 2012, 9:30 pm

Either i am dumb or that, cast-usage, is the opposite of what Mr.James says above
(not that it would make much difference right now, but i would like to get used to do it the right way from the beginning).
nadir
 
Posts: 1708
Joined: February 9th, 2011, 8:07 am

Re: C malloc

Postby Beewolf » April 11th, 2012, 1:08 pm

I'm not sure how it contradicts what MrJames wrote; he said you didn't need to cast the result of malloc, I said you didn't need to cast any void pointer(the return type of malloc) in C and added that you really shouldn't cast any void pointer unless you need the code to compile as C++ as well.
Llewellyn H. Rockwell Jr. wrote:Data points on their own convey no theory, suggest no conclusions, and offer no truths. To arrive at truth requires the most important step that we as human beings can ever take: thinking. Through this thinking, and with good teaching and reading, we can put together a coherent theoretical apparatus that helps us understand.
User avatar
Beewolf
 
Posts: 467
Joined: February 9th, 2011, 4:37 pm

Re: C malloc

Postby nadir » April 11th, 2012, 9:05 pm

In C, void pointers should always be implicitly cast(eg, always just assign malloc's result to a correctly-typed pointer)

I can't read that as:
I said you didn't need to cast any void pointer(the return type of malloc) in C and added that you really shouldn't cast any void pointer

and to me the one looks like the opposite of the other. I probably misunderstand what you mean with "implicitly". I read it as typing (char *)

For example here:
http://www.gnu.org/software/libc/manual ... tion-and-C
it is getting done.
nadir
 
Posts: 1708
Joined: February 9th, 2011, 8:07 am

Re: C malloc

Postby MrJames » April 15th, 2012, 11:29 am

implicit = "done for you behind your back"
explicit = "you have to tell it to do so yourself"

Look, In C, the language specifies that a pointer to void will be automatically "converted (casted) behind your back (implicitly)" to whatever pointer type you are assigning it to.
This signature is not available in your country.
User avatar
MrJames
 
Posts: 567
Joined: February 9th, 2011, 8:27 pm

Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

x