在“C”的问题动态内存分配 [英] Dynamic memory allocation in 'c' Issues

查看:103
本文介绍了在“C”的问题动态内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写使用的东西的malloc一个code,然后面临的一个问题,所以我写了一个测试code这实际上总结了整个混乱,低于::

I was writing a code using malloc for something and then faced a issue so i wrote a test code which actually sums up the whole confusion which is below::

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

int main()     
{
     int *p = NULL;
     void *t = NULL;
     unsigned short *d = NULL;

     t = malloc(2);
     if(t == NULL) perror("\n ERROR:");
     printf("\nSHORT:%d\n",sizeof(short));
     d =t;
     (*d) = 65536;
     p = t; 
     *p = 65536;
     printf("\nP:%p: D:%p:\n",p,d);
     printf("\nVAL_P:%d ## VAL_D:%d\n",(*p),(*d));
     return 0;
  }
   Output:: abhi@ubuntu:~/Desktop/ad/A1/CC$ ./test

            SHORT:2
            P:0x9512008: D:0x9512008:
            VAL_P:65536 ## VAL_D:0

我使用malloc分配的 2字节的内存。的malloc返回的一个void *指针存储在一个void *指针'T'

I am allocating 2 bytes of memory using malloc. Malloc which returns a void * pointer is stored in a void* pointer 't'.

这2指针声明再经过对 - 整数型和D - 短型。然后我分配吨至他们两个*的(P = T和D = T)的*的这意味着这两个D&放大器;带有p指向同一位置纪念品堆。

Then after that 2 pointers are declared p - integer type and d - of short type. then i assigned t to both of them*(p =t and d=t)* that means both d & p are pointing to same mem location on heap.

在试图65536(2 ^ 16)保存到(* D)我得到警告说,大的int值被截断,这是符合市场预期。
现在我再救65536(2 ^ 16)(* P),这并没有引起任何警告。

on trying to save 65536(2^16) to (*d) i get warning that large int value is truncated which is as expected. Now i again saved 65536(2^16) to (*p) which did not caused any warning.

*在打印两者(* p)和( D)我得到不同的值(但每个纠正有自己定义的指针类型)。

*On printing both (*p) and (d) i got different values (though each correct for there own defined pointer type).

我的问题是:


  1. 虽然我分配使用malloc堆纪念品的2个字节(即16位)我怎么能保存在这两个字节65536(通过使用( P),它是整数类型的指针) ??
    我有一种感觉,这个原因是自动型converion无效
    的为int *指针(以P = T),那么,这是为P分配牛逼导致访问内存区域之外的东西通过的malloc分配。 ??

  1. Though i have allocated 2 bytes(i.e 16 bits) of heap mem using malloc how am i able to save 65536 in those two bytes(by using (p) which is a pointer of integer type).?? i have a feeling that the cause of this is automatic type converion of void to int* pointer (in p =t) so is it that assigning t to p leads to access to memory regions outside of what is allocated through malloc . ??.

尽管这一切正在发生的事情是地狱通过(* p)和(* D)derefrencing相同的内存区域如何打印两种不同的答案(尽管这也可以解释,如果我在想什么有问题的原因1)。

Even though all this is happening how the hell derefrencing the same memory region through (*p) and (*d) prints two different answers( though this can also be explained if what i am thinking the cause in question 1).

有人可以把这个有一些启发,这将是真正的AP preciated..and也如果有一个人能解释这背后的原因..

非常感谢

推荐答案

首先回答你的第二个问题:

Answering your second question first:

的解释是,在 INT 通常为4个字节,并且最显著字节可以存储在第一的两个位置。 A ,里面只有2个字节,也存储于前两个位置的数据。很明显,那么,存储 65536 INT ,但在同一个内存位置指向,将导致数据被通过为 INT 两个字节相对于,用两个最显著字节 INT 对应的存储的

The explanation is the fact that an int is generally 4 bytes, and the most significant bytes may be stored in the first two positions. A short, which is only 2 bytes, also stores its data in the first two positions. Clearly, then, storing 65536 in an int and a short, but pointing at the same memory location, will cause the data to be stored offset by two bytes for the int in relation to the short, with the two least significant bytes of the int corresponding to the storage for the short.

因此​​,当编译器打印 * D ,这间$ P $点这是一个和外观在对应于存储领域的,这是不是在哪里编译器previously存储在 65536 * p 写。需要注意的是写 * P = 65536; 已改写了previous * D = 65536; ,填充两个最显著个字节 0

Therefore, when the compiler prints *d, it interprets this as a short and looks at the area corresponding to storage for a short, which is not where the compiler previously stored the 65536 when *p was written. Note that writing *p = 65536; overwrote the previous *d = 65536;, populating the two least significant bytes with 0.

关于第一个问题:该编译器的的存储 65536 * P 在2个字节。它只是去你分配的内存边界之外 - 这很可能在某个时候导致错误

Regarding the first question: The compiler does not store the 65536 for *p within 2 bytes. It simply goes outside the bounds of the memory you've allocated - which is likely to cause a bug at some point.

这篇关于在“C”的问题动态内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆