Thread: Initializing malloc()

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    26

    Initializing malloc()

    Why doing something like this okay

    Code:
    char *s;    
    s = (char *)malloc(100);
    s[0] = 0;
    but this crashes my program

    Code:
    char *s.;
    s[0] = '0';
    What's the difference?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The difference is that in the latter, you created a pointer s, but it doesn't point to anything, so you cannot dereference it with s[0]. In the former you caused it to point to memory starting from the address returned by malloc, so s[0] is fine (but you should check that malloc did not return a null pointer).
    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

  3. #3
    Registered User
    Join Date
    Jun 2018
    Posts
    26
    Here I'm assigning s to "Hello"

    Code:
    char *s = "Hello";
    s[0] = '0';
    But this crashes program too

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    What you should really have is
    const char *s = "hello";

    String constants are placed in read-only memory, so you get an exception if you try to change them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2018
    Posts
    26
    So when I assign memory using malloc(). they are placed in read and write memory, right??

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 10-03-2015, 04:31 AM
  2. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  3. initializing hex?
    By keithmolo in forum C++ Programming
    Replies: 4
    Last Post: 08-13-2003, 08:34 AM
  4. Initializing
    By Registered in forum C++ Programming
    Replies: 3
    Last Post: 07-19-2002, 01:57 PM
  5. initializing
    By srinurocks in forum C Programming
    Replies: 1
    Last Post: 06-18-2002, 06:11 AM

Tags for this Thread