0

Is a non-void pointer in C only cares about the memory space from its address to the address that the memory space is suitable for the type or ...?

Example:

typedef struct {...} A;

// the allocated memory space is much larger than sizeof(A)
A* temp = (A*) malloc(sizeof(A) + 256 * 256); 

char* charPointer = (char*) temp;

charPointer += sizeof(A);

temp = (A*) charPointer;

In the last line, is temp still point to the new "A variable"? (seems an array of A allocated)

Update:

Does the cast in temp declaration & initialisation turns the memory space into an array of A, or memory space has no "type", the temp takes first (size: sizeof(A) ) memory space to store A variable, and the rest of memory space did nothing?

BigPa
  • 71
  • 2
  • 4
  • 1
    ``malloc`` allocates memory. That memory has no type at all, it's just a collection of bytes. In this case, it allocates sizeof(A) + 65k bytes. How that memory is used or treated is entirely up to the programmer. – Chuck Walbourn Apr 13 '16 at 04:04
  • [you don't need to cast the result of malloc in C](http://stackoverflow.com/q/605845/995714) – phuclv Apr 13 '16 at 05:46

3 Answers3

0

Yes, it points to a "A struct". Allocated memory is only some of bytes, user can access that with any pointer type.

In these usages the user should be aware of dynamic memory allocation and pointers' concept in c, to avoid the segmentation fault problem.

G. Emadi
  • 230
  • 1
  • 8
  • Does that mean memory space has no "type", depends on the pointer's type? Like a integer takes 32bit, a `int*` variable which points to a memory space takes first 32bit to store a `int`. (The rest of memory space did nothing) – BigPa Apr 13 '16 at 05:55
  • Yes it does. A variable pointer from any type could point to any allocated memory. – G. Emadi Apr 13 '16 at 07:16
  • Yes it does. A variable pointer from any type could point to any allocated memory. For instance, if you have memory bytes from 1000 till 1012, you can access that via int * or any pointr type. The trick is if you define int32_t *x=1000; then x+1 points to 1004, it goes forward as size of int32_t, 4 bytes. Another important note is you shuold consider the range of memory to avoiding from accessing to others' memory, that is not yours. – G. Emadi Apr 13 '16 at 07:23
0

First of all a void* is implicitly convertible to any other pointer type (hint: the cast to value returned by malloc is superfluous).

Then memory means nothing, is how you interpret its contents that gives it a meaning.

So you are basically allocating sizeof(A) + SOME_LENGTH bytes of memory, then you tell the compiler that you want to treat a specific address starting from the allocated memory as a A*.

Nothing prevents you from doing it, and it will work as long as the memory reserved starting from the address is >= sizeof(A).

The only problem is how you release the memory. The derived address charPointer + sizeof(A) is not an address that is marked as something returned by malloc from the operating system. This means that the following code yields undefined behavior:

void* temp = malloc(sizeof(A) + sizeof(A));
A* ptr = temp + sizeof(A);
free(ptr);
Jack
  • 131,802
  • 30
  • 241
  • 343
0

In C, malloc returns the address of the first byte of a new memory allocation.

You have to cast it, (or at least put it in a pointer type).

Depending of your pointer type, if you increment this address, it will jump the correct amount of byte.

For exemple :

main.c

int* myPointer = NULL;

mypointer = (int*) malloc(sizeof(int) * 10);
//mypointer is the address of the first int

mypointer++;
//mypointer is now the address of the second int.

He knows how many byte he have to jump after the pointer incrementation, because he knows the type of your pointer (int*).

An int is 4Byte, so in the memory when you increment an int* it goes 4 address further.

So yes it point to the A struct, but if you cast the wrong type, you will have segmentation fault because it will not increment by 4 (in this exemple).

Hope it helped.

neolectron
  • 19
  • 4
  • Does the cast makes the (plain) memory space into an array of A? Or the `temp` treat a piece of memory space (from the address of `temp` to the address that increment an `A`) like the memory space that a integer variable in function (on stack)? – BigPa Apr 13 '16 at 05:47
  • if your pointer is 'pointing' a struct, it's still ok to do `pointer++`, so it implicity do a sizeof on your data and jump to the right amount of bytes. – neolectron Apr 15 '16 at 18:22