Thread: malloc doesn't do anything?

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    3

    Question malloc doesn't do anything?

    I just recently started programming in C.

    I'm running into problems with Malloc. It seems like malloc doesn't do anything with my string?

    I have the following code:

    Code:
      char *s;
      s = malloc( 5 * sizeof(char));
      strcpy(s, "123451234567890876432123456789678");
     or   s = "123456678";
      printf("String is: %s", s);
    Why can I put a string in that is larger dan 5 bytes?
    I've tried many different ways and everytime I reserve a few bytes I can make the string larger.

    Maybe i'm using things wrong but i would appreciate it if someone can explain to me why this works...

  2. #2
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Malloc guarantees you, in this case, 5 bytes of contiguous memory. There might be more available after your desired block, or there might be garbage which will crash your program when you try to access it. Malloc just returns a pointer to the start of a block of memory that will fit what you want it to. It might be more, but it will never be less.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why can I put a string in that is larger dan 5 bytes?
    Luck.

    Incidentally, s = "12345678" changes what s points to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    3
    Ok, so it's just plain luck now...
    Can I determine the size of the newly allocated block (after malloc)?

    e.g. sizeof(s) or strlen(s) ? Will it return the allocated space?
    When I use these functions now they always return 4 bytes even after mallocing 50

  5. #5
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    You've defined it, so you already know it. It's 5 bytes.

    Doing anything with the data that you "luckily" have access to is dangerous. Don't do it.

    If you need more memory than five bytes, allocate more than five bytes.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When I use these functions now they always return 4 bytes even after mallocing 50
    You're probably taking sizeof(s) which returns the size of the pointer s, not the size of what was allocated.

    You've defined it, so you already know it. It's 5 bytes.
    Yep. Also, note that sizeof(char) is guaranteed to be 1.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Registered User
    Join Date
    Mar 2008
    Posts
    3
    Thanks that makes it a lot less confusing...

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. the basics of malloc
    By nakedBallerina in forum C Programming
    Replies: 21
    Last Post: 05-20-2008, 02:32 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM