Question about a malloc char[] statement in C tutorial.

[purple]
In a tutorial I'm following it shows that to allocate just enough memory to hold the string to do this:

[code]
s.comment = (char *)malloc(sizeof(char[strlen(comm)+1]));
[/code]

I know that arrays need to have a constant expression from when I was learning C++. My compiler (bcc32) also complains that it requires a constant expression.

Why is the tutorial showing this? Am I missing something?
[/purple]

Comments

  • [b][red]This message was edited by Jonathan at 2004-1-12 16:3:56[/red][/b][hr]
    : [purple]
    : In a tutorial I'm following it shows that to allocate just enough memory to hold the string to do this:
    :
    : [code]
    : s.comment = (char *)malloc(sizeof(char[strlen(comm)+1]));
    : [/code]
    :
    : I know that arrays need to have a constant expression from when I was learning C++. My compiler (bcc32) also complains that it requires a constant expression.
    :
    : Why is the tutorial showing this? Am I missing something?
    : [/purple]
    :
    That isn't declaring an array, but is rather allocating memory that is the size of a char array, supposedly - it's kinda clumsy though (I don't know it'd work, but I'm pretty sure that's what it's trying to do). You'd probably get the same effect with:-

    s.comment = malloc(strlen(comm) + 1);

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");



  • [b][red]This message was edited by Extrasolar at 2004-1-13 12:31:12[/red][/b][hr]
    [purple]
    Here is the code as it is in the tutorial.

    [code]
    [red]#include

    int main()
    {[/red]
    typedef struct
    {
    char name[21];
    char city[21];
    char phone[21];
    char *comment;
    } Addr;
    Addr s;
    char comm[100];

    gets(s.name, 20);
    gets(s.city, 20);
    gets(s.phone, 20);
    gets(comm, 100);
    s.comment =
    (char *)malloc(sizeof(char[strlen(comm)+1]));
    strcpy(s.comment, comm);

    [red]return 0;
    }[/red]
    [/code]
    Only the part in black is listed on the tutorial. I've added the rest.
    [/purple]


  • [b][red]This message was edited by stober at 2004-1-13 14:5:53[/red][/b][hr]
    : [b][red]This message was edited by Extrasolar at 2004-1-13 12:31:12[/red][/b][hr]
    : [purple]
    : Here is the code as it is in the tutorial.
    :
    : [code]
    : [red]#include
    :
    : int main()
    : {[/red]
    : typedef struct
    : {
    : char name[21];
    : char city[21];
    : char phone[21];
    : char *comment;
    : } Addr;
    : Addr s;
    : char comm[100];
    :
    [red]// gets only takes one argument, and that is why most programmers
    // do not use that function. If you type more than 20 characters (in your
    // case) gets will crash your system. You should use fgets instead of gets()[/red]

    [blue]fgets(s.name,sizeof(s.name)-1,stdin);[/blue]


    : gets(s.name, 20);
    : gets(s.city, 20);
    : gets(s.phone, 20);
    : gets(comm, 100);
    [red]That tutorial is NUTS![/red]
    : s.comment = [blue](char *)malloc(strlen(comm)+1);[/blue]

    : (char *)malloc(sizeof(char[strlen(comm)+1]));
    : strcpy(s.comment, comm);

    [red]you can also duplicate strings like this:[/red]
    [blue] s.comment = strdup(comm);[/blue]
    :
    : [red]return 0;
    : }[/red]
    : [/code]
    : Only the part in black is listed on the tutorial. I've added the rest.
    : [/purple]
    :
    :
    :







  • [purple]
    Cheers. I'll find a better tutorial. Why is that code written like that then? It's off Howstuffworks.com. Looked good with all the diagrams and stuff to show how it worked.
    [/purple]
    :
    : : [purple]
    : : Here is the code as it is in the tutorial.
    : :
    : : [code]
    : : [red]#include
    : :
    : : int main()
    : : {[/red]
    : : typedef struct
    : : {
    : : char name[21];
    : : char city[21];
    : : char phone[21];
    : : char *comment;
    : : } Addr;
    : : Addr s;
    : : char comm[100];
    : :
    : [red]// gets only takes one argument, and that is why most programmers
    : // do not use that function. If you type more than 20 characters (in your
    : // case) gets will crash your system. You should use fgets instead of gets()[/red]
    :
    : [blue]fgets(s.name,sizeof(s.name)-1,stdin);[/blue]
    :
    :
    : : gets(s.name, 20);
    : : gets(s.city, 20);
    : : gets(s.phone, 20);
    : : gets(comm, 100);
    : [red]That tutorial is NUTS![/red]
    : : s.comment = [blue](char *)malloc(strlen(comm)+1);[/blue]
    :
    : : (char *)malloc(sizeof(char[strlen(comm)+1]));
    : : strcpy(s.comment, comm);
    :
    : [red]you can also duplicate strings like this:[/red]
    : [blue] s.comment = strdup(comm);[/blue]
    : :
    : : [red]return 0;
    : : }[/red]
    : : [/code]
    : : Only the part in black is listed on the tutorial. I've added the rest.
    : : [/purple]
  • : [purple]
    : Cheers. I'll find a better tutorial. Why is that code written like that then? It's off Howstuffworks.com. Looked good with all the diagrams and stuff to show how it worked.
    : [/purple]
    : :


    online tutorials should be used only as a last resort because none of them are all that good becauwse of space and size limitations. Best way to learn is college courses. If you can't do that then buy an Intro to C/C++ book such as one of the Teach Yourself books.
  • : : [purple]
    : : Cheers. I'll find a better tutorial. Why is that code written like that then? It's off Howstuffworks.com. Looked good with all the diagrams and stuff to show how it worked.
    : : [/purple]
    : : :
    :
    :
    : online tutorials should be used only as a last resort because none of them are all that good becauwse of space and size limitations. Best way to learn is college courses. If you can't do that then buy an Intro to C/C++ book such as one of the Teach Yourself books.
    :
    [blue]Just wanted to add that below:
    [code]
    malloc (sizeof (char) * (strlen () + 1));
    [/code]
    is more portable then both versions from previous posts.[/blue]
  • : [blue]Just wanted to add that below:
    : [code]
    : malloc (sizeof (char) * (strlen () + 1));
    : [/code]
    : is more portable then both versions from previous posts.[/blue]
    :

    Actually its not -- sizeof(char) is always 1 (by definition) on every os and compiler. There are NO instances where sizeof(char) != 1.
  • : : [purple]
    : : Cheers. I'll find a better tutorial. Why is that code written like that then? It's off Howstuffworks.com. Looked good with all the diagrams and stuff to show how it worked.
    : : [/purple]
    : : :
    :
    :
    : online tutorials should be used only as a last resort because none of them are all that good becauwse of space and size limitations. Best way to learn is college courses. If you can't do that then buy an Intro to C/C++ book such as one of the Teach Yourself books.
    :
    [purple]
    I was learning C++ from "Teach yourself in 24 hours" by Jesse Liberty. Pretty good. I just wanted to learn the differeneces between C++ and C (apart from the obvious) and to learn C's syntax. Yeah I know I'm doing it backwards. :)

    I'll see how I get on with online tutorials for the moment and then perhaps buy a book. In the UK we have home learning companies (as i suppose does USA) like Learn Direct and ICS that do programming courses. I'm looking in to them. I'll also see about taking night classes at collage. :)
    [/purple]

  • : : [blue]Just wanted to add that below:
    : : [code]
    : : malloc (sizeof (char) * (strlen () + 1));
    : : [/code]
    : : is more portable then both versions from previous posts.[/blue]
    : :
    :
    : Actually its not -- sizeof(char) is always 1 (by definition) on every os and compiler. There are NO instances where sizeof(char) != 1.
    :
    [blue]Isn't that a contradiction? Some machines can have a 32bit 'char', yet you say that sizeof(char) on these machines will return 1 byte instead of 4 bytes? ...hmmm... strange... MSDN says:

    "...The [b]sizeof[/b] operator gives the amount of storage, in [b]bytes[/b], required to store an object of the type of the operand. This operator allows you to avoid specifying machine-dependent data sizes in your programs."
    [/blue]
  • Back to your original problem, It's possible not having stdlib.h or some form of alloc.h included was the culprit. I don't know that stdio knows of malloc. Before using dynamic memory though, you should always check the return value from the call. If you don't, you will trash memory that doesn't belong to you.

    if((some_var = malloc(some_type * some_size)) == NULL){ perror("oops"); return 1; }
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories