我有一个非常奇怪的问题,试图在分配的内存上调用free导致我的程序崩溃.
这是相关的代码:
int i, count;
char *specifier;
char aisle[1];
count = 0;
/*Find name of the new item and assign to the name field of new_node...*/
for (i = 0; input[i] != ','; i++){
count++;
}
specifier = (char*)malloc((count+1)*sizeof(char));
if (specifier == NULL){
printf("Out of memory. Shutting down.\n");
exit(EXIT_FAILURE);
}
for (i = 0; input[i] != ','; i++){
specifier[i] = input[i];
}
specifier[count+1] = '\0';
new_node->name = specifier;
printf("%s\n", new_node->name);
free(specifier); /*PROGRAM CRASHES HERE*/
printf("boom\n");
specifier = NULL;
/*Function continues …Run Code Online (Sandbox Code Playgroud) *注意:这不是一个重复的问题,因为你提到的答案没有回答我的问题.我知道malloc()和calloc()应该做什么,但想知道为什么在将它与虚拟机一起使用时似乎没有什么区别.
我知道应该是什么区别 - malloc()只是为你分配内存,而calloc()用0来初始化它.
问题是,在我的代码中它没有显示,并且malloc()在从我的虚拟机Ubuntu运行时似乎没有任何区别.我运行了几次,malloc就像calloc一样.
注意 - 我刚刚用我的实际硬盘驱动器检查过,它似乎工作正常,我得到了不同的结果.
代码:
#include <stdio.h>
#include <stdlib.h>
int main(){
int i,n;
float *ptr1, *ptr2;
printf("enter a total number of float items: ");
scanf("%d", &n);
ptr1 = malloc(n*sizeof(float));
ptr2 = calloc(n, sizeof(float));
printf("malloc | calloc\n");
printf("----------------------\n");
for(i=0;i<n;i++)
printf("%-10f %10f\n", *(ptr1+i), *(ptr2+i));
printf("\n");
free(ptr1);
free(ptr2);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有两个简单的结构,在我的主要创建中:
struct Car *myCar[200]
struct Car *otherCar[200]
Run Code Online (Sandbox Code Playgroud)
当我第一次尝试初始化它们时,我试过:
for (int i = 0; i < 200; i++){
myCar[i] = malloc(sizeof(struct Car*));
otherCar[i] = malloc(sizeof(struct Car*))
}
Run Code Online (Sandbox Code Playgroud)
这不起作用.但如果我将它们拆分成单独的循环,如:
for (int i = 0; i < 200; i++){
myCar[i] = malloc(sizeof(struct Car*));
}
for (int x = 0; x < 200; x++){
otherCar[x] = malloc(sizeof(struct Car*))
}
Run Code Online (Sandbox Code Playgroud)
有用.有人可以向我解释为什么它在每个例子中都不起作用吗?
对于下面的C代码,当我只输入一个字符时,我希望最后一个printf打印"10,10".而是打印"10,8".input当malloc 10个字节时,为什么只有8个字节?
char* input;
unsigned long inputLen = 10;
input = (char*) malloc(10 * sizeof(char));
printf("Input: ");
getline(&input,&inputLen,stdin);
printf("%lu,%d",inputLen,sizeof(input));
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <stdlib.h>
typedef struct lis
{
int num;
struct lis * next;
} list;
void fun(list ** h, int nu) {
*h = malloc(sizeof(list)*nu);
list *p = *h;
int i=1;
list * nextx;
while(i<=nu) {
nextx = p + 1;
p->num = i;
p->next = nextx;
//printf("%d\n", nextx);
p += 1;
i++;
}
p->next = NULL;
}
int main(int argc, char const *argv[])
{
list * first = NULL;
fun(&first,10);
free(first);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在学习c中的列表
无论何时运行此代码,都会产生malloc错误
如果我注释掉printf("%d\n", …
此代码给出了分段错误.在GDB调试时,它给出了这个错误:
"程序收到信号SIGSEGV,分段错误.在_IO_vfscanf_internal中的0x00007ffff7a6dde5(s =,格式=,argptr = argptr @ entry = 0x7fffffffdba8,errp = errp @ entry = 0x0)at vfscanf.c:1902 1902 vfscanf.c:没有这样的文件或目录. "
void readData()
{
int **arr,m;
scanf("%d",&m);
arr = (int **)malloc(sizeof(int)*m);
for(int i=0;i<m;i++)
{
arr[i] = (int *)malloc(sizeof(int) * 2);
}
for(int i=0;i<m;i++)
{
printf("..%d ..\n",i); // if m = 20 then running only 12 times
scanf("%d %d",&arr[i][0],&arr[i][1]);
}
}
int main()
{
readData();
}
Run Code Online (Sandbox Code Playgroud)
如果m = 20那么,第二个循环只运行12次然后给出分段错误.第一个循环运行20次. 请帮帮我.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef struct _person
{
char *fname;
char *lname;
bool isavailable;
}Person;
Person *getPersonInstance(void)
{
Person *newPerson = (Person*) malloc(sizeof(Person));
if(newPerson == NULL)
return NULL;
return newPerson;
}
void initializePerson(Person *person, char *fname, char *lname, bool isavailable)
{
person->fname = (char*) malloc(strlen(fname)+1);
/*problematic behaviour if i write: person->fname = (char*) malloc (sizeof(strlen(fname)+1)); */
person->lname = (char*) malloc(strlen(lname)+1);
/*problematic behaviour if i write: person->lname = (char*) malloc (sizeof(strlen(lname)+1)); */
strcpy(person->fname,fname);
strcpy(person->lname,lname);
person->isavailable = isavailable; …Run Code Online (Sandbox Code Playgroud) 代码如下:
Board* constructBoard(int dimension)
{
//Allocate memory for board
Board *board = malloc(sizeof(Board));
if(!board)
{
return NULL;
}
//Allocate memory for matrix
board->matrix = malloc(dimension * sizeof(int*));
if(!board->matrix)
{
freeBoard(board);
return NULL;
}
//Allocate memory for each row of matrix
for(int row = 0; row < dimension; row++)
{
// Following line is line 29 from error below <---------------------------
board->matrix[row] = malloc(dimension * sizeof(int));
if(!board->matrix[row])
{
freeBoard(board);
return NULL;
}
board->dimension = row +1;
}
board->value = 0;
return board; …Run Code Online (Sandbox Code Playgroud) 我正在开发一个几乎没有发现内存泄漏的程序.这段代码实际上已有几十年历史,由一个不知名的人编写.我有返回的功能,char *我想知道我是否需要free内存.
具体来说,我有一个替换字符串中的子字符串的函数,我将指针返回到新malloc编辑的内存.我free是原始的字符串吗?
原始版本:
return(findAndReplace(str, "•",rstring));
Run Code Online (Sandbox Code Playgroud)
新版本:
char *result = findAndReplace(str, "•", rstring);
free(str);
return result;
Run Code Online (Sandbox Code Playgroud)
问题是:我可以100%确定,因为我从其他地方返回的指针已经分配了内存吗?释放内存是否安全(假设它没有在其他任何地方使用)?
编辑:str来自这里:
str = axiom_element_get_text(element, env, messageDataNode);
Run Code Online (Sandbox Code Playgroud) 在我不小心使用的代码中
list* Head = malloc(sizeof(list*));
Run Code Online (Sandbox Code Playgroud)
而不是正确的
list* Head = malloc(sizeof(list));
Run Code Online (Sandbox Code Playgroud)
创建一个新的list类型节点,但以后工作得很好.
所以我的问题是为什么它能正常工作?