Question on malloc() and reading strings

This is a discussion on Question on malloc() and reading strings within the C Programming forums, part of the General Programming Boards category; Hi, I've just switched from java programming to c programming and am currently facing some problems (probably as I'm unfamilar ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    2

    Question on malloc() and reading strings

    Hi,

    I've just switched from java programming to c programming and am currently facing some problems (probably as I'm unfamilar with pointers and c syntax).

    Let's same my program has to read in an unknown number of strings (with 40 chars max) until it reads the string "halt". I have to use the malloc function to ensure that the correct amount of memory is allocated to the input string, and then print out the size of this new string (as proof).

    Why is it that the size of the new string is fixed at the size of the very first string processed? Guess that I'm doing something seriously wrong here.

    Code:
    int main() {
    
       char[41] currString;
       char *newString;
       scanf("%s",currString);
       while (strcmp(currString,"stop")!=0) {
    
    newString=(char *) malloc (strlen(currString)+1);
    printf("%lu\n",sizeof(newString));
    scanf("%s",currString);
    return 0; }
    [/code]

    Hope to get some advice. Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by rockysfr
    Code:
    int main() {
    
       char currString[41];
       char *newString;
       scanf("%s",currString);
       while (strcmp(currString,"stop")!=0) {
    
    newString=(char *) malloc (strlen(currString)+1);
    printf("%lu\n",sizeof(newString));
    scanf("%s",currString);
    return 0; }
    The sizeof operator returns the size of the object, not what the object contains. So sizeof(any_pointer) on 32-bit compilers is always 4, regardless of how many bytes is allocated for the pointer. There is no known method to later get the number of bytes allocated for the pointer, your program must remember that information.

    Your program has another bug -- memory allocated by malloc() must be released by free(). If you don't do that, your program will "leak" memory, meaning that the memory will be lost and cannot be recovered until the program terminated.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    And you shouldn't cast malloc. (#include <stdlib.h> to get rid of the errors.)

    Also consider using a break statement in your loop, so you don't have to repeat code.

    Why is it that the size of the new string is fixed at the size of the very first string processed?
    sizeof a pointer returns the size of the pointer in memory (usually 4).

    And put a closing brace in to end the while loop.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about malloc() and free()
    By MissEileen in forum C Programming
    Replies: 8
    Last Post: 01-24-2009, 02:46 PM
  2. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 10:33 PM
  3. Question regarding reading data from file into arrays
    By vutek0328 in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 10:20 AM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 09:44 AM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM

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