malloc using 4 bytes for char
NickName:user3261224 Ask DateTime:2015-02-26T11:59:54

malloc using 4 bytes for char

I am writing a code to examine how memory is managed between stack and heap. for a course work.

#include<stdio.h>
#include<stdlib.h>

#define NUM_OF_CHARS 100


// function prototype
void f(void);


int main()
{
    f();
    return 0;
}

void f(void)
{

    char *ptr1;
    ptr1 = (char *) malloc(NUM_OF_CHARS * sizeof(int));
    printf("Address array 1: %016lx\n", (long)ptr1);


    char *ptr2;
    ptr2 = (char *) malloc(NUM_OF_CHARS * sizeof(int));
    printf("Address array 2: %016lx\n", (long)ptr2);

}

when I run this code I get the following:

Address array 1: 000000000209e010
Address array 2: 000000000209e1b0

my expectation was to see a difference in the address of 100 bytes, but the difference is 416 bytes, when I changed the NUM_OF_CHARS to any other value (200,300,...) the result was always (NUM_OF_CHARS*4 + 16), so it seams like malloc is allocating 4 bytes for each char rather one byte plus 16 bytes of some overhead.

can anyone explain what is happening here?

Copyright Notice:Content Author:「user3261224」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/28734226/malloc-using-4-bytes-for-char

More about “malloc using 4 bytes for char” related questions

malloc using 4 bytes for char

I am writing a code to examine how memory is managed between stack and heap. for a course work. #include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; #define NUM_OF_CHARS 100 // function prototype v...

Show Detail

Frist few bytes of a char* are corrupted after creating it with malloc()

Whenever I create a char* with malloc(), the first few bytes get random data, which is different every time I compile the code. In this case, I wanted to concatenate 2 char* to create 1 char*. char *

Show Detail

Using malloc to allocated 0 bytes

So allocating zero bytes is ill-defined so I want to treat 0 bytes as a fail. Would this code do the trick #include &lt;stdio.h&gt; #incude "xmalloc.h" void *malloc_or exit(size_t nbytes, const ch...

Show Detail

How to store an int in a char * that is assigned by malloc

I was trying to store an int value (all 4 bytes) into a char *: So, I would like to store the full value i.e all the bytes (all 4) of the int variable into the char such that I use up 4 bytes out...

Show Detail

strlen of char* malloc

I have the following code char *new_str; int size_in_bytes; int length; size_in_bytes = 2*sizeof(char); new_str = (char *)malloc(size_in_bytes); length = strlen(new_str); Expecting the length to...

Show Detail

malloc on (char**)

Well, I'm trying to write a shell for linux using C. Using the functions fork() and execl(), I can execute each command, but now I'm stuck trying to read the arguments: char * command; char ** c_a...

Show Detail

C Question: why char actually occupies 4 bytes in memory?

I wrote a small program to check how many bytes char occupies in my memory and it shows char actually occupies 4 bytes in memory. I understand it's mostly because of word alignment and don't see

Show Detail

How to create a new char array in NASM assembly using malloc

Given this c code: char** names=(char**)malloc(count*sizeof(char*)); I want to convert it to NASM assembly code. Here is what I tried, but the code crashes: mov eax, dword count mov ebx, [ea...

Show Detail

malloc() weirdness - always 8 bytes allocated?

I'm experiencing some behavior I don't understand with malloc. For instance, allocating memory for a structure works just fine, i.e.: typedef struct my_struct { char buffer[4096];

Show Detail

What is the difference between int* and char* when using malloc() to allocate the same size of memory

int* pInt = malloc(512); char* pChar = malloc(512); I have a hard time to grasp the concept. Pointer type has the same size, 4 or 8 bytes, depending on the system. int has 4 bytes, and char has 1 ...

Show Detail