0

After using malloc() to initialize 5000 bytes of memory, how would I reference the bytes in this memory space? For example, if I need to point to a starting location of data within the memory, how would I go about that?

EDIT: Does it matter what I use to point to it? I mean I am seeing people use bytes/int/char? Is it relevant?

Error I get: enter image description here

antonpug
  • 13,724
  • 28
  • 88
  • 129
  • 2
    The problem in your screenshot is that you are using `sizeOf` instead of `sizeof`. Also, `mem` appears to be a macro, which is expanding into something unexpected and causing problems. – Mankarse Nov 13 '11 at 04:46
  • What do you mean it is a macro? I have it in the header as #define mem...that's probably an incorrect way to declare it? How would I write a declaration for mem? – antonpug Nov 13 '11 at 04:53
  • `#define mem` defines `mem` to be a [macro](http://en.wikipedia.org/wiki/C_preprocessor) that expands to nothing. It is not at all clear what you are trying to do. If mem was not already `#defined` as a macro you would be declaring it with the syntax `byte *mem`. This would be very problematic, because it would mean that calling `MemoryManager` would do nothing except leak memory. – Mankarse Nov 13 '11 at 05:00
  • Ahh I see, so I don't need to define it in the header! – antonpug Nov 13 '11 at 05:01
  • 1
    You might find these useful, [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – Blastfurnace Nov 13 '11 at 05:05

6 Answers6

1
char * buffer = malloc(5000);

buffer[idx] = whatever;

char * p = buffer + idx;
*p = whatever;
Thomas Eding
  • 35,312
  • 13
  • 75
  • 106
1

You can use the subscript array[n] operator to access the index you are interested in reading/writing, like so:

uint8_t* const bytes = (uint8_t*)malloc(5000);

bytes[0] = UINT8_MAX; // << write UINT8_MAX to the first element
uint8_t valueAtIndexZero = bytes[0]; // << read the first element (will be UINT8_MAX)
...
free(bytes), bytes = 0;
justin
  • 104,054
  • 14
  • 179
  • 226
0

Malloc doesn't initialize the bits allocated by it. Use calloc() rather.

int *p = malloc (5000); // p points to the start of the dynamically allocated area.
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
0

malloc() returns a pointer to the allocated memory:

typedef unsigned char byte;
byte * mem = malloc( 5000 );

byte val = mem[1000]; /* gets the 1000th byte */
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
TTimo
  • 1,276
  • 1
  • 13
  • 20
  • initialize(int bytes) { typedef unsigned char byte; byte *mem = malloc(bytes); }
    I get an error on *mem says Expected Identifier
    – antonpug Nov 13 '11 at 04:31
0

As has been mentioned by others, you could do something like this:

int nbytes = 23; // number of bytes of space to allocate
byte *stuff = malloc(nbytes * sizeof stuff[0]);

stuff[0] = 0; // set the first byte to 0
byte x = stuff[0]; // get the first byte

int n = 3;
stuff[n] = 0; // set the nth byte to 0
x = stuff[n]; // nth byte, or in the case of some other type, nth whatever - just make sure it's a safe value, from 0 (inclusive) to the number (nbytes here) of things you allocated (exclusive)

However, a couple of things to note:

  1. malloc will not initialise the memory, but calloc will (as mentioned by Prasoon Saurav)
  2. You should always check to see if the memory allocation failed (see below for an example)
int nbytes = 23; // or however many you want  
byte *stuff = malloc(nbytes * sizeof stuff[0]);

if (NULL == stuff) // memory allocation failed!
{
 //handle it here, e.g. by exiting the program and displaying an appropriate error message
}

stuff[0] = 0; // set the first byte to 0
byte x = stuff[0]; // get the first byte

int n = 3;
stuff[n] = 0; // set the nth byte to 0
x = stuff[n]; // nth byte, or in the case of some other type, nth whatever
Iskar Jarak
  • 5,136
  • 4
  • 38
  • 60
0

After using malloc() to initialize 5000 bytes of memory, how would I reference the bytes in this memory space? For example, if I need to point to a starting location of data within the memory, how would I go about that?

Does it matter what I use to point to it? I mean I am seeing people use bytes/int/char? Is it relevant?

as you have seen malloc allocates a block of memory counted in bytes, you can assign a pointer to that block and depending on the pointer type the compiler knows how to reference individual elements:

unsigned char *memblob = malloc( 1024 );
short* pshort = (short*)memblob; 

now if you reference the second short value i.e. *(pshort + 1) or pshort[1] the compiler knows that it needs to add 2 bytes (sizeof(short)) in order get the next element.

float* pfloat = (float*)memblob;

now if you reference the second float value i.e. *(pfloat + 1) or pfloat[1] the compiler knows that it needs to add 4 bytes (sizeof(float)) in order get the next element.

same with own defined data types:

typedef struct s
{
  short a;
  long  b;
} mystruct_t;

mystruct_t* pstruct = (mystruct_t*)memblob; 

pstruct + 1 accesses the struct at offset sizeof(mystruct_t)

so it is really up to you how you want to use the allocated memory

AndersK
  • 35,813
  • 6
  • 60
  • 86