malloc question

This is a discussion on malloc question within the C Programming forums, part of the General Programming Boards category; hi all, i'm extremely new to C so please bare with me. I'm currently interested in studying strings in C, ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    2

    malloc question

    hi all, i'm extremely new to C so please bare with me.

    I'm currently interested in studying strings in C, since there isn't one, and malloc is one of the way, I'm not understanding when to use it.

    I have two test case, which seems to say I don't need malloc at all when allocating string to array :


    1. Using STRCPY needs the pointer to have an allocated size :

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    int main()
    {
    
    	char *myArray;
    	//myArray = (char *) malloc(sizeof(myArray));
    	
            strcpy(myArray, "Hello World!");
    
    	//*** Process returned -1073741819 if no malloc***
    
    	puts(myArray);
    
    	return 0;
    }

    2. Without STRCPY, you don't need to allocate a size for a string pointer :

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    int main()
    {
    
    	char *myArray;
    	//myArray = (char *) malloc(sizeof(myArray));
    	
    	//strcpy(myArray, "Hello World!");
    
    	myArray = "Hello World!";
    
    	puts(myArray);
    
    	return 0;
    }

    why is it like that?? does that mean i can go around creating bunch of pointers as 'string datatype in C' now without allocating space?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    In your first program:

    Code:
    myArray = (char *) malloc(sizeof(myArray));
    You should be allocating sizeof(*myArray).

    But there is a fundamental difference between your two programs. strcpy() copies the entire contents of a string from one buffer to another buffer . Assigning a string literal (ie. "Hello World!") to a char *, is just that.... you're taking the address where "Hello World!" is stored in memory and making your pointer point to that. That doesn't do any copying.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Note that in the second example, myArray should be const char*, not char*, because string literals are often read-only.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    I hate to chime on with a question on a thread thats not mine but it is probably related. I am fairly new with C pointers in general. Each time you make a char pointer to something such as a character string like

    char * myString = "Hello World";

    Hello World is allocated somewhere in memory and its address returned to myString? Does this only work for strings and not say ints or structures? Why or why not.

    Thanks. Ill make a new thread if that is better.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    "Hello World" is usually put in a read-only data segment and its address is put into myString. Because it's typically read-only, it should be const char*.
    This only works for string literals.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    How come then, with my understanding of C strings at this point the code:
    Code:
    char * myString = "Hello World\n";
    printf ("%c", myString);
    does not print out H

    myString should be a pointer to the first byte in memory where the string is stored? Is this different for string literals?

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    When printing characters, you pass the character data.
    To print string literals, you pass the address to where the first character of the string is stored. This is required because you cannot pass an array by value in C.
    Again, myString should be const char*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Because &#37;c means char, not a char *. If you change it to this, you'll get what you want:

    Code:
    printf ("%c", *myString);

  9. #9
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Why didn't I see that... Sorry

    And thanks!

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    2
    Quote Originally Posted by MacGyver View Post
    In your first program:

    Code:
    myArray = (char *) malloc(sizeof(myArray));
    You should be allocating sizeof(*myArray).

    But there is a fundamental difference between your two programs. strcpy() copies the entire contents of a string from one buffer to another buffer . Assigning a string literal (ie. "Hello World!") to a char *, is just that.... you're taking the address where "Hello World!" is stored in memory and making your pointer point to that. That doesn't do any copying.
    ah I see now, i've always assumed I could accept dynamic input from user using the pointer directly without allocating any space.

    BUT...regarding the sizeof(*myArray), it doesn't seem to matter at all..infact, as demonstrated below, sizeof(1) will accept a long string (i typed around 100 characters) and it still won't have any buffer overflows..why is it like that?


    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    int main()
    {
    
    	char *myArray;
    	myArray = malloc(sizeof(1));
            //if removed, there'll be error
    	
    	puts("Okay, type your name below : \n");
    	gets(myArray);
    
    	printf("Hey-- you're %s right?!\n", myArray);
    
    	return 0;
    }

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by adrive View Post
    ah I see now, i've always assumed I could accept dynamic input from user using the pointer directly without allocating any space.
    No doubt you never gave any thought about where such space would fit in memory.

    Quote Originally Posted by adrive View Post
    BUT...regarding the sizeof(*myArray), it doesn't seem to matter at all..infact, as demonstrated below, sizeof(1) will accept a long string (i typed around 100 characters) and it still won't have any buffer overflows..why is it like that?
    It's like that because you are doing something undefined by the C standard. What is possibly happening is that you are trashing memory your program has access to. What are you overwriting? No idea. malloc() might actually be allocating more than 1 byte, and you just might be overwriting that.

    You could very well take this program and run it with another compiler or on another system, and it would crash.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Alternative to malloc
    By stellastarr in forum C Programming
    Replies: 13
    Last Post: 04-30-2007, 04:10 PM
  4. malloc, calloc question
    By chen1279 in forum C Programming
    Replies: 12
    Last Post: 09-07-2006, 05:54 PM
  5. Question about malloc()
    By cdalten in forum C Programming
    Replies: 6
    Last Post: 05-12-2006, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21