Thread: Dynamically allocated array

  1. #16
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I had a few questions regarding the way the program was posted by the op

    ptr = (char*)malloc(2);

    Now this allocates 2 bytes in memory but ptr can hold the address of only the first byte as its a char pointer.
    ptr holds the address of one byte, but you still have room to add 2 bytes.
    Code:
    ptr = (char*)malloc(2); // Note that the cast to char* is not needed
    ptr[0] = 'a';
    ptr[1] = 'b';
    ptr[2] = 'c'; // BAD, you only have room for 2 chars -- not 3! 
    
    or you can do...
    
    ptr = (char*)malloc(2);
    *ptr = 'a';
    ptr++;
    *ptr = 'b';
    bit∙hub [bit-huhb] n. A source and destination for information.

  2. #17
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by bithub View Post
    ptr holds the address of one byte, but you still have room to add 2 bytes.
    Code:
    ptr = (char*)malloc(2); // Note that the cast to char* is not needed
    ptr[0] = 'a';
    ptr[1] = 'b';
    ptr[2] = 'c'; // BAD, you only have room for 2 chars -- not 3! 
    
    or you can do...
    
    ptr = (char*)malloc(2);
    *ptr = 'a';
    ptr++;
    *ptr = 'b';
    Thanks it makes it clear now. But even if the assignment

    ptr[2] = 'c'; // BAD, you only have room for 2 chars -- not 3!

    were to be done it would not generate error and may be the content at that location would get overwritten. Also why is this peculiar pattern happening that when the memory is allocated and i want to see the number of 1's and 0's at that location there seems to be a repeating pattern as pointed by the op.

    But when i tried using the code that i modified it was not the same.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The memory you get back from malloc() is completely undefined, never mind that the memory OUTSIDE this is not to be touched - it may not give you an error to read [or even write] it, but it doesn't belong to your program, so you shouldn't access it.

    If you are using Microsoft compilers, you may find that the memory allcoated is filled with different patterns for the actual memory you allocated (1 byte) and the memory before/after - which may either be padding [because the system requires for example 8 byte alignment of any memory allocated by malloc] or meta-data used by malloc itself to know where the next allocation is or some such - or free memory that hasn't been used ever.

    Even if the compiler is not filling the memory in any way, you may find that you get some "random" content when you allocate memory, because the memory contains whatever it was used to hold before the call to malloc (what it held when free was called).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How To Declare and Dynamically Size a Global 2D Array?
    By groberts1980 in forum C Programming
    Replies: 26
    Last Post: 11-15-2006, 09:07 AM
  2. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  3. Destructors in dynamically allocated arrays
    By frenchfry164 in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2003, 11:26 PM
  4. delete dynamically allocated char array
    By xddxogm3 in forum C++ Programming
    Replies: 7
    Last Post: 11-23-2003, 04:57 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM