-1

I am new to programming and I am trying to understand the difference between

A = (char * ) malloc(sizeof(char)*n);

and

A = (char * ) malloc(sizeof(char)); 

or

A = new char [n];

and

A = new char;

What is the default memory that a compiler is allocating to this pointer, when I do not specify the number of objects of particular data type.

Also when I declare

A = new char [n];
cout << A[n+1];

it does not give me a segmentation fault.

Should It not give segmentation fault because I am trying to access memory beyond what has been allocated for the Array.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Raghav
  • 41
  • 4

4 Answers4

5

Memory is not "allocated to this pointer", it's allocated and then you get a pointer to the memory.

This:

char *a = malloc(sizeof(char) * n);

is the same as

char *a = malloc(n);

since sizeof(char) is always 1. They both allocate space for n characters worth of data, and return a pointer to the location where the first character can be accessed (or NULL on failure).

Also, the casts are not needed in C, you should not have any.

Since sizeof(char) is 1, the second call is equivalent to:

char *a = malloc(1);

which means it allocates a memory block of size 1. This is of course distinct from the pointer to that memory block (the value that gets stored in the pointer variable a). The pointer is most likely larger than 1 char, but that doesn't affect the size of the block.

The argument to malloc() specifies how many chars to allocate space for.

I ignored the new usage, since that is C++ and the question is tagged C.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
2
A = (char * ) malloc(sizeof(char)*n);

This allocates space for n characters.

A = (char * ) malloc(sizeof(char)); 

This allocates memory for 1 character.

Every call to malloc allocates memory in the heap.

The other code is C++, and it's exactly the same, except that it will use stack memory if A is a local variable. Accessing A[n+1] may or may not yield a segfault. A[n+1] can reference a memory address that you are allowed to use. Segfault happens when you go out of the region of memory you can access, and the way it works is that there is a "red zone" from which it is considered you accessed invalid memory. It may be the case that A[n+1] just isn't "invalid enough" to trigger a segfault.

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
2
  • allocate space for N characters (N should be some positive integer value here)

    char *ma = (char * ) malloc(N);
    char *na = new char [N];
    
    • don't forget to release this memory ...

      delete [] na;
      free(ma);
      
  • allocate space for a single character

    char *mc = (char * ) malloc(sizeof(char)); 
    char *nc = new char;
    

Now, as the others have pointed out, you tagged this C, but half your code is C++. If you were writing C, you couldn't use new/delete, and wouldn't need to cast the result of malloc.

Oh, and the reason you don't get a segmentation fault when you read off the end of your array is that this is undefined behaviour. It certainly could cause a SEGV, but it isn't required to check, so may appear to work, at least some of the time, or fail in a completely different way.

Useless
  • 64,155
  • 6
  • 88
  • 132
1

Well, the compiler doesn't allocate memory for the data. Only the pointer which is either 4 or 8 bytes depending on your architecture.

There is no difference between the first two and the last two in terms on functionality. Most C++ libraries I've seen use malloc internally for new.

When you run the code to allocate n characters and you print out the n + 1th character, you aren't getting a segmentation fault most likely because n isn't a multiple of some number, usually 8 or 16. Here's some code on how it might do that:

void* malloc(size_t size) {
    if (size & 0x7 != size)
        size = size & 0x7 + 1;
    return _malloc(size);
}

So, if you requested, say, 5 bytes, malloc would actually allocate, with that code, 8 bytes. So, if you request the 6th byte (n + 1), you would get garbage, but it is still valid memory that your program can access.

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74