2

I'm having trouble creating a dynamic char array. This is what I have so far.

char * arr;
arr = (char*)malloc (2 * sizeof (char));

It's not allocating space for only 2 characters, it's letting me enter up to arr[8] and then giving me strange errors after 8.

I also tried making a 2 dimensional char array. The first dimension allocates correctly, but then the second dimension has more space than I allow it to have and gets an error at around 12 characters or so. Any help would be greatly appreciated. I would prefer to make a 1 dimensional dynamic array if possible.

2
  • 1
    C sorry I changed the title Commented Feb 15, 2014 at 2:46
  • You only allocated memory for 2 chars, you are entering more than 2 chars, this will work until you hit overwrite some important piece of memory, and result in a segmentation fault, when you say the 2nd dimension has more space than you allowd it, In C you the programmer must manage your memory, the program wont stop writing to an array since you said it only has 2 chars, it will continue until a segmentation fault occurs(hopefully) or, it might never give you a warning and you could overwrite some other variable.. Commented Feb 15, 2014 at 3:11

2 Answers 2

5

This line arr = (char*)malloc (2 * sizeof (char)); will allocate memory for 2 bytes only. But you are overwriting the memory by accessing the more 8 or more than 8 byes. If you access more than two byes means, it will give some unpredictable issue. In case you want more memory please follow the below code.

#define USER_SIZE 10
arr = (char*)malloc ( USER_SIZE * sizeof (char));

Assign the value in USER_SIZE macro and then allocate the memory as much as you want.

Example for 2D pointer ( 5 X 10 )

#define ROW 5
#define COLUMN 10
main()
{
  unsigned char **p = NULL, colum = 0;
  p = malloc ( ROW * sizeof ( unsigned char *) );
  for (;colum< ROW; ++colum )
  {
    p[colum] = malloc (COLUMN * sizeof (unsigned char  ));
  }
}
Sign up to request clarification or add additional context in comments.

Comments

2

What you are doing is called buffer overflow by writing beyond the bounds of memory allocated by malloc call. The compiler doesn't do bounds checking (it assumes you know what you are doing, and you only pay for what you use) and allow you to compile and run. However, it will lead to undefined behaviour and your program may crash. You shouldn't rely on such behaviour.

You, the programmer, has to make sure that you don't do illegal memory access. You should not cast the result of malloc. Also, malloc can fail to allocate memory in which case it returns NULL, the null pointer, which you should take care of. You can combine the two statements into one.

int length = 8; // you can also use a macro
char *arr = malloc(length * sizeof *arr);
if(arr) {
    // malloc call successful
    // do stuff with arr
} 

Comments

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.