Thread: confused about dynamic memory allocation

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    24

    confused about dynamic memory allocation

    Which of the following pieces of code, in general, can result in an invalid memoryaccess? This question has multiple answers.
    (
    A) char *str = (char*) malloc(10 * sizeof(char);
    str[10] = ’\0’;
    printf("%s", str);
    (B) char *str = (char*) malloc(10 * sizeof(char);
    str[0] = ’\0’;
    printf("%s", str);
    (C) char *str;
    str[0] = ’\0’;
    printf("%s", str);
    (D) char *str = NULL;
    str[0] = ’\0’;
    printf("%s", str);





    Can someone explain this question to me, I'm really confused?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The question is pretty clear. Which do you think result in invalid memory access, and why?

    If you don't understand the question, I suggest you read up about the functions used in the examples.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Multiple answers since more than one of them could be invalid ?
    Last edited by rcgldr; 02-23-2016 at 05:35 PM.

  4. #4
    Registered User
    Join Date
    Jan 2016
    Posts
    24
    A would be invalid memory access because we start at str[0] and end at str[9]. Therefore str [10] is not part of this array. D would also be correct because in its first statement the pointer char*str is set to null which is zero memory.

  5. #5
    Registered User
    Join Date
    Jan 2016
    Posts
    24
    So are the final answers A,D?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, but there's another one.
    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
    Registered User
    Join Date
    Jan 2016
    Posts
    24
    Is C also correct because it did not allocate any memory?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes.

    EDIT:
    Well, if you consider that malloc can return a null pointer, B is problematic too.
    Last edited by laserlight; 02-23-2016 at 06:32 PM.
    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

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you're in the process of learning about dynamic memory allocation, and you're receiving code snippets like that, then I suggest you read this: FAQ > Casting malloc - Cprogramming.com

    The gist of it is to not cast the return value of "malloc()".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Memory Allocation
    By kwzeet in forum C Programming
    Replies: 4
    Last Post: 07-14-2013, 04:35 PM
  2. Dynamic Memory Allocation
    By JamesD in forum C Programming
    Replies: 10
    Last Post: 03-12-2011, 12:08 AM
  3. confused with memory allocation
    By ssharish in forum C Programming
    Replies: 2
    Last Post: 02-26-2005, 10:17 PM
  4. dynamic memory allocation
    By inquisitive in forum C++ Programming
    Replies: 5
    Last Post: 03-13-2004, 02:07 AM

Tags for this Thread