user2958652
user2958652

Reputation: 136

Using memory declared for other types

If I were to initialize memory for a variable of four bytes, would I be able to point two two-byte variables to the memory stored for it? I'm trying to further understand memory management and theoreitcs.

An example of what I'm taking about:

int main() {
    short* foo = malloc(4*(foo));    // sizeof(*foo)?
    /*in which sizeof will return 2 so I could say
      * malloc(20)
      * so could I say malloc(2*sizeof(long))?
      */
}

Or are types generally declared adjacent to each other on the heap, I.e. a block is reserved for long, and a block is reserved for short typed variables?

Edit I forgot to include a question. If I were to declare two variables of type short next to each other (an array), could I safely point a long to the first item, an and access both via bitmap? Obviously this is mainly for theoretics, as I feel that there would be better, more obvious answers to problems.

Upvotes: 3

Views: 111

Answers (4)

Michael
Michael

Reputation: 1503

Sure, you are able to access it as you want. Little info from manual to calloc() and malloc():

SYNOPSIS
       #include <stdlib.h>

       void *malloc(size_t size);
       void *calloc(size_t nmemb, size_t size);
       ...

DESCRIPTION
       The  malloc() function allocates size bytes and returns a pointer to 
       the allocated memory.  The memory is not initialized.  If size is 0, 
       then malloc() returns either NULL, or a unique pointer value
       that can later be successfully passed to free().
       ...
       The  calloc() function allocates memory for an array of nmemb elements 
       of size bytes each and returns a pointer to the allocated memory.  The 
       memory is set to zero.  If nmemb or size is 0, then calloc() returns 
       either NULL, or a unique pointer value that can later be successfully 
       passed to free().
       ...

Upvotes: 1

jocke-l
jocke-l

Reputation: 703

Yes, As stated by others, C is a weak-typed language. Which means that it has very little or no restrictions of type-conversions.

For example:

void *ptr;
unsigned int *m;
unsigned int *e;
double *d;

ptr = malloc(sizeof(double));

d = ptr;

m = ptr;
e = ptr + 6;

*d = 123.456f;

printf("mantissa: %u\nexponent: %u\ndouble: %f\n", *m, *e, *d);

/* Output:
 * mantissa: 536870912
 * exponent: 16478
 * double: 123.456001
 */

Upvotes: -1

noelicus
noelicus

Reputation: 15055

Yes. C doesn't care about types when you allocate memory - it's just a block of memory as big as you've requested. It'll let you write in it, over it, under it ... until something bad happens!

If you want neighbouring values an array is a good way to go:

int *myAllocatedArray = (int*)calloc(2, sizeof(int));

myAllocatedArray[0] = 100;
myAllocatedArray[1] = 200;

calloc will initialize every byte to 0.

Upvotes: 2

Mrinal Saurabh
Mrinal Saurabh

Reputation: 978

Yes it will work, because malloc takes integer arguments like: malloc(2*4), 4=sizeof(long), or anything like malloc(20);

Upvotes: 1

Related Questions