我试试看:
#include <stdio.h>
#include <malloc.h>
void foo(int* x)
{
x = (int *)malloc(sizeof(int));
*x = 5;
}
int main()
{
int a;
foo(&a);
printf("%d\n",a); //a = -858993460
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么a!= 5的值.我在vs2010,Windows 7中运行此代码.
编译器(这里我正在考虑gcc,但我想它可能是任何C编译器)是否关心变量的来源?如果指针来自malloc,为什么它会有区别?它只是某些编译器使用的优化还是在C标准中提到的?
我来自Python,所以使用malloc对我来说是新的.直观地说,下面应该有效,但有语法问题.在我的第一行中,我想将数组大小设置为最大8个整数.在第二行,我想添加这4个整数.这一行仅作为示例,在生产中我将有用户输入最多8位数字.当我去编译(clang)时,我得到size of array has non-integer type 'void *' 如果我注释掉第一行并用第二行初始化(并添加int类型),代码就可以了.所以我显然错误地设置了大小.有任何想法吗?
int main(void)
{
int mult_digits[malloc(8 * sizeof(int))];
mult_digits[] = {1,2,3,4};
int size_mult = sizeof mult_digits / sizeof *mult_digits;
printf("Size of the array is %d\n", size_mult);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 你好我用C编写了一个程序,当我在一个函数中使用malloc时,我不断得到分段错误或内存消息不足,我的代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define W 1031
#define B 256
/*didn't recognise NULL otherwise in eclipse*/
#ifndef NULL
#define NULL ((void *) 0)
#endif
/// structs ///
FILE *textlist;
struct coor
{
int line;
int col;
struct coor *next;
};
typedef struct coor coor, *coor_ptr;
struct file
{
struct file *next;
coor *c;
char* filetitle;
};
typedef struct file file, *file_ptr;
struct tree
{
char *word;
struct tree *left, *right;
file_ptr list ;
};
typedef struct tree …Run Code Online (Sandbox Code Playgroud) 我目前正在制作一个涉及为考试创建模板的计划.在我允许用户向考试添加问题的功能中,我需要确保仅使用存储其数据所需的内存.经过对各种输入函数(getc,scanf等)之间的差异的大量研究,我已经设法这样做了,我的程序似乎正在运行,但我关注一件事.这是我的函数的代码,我在相关的行上发表了评论:
int AddQuestion(){
Question* newQ = NULL;
char tempQuestion[500];
char* newQuestion;
if(exam.phead == NULL){
exam.phead = (Question*)malloc(sizeof(Question));
}
else{
newQ = (Question*)malloc(sizeof(Question));
newQ->pNext = exam.phead;
exam.phead = newQ;
}
while(getchar() != '\n');
puts("Add a new question.\n"
"Please enter the question text below:");
fgets(tempQuestion, 500, stdin);
newQuestion = (char*)malloc(strlen(tempQuestion) + 1); /*Here is where I get confused*/
strcpy(newQuestion, tempQuestion);
fputs(newQuestion, stdout);
puts("Done!");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
令我感到困惑的是,我尝试运行相同的代码,但只需稍加修改即可测试幕后发生的事情.我尝试从我malloc那里删除+ 1 ,我放在那里因为strlen只计算但不包括终止字符,我假设我想要包含终止字符.那仍然没有顺利.所以我尝试运行它,但是使用-1而不是印象,这样做会删除终止字符之前的任何内容(换行符,正确吗?).不过,它在不同的行上显示了所有内容.所以现在我有些困惑,并怀疑我对字符数组如何工作的了解.有人可以帮助澄清这里发生的事情,或者可能为我提供一个资源,可以更详细地解释这一切吗?
我使用malloc,memset和free时遇到问题,这不能按预期工作.问题在于最新的printf功能,而不是打印"test",它打印一些奇怪的字符,然后进行测试,比如"@#°test".你能解释我为什么吗?我注意到如果我在第二个malloc之后做了一个memset,一切正常.另外,即使没有memset,如果我不调用函数"function()",我真的不明白为什么一切正常
这是我的代码:
#define size 10
void function(){
...
...other code...
...
char *b=malloc(size);
read(file_ds,b,size); //file_ds stands for file descriptor, correctly opened with open function.
memset(b,0,size);
free(b);
}
void main(){
...
...other code...
...
function(); //If I don't call this, everything works fine, even without a memset
char *c=malloc(size);
strcat(c,"test");
printf("%s",c);
}
Run Code Online (Sandbox Code Playgroud) 现在,当我在块数组中打印每个元素时,每个元素都具有相同的地址.例如:
ints: 20 bytes stored at 0xbffa84fc
doubles: 80 bytes stored at 0xbffa84fc
chars: 8 bytes stored at 0xbffa84fc
Students: 1008 bytes stored at 0xbffa84fc
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <stdlib.h>
struct Album {
char* title;
}
int main(){
int i, size;
struct Album* pAlbum;
printf("Enter the number of album: ");
scanf_s("%d", &size);
pAlbum = malloc(sizeof(pAlbum) * size);
for(i=0; i<size; i++) {
printf("Enter the album title: ");
scanf_s("%p", pAlbum[i].title);
}
free(pAlbum);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想让用户输入他们想要的多个专辑的标题.错误是scanf只pAlbump[i].tittle为循环出现一次.我是不是错误地分配了内存?
我在for循环中声明了一个二维数组a(m*n).当for循环第一次运行时没有问题,但第二次它会产生错误.该计划是:
for (int j=0; j<2; j++) {
int **a = (int**)calloc(n,sizeof(int*));
for (int i=0; i<n; i++) {
a[i] = (int*)calloc(m,sizeof(int));
}
//...some operation to a[m,n] array
for (int i=0; i<n; i++)
free(a[i]);
free(a);
}
Run Code Online (Sandbox Code Playgroud)
当for循环第二次运行时,它将运行到包含的行calloc并在控制台上生成以下错误:
malloc:***对象0x94a8b14的错误:释放对象的校验和不正确 - 对象可能在被释放后被修改
如果删除包含的行,则没有区别free.
我有以下结构
struct NETWORK_ENDPOINT {
unsigned char Type;
unsigned char Protocol;
unsigned char IPv4[IPV4SIZE + 1];
unsigned int PortNumber;
unsigned char SocketIndex;
unsigned char RESERVED;
unsigned char *InboundData;
unsigned int InboundDataSize;
unsigned char *OutboundData;
unsigned int OutboundDataSize;
};
Run Code Online (Sandbox Code Playgroud)
在我分配的代码中:
struct NETWORK_ENDPOINT *Endpoint = malloc(sizeof(struct NETWORK_ENDPOINT));
Run Code Online (Sandbox Code Playgroud)
然后在代码中我将分配OutboundData.
Endpoint->OutboundData = malloc(20); // malloc() size may vary,
// but in the problem situation it is 20
Run Code Online (Sandbox Code Playgroud)
然后我做:
memcpy(Endpoint->OutboundData, Data, 20);
Run Code Online (Sandbox Code Playgroud)
然后问题:从调试器我可以看到Endpoint给定地址@ 0x1fd6,并OutboundData给出地址@ 0x1fca,所以只有12之间.不应该是至少20?
该memcpy()函数然后将在填写OutboundData(可以在内存中的数据被正确地放置看到),但一旦在它通过12个字节时,它将开始覆盖该结构的开始Endpoint,破坏 …