0

I'm trying to understand how malloc and characters arrays(c style) work. Consider the following code,

// Example program
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

int main()
{
  //section1: Init  
  char line0[10] = {'a','b','c','d','e','f','g','h','i','j'};
  char line1[10] = {'z','y','x','w','v','u','t','s','r','q'};

  //section2: Allocate Character array
  char* charBuffer = (char*) malloc(sizeof(char)*10);
  cout<<sizeof(charBuffer)<<endl;

  //Section3: add characters to the array
  for (int i=0;i<10;i++)
  {
   *&charBuffer[i] = line0[i];
  }
  //Section4:-add character to array using pointers
  for (int i=0;i<15;i++)
  {
   charBuffer[i] = line1[i%10];
  }
  //section5:-address of characters in the array
  for (int i=0;i<15;i++)
  {
   cout<<"Address of Character "<<i<<" is: "<<&charBuffer[i]<<"\n";
  }
  char *p1;
  p1 = &charBuffer[1];
  cout<<*p1<<endl;
  cout<<charBuffer<<endl;
  free(charBuffer);
  return 0;
}

output:-

8
Address of Character 0 is: zyxwvutsrqzyxwv
Address of Character 1 is: yxwvutsrqzyxwv
Address of Character 2 is: xwvutsrqzyxwv
Address of Character 3 is: wvutsrqzyxwv
Address of Character 4 is: vutsrqzyxwv
Address of Character 5 is: utsrqzyxwv
Address of Character 6 is: tsrqzyxwv
Address of Character 7 is: srqzyxwv
Address of Character 8 is: rqzyxwv
Address of Character 9 is: qzyxwv
Address of Character 10 is: zyxwv
Address of Character 11 is: yxwv
Address of Character 12 is: xwv
Address of Character 13 is: wv
Address of Character 14 is: v
y
zyxwvutsrqzyxwv

I want to understand the following,

  1. Why is the size of the charBuffer 8(see first line of output) although I have allocated a size of 10?
  2. Why I am able to add 15 characters to charBuffer although I have allocated memory for only 10 character using malloc? (see section 4 of the code)
  3. Why are the characters after the reference index being printed instead of the address of the corresponding characters in the output for section 5?
  4. How do I find the address of individual characters?
  5. It is possible to know the size of the array as the elements of the character array are getting filled? For example display the the sizeof(charbuffer) in the loop in section 3, we should get 1,2,3..,10?
2
  • Gees... one question per question, please!! Commented May 25, 2015 at 23:39
  • @LightnessRacesinOrbit Sorry about that. But is it ok to post 5 back-to-back question when the context is the same? Commented May 25, 2015 at 23:41

1 Answer 1

4

Why is the size of the charBuffer 8(see first line of output) although I have allocated a size of 10?

It's not. You printed out the size of a pointer to that buffer.

Why I am able to add 15 characters to charBuffer although I have allocated memory for only 10 character using malloc?

You're not. Conversely, though, the computer is allowed not to go out of its way informing you of your mistake. You're violating memory rules.

Why are the characters after the reference index being printed instead of the address of the corresponding characters in the output for section 5?

Because inserting a char* to a stream triggers formatted insertion, whereby the stream assumes you're streaming a C-string. Which, well, you are.

How do I find the address of individual characters?

You could write static_cast<void*>(&charBuffer[i]) to avoid this special-case handling and get the address printed out instead.

It is possible to know the size of the array as the elements of the character array are getting filled? For example display the the sizeof(charbuffer) in the loop in section 3, we should get 1,2,3..,10?

The size of the array never changes, only the number of elements to which you have written a new value. You can track that yourself using a counter variable.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for the answers! Just one more clarification. For question 1, Is there any way to the find the size of the block of memory that malloc allocates in the heap?
"Which, well, you are." - he isn't because there is no C-string in his code , just a non-terminated char array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.