如何访问C中分配的内存?

发布于 2024-12-15 08:21:51 字数 209 浏览 1 评论 0原文

使用malloc()初始化了5000字节的内存后,我该如何引用这块内存空间中的字节呢?例如,如果我需要指向内存中数据的起始位置,我该怎么做?

编辑:我用什么来指向它重要吗?我的意思是我看到人们使用 bytes/int/char?相关吗?

我得到的错误: 在此处输入图像描述

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

骄傲 2024-12-22 08:21:52

您可以使用下标 array[n] 运算符来访问您有兴趣读/写的索引,如下所示:

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;

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;
往昔成烟 2024-12-22 08:21:52
char * buffer = malloc(5000);

buffer[idx] = whatever;

char * p = buffer + idx;
*p = whatever;
char * buffer = malloc(5000);

buffer[idx] = whatever;

char * p = buffer + idx;
*p = whatever;
触ぅ动初心 2024-12-22 08:21:52

Malloc 不会初始化它分配的位。请使用 calloc()

int *p = malloc (5000); // p points to the start of the dynamically allocated area.

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.
下壹個目標 2024-12-22 08:21:52

正如其他人所提到的,您可以执行以下操作:

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)

但是,需要注意以下几点:

  1. malloc 不会初始化内存,但 calloc 会(如前所述)作者:Prasoon Saurav)
  2. 您应该始终检查内存分配是否失败(请参阅下面的示例)
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

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
別甾虛僞 2024-12-22 08:21:52

malloc() 返回指向已分配内存的指针:

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

byte val = mem[1000]; /* gets the 1000th byte */

malloc() returns a pointer to the allocated memory:

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

byte val = mem[1000]; /* gets the 1000th byte */
ι不睡觉的鱼゛ 2024-12-22 08:21:52

使用malloc()初始化5000字节内存后,我会如何
引用该内存空间中的字节?例如,如果我需要
指向内存中数据的起始位置,我将如何去
关于那个?

我用什么来指向它重要吗?我的意思是我正在见人
使用字节/整数/字符?相关吗?

正如您所看到的 malloc 分配一个以字节为单位的内存块,您可以分配一个指向该块的指针,并且根据指针类型编译器知道如何引用单个元素:

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

现在如果您引用第二个元素短值,即 *(pshort + 1)pshort[1] 编译器知道它需要添加 2 个字节 (sizeof(short) )以便获取下一个元素。

float* pfloat = (float*)memblob;

现在,如果您引用第二个浮点值,即 *(pfloat + 1)pfloat[1] 编译器知道它需要添加 4 个字节 (sizeof( float)) 以便获取下一个元素。

与自己定义的数据类型相同:

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

mystruct_t* pstruct = (mystruct_t*)memblob; 

pstruct + 1 在偏移量 sizeof(mystruct_t) 处访问 struct

因此这实际上取决于您想要的方式使用分配的内存

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文