我写了以下代码:
[all the required initialization]
printf("longueur de mid: %d\n",mid);
printf("longueur de n-mid: %d\n",n - mid);
L = (char*) malloc((mid)*sizeof(char));
R = (char*) malloc((n - mid)*sizeof(char));
printf("longueur de L: %d\n",strlen(L));
printf("longueur de R: %d\n",strlen(R));
[data treatment and free()]
Run Code Online (Sandbox Code Playgroud)
随着printf我得到了这个结果:
longueur de mid: 2
longueur de n-mid: 2
longueur de L: 3
longueur de R: 3
Run Code Online (Sandbox Code Playgroud)
为什么输出会有所不同?
过去这个问题很可能已被回答(很多次),但我通过搜索找不到任何这样的答案.无论如何,这是一个非常小的问题.
我创建了一个小函数(粗略地)执行非标准GCC函数strdup()的功能.你传递一个字符串,它检查大小,为新字符串分配足够的内存,并返回它(或NULL).但是,无论出于何种原因,我似乎无法在调用函数中释放该指针.我在运行时遇到seg错误或"无效指针"错误.
我怀疑这个问题只是因为我试图释放的指针与最初发送给malloc的指针不同,并且可以通过向函数传递一个双指针并且可以很容易地解决这个问题.它为它分配了新的字符串,并没有返回任何内容,但令我困惑的是,我可以成功地释放"官方"strdup()函数返回的指针而没有任何问题.是什么赋予了?
这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *strdup2(char *buf);
int main()
{
char *str1, *str2;
str1 = strdup("This is the first test!");
str2 = strdup2("This is the second test!");
printf("%s\n%s\n", str1, str2);
free(str1);
puts("First pointer successfully freed.");
free(str2);
puts("Second pointer successfully freed.");
return 0;
}
char *strdup2(char *buf)
{
size_t len = strlen(buf)+1;
char *str = malloc(len);
if (str == NULL)
return NULL;
memcpy(str, buf, len);
return buf;
}
Run Code Online (Sandbox Code Playgroud)
运行时,我得到:
This is the first test!
This …Run Code Online (Sandbox Code Playgroud) 很抱歉提出这个问题,我试图在stackoverflow上找到解决方案,但没有令人满意的结果.我的代码如下,感谢您的耐心等待.
#include <inttypes.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
typedef uint32_t req_id_t;
typedef uint32_t view_id_t;
typedef uint64_t db_key_type;
typedef struct view_stamp_t {
view_id_t view_id;
req_id_t req_id;
}view_stamp;
typedef struct consensus_component_t {
view_stamp* highest_committed_vs;
}consensus_component;
uint64_t vstol(view_stamp* vs) {
uint64_t result = ((uint64_t)vs->req_id)&0xFFFFFFFFl;
uint64_t temp = (uint64_t)vs->view_id&0xFFFFFFFFl;
result += temp<<32;
return result;
}
int main() {
consensus_component* comp = (consensus_component*)malloc(sizeof(consensus_component));
memset(comp, 0, sizeof(consensus_component));
if(NULL != comp) {
comp->highest_committed_vs->view_id = 1;
comp->highest_committed_vs->req_id = 0;
db_key_type start = vstol(comp->highest_committed_vs)+1;
printf("%" PRIu64 …Run Code Online (Sandbox Code Playgroud) 我试着用我的结构存储很多东西.变量似乎存储在"do-while"中,但是当我尝试访问do-while之后存储的元素时,元素就消失了.我做错了什么?
#include <stdio.h>
#include <stdlib.h>
struct Sauto {
int part;
};
int main() {
int i=0;
struct Sauto *car;
do {
car = malloc(sizeof(struct Sauto)+(sizeof(struct Sauto)*i));
struct Sauto car[i];
printf("part%i:", i);
scanf("%i", &car[i].part);
printf("part%i is %i\n", i, car[i].part);
i++;
} while (i<3);
printf("part 0 is %i\n", car[0].part);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我在C中得到了一段代码,问的问题是它有什么问题吗?我很乐意回答,但是在malloc演员面前的星号让我感到困惑!
char f() { return *(char*)malloc(10); }
Run Code Online (Sandbox Code Playgroud) 我被教导如果你做malloc(),但你没有free(),内存将一直保持到重新启动.好吧,我当然测试了它.一个非常简单的代码:
#include <stdlib.h>
int main(void)
{
while (1) malloc(1000);
}
Run Code Online (Sandbox Code Playgroud)
我在任务管理器(Windows 8.1)中查看了它.
嗯,该程序真的很快就占用了2037.4 MB而且就这样.我知道它可能是Windows限制程序.
但这是奇怪的部分:当我关闭控制台时,内存使用百分比下降,即使我被教导它不应该!
免费呼叫是多余的,因为操作系统无论如何都要释放它?
(这里的问题是相关的,但不能完全回答我是否应该自由.)
为什么这样做?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char * abc = malloc(1) + 4; //WRONG use of malloc.
char * xyz = "abc";
strcpy(abc, xyz); //Should fail.
printf("%s\n", abc); //Prints abc
}
Run Code Online (Sandbox Code Playgroud)
我希望strcpy因没有足够的内存而失败,因为我将1传递给malloc()的参数.相反,它可以完美地编译和运行(在Linux上的GCC和Windows上的dev c ++).
这是预期的行为,还是一个幸福的巧合?
我认为这不是一个好习惯,但为什么它有效?
没有+4最后malloc(),我得到一个分段错误.这主要是我很好奇的.
我需要写共享内存,因此我有
#define FLAGS IPC_CREAT | 0644
int main() {
key = ftok("ex31.c", 'k');
shmid = shmget(key, 3, FLAGS);
shmaddr = shmat(shmid,0,0); // THOSE LINES WORK AS EXPECTED
char* userInput = malloc(5);
read(0, userInput, 3); // I want to read "b34" for example, WORKS GOOD
strcpy(shmaddr,userInput); // THROWS EXCEPTION!
}
Run Code Online (Sandbox Code Playgroud)
它会抛出异常strcat,如果我删除它,则会在下一行抛出异常strcpy.我需要写入内存" b34\0"(4个字符),然后读取它.
我正在尝试编写一个收集学生信息的程序.我正在使用一系列学生(结构)
typedef struct {
char name[50];
struct Course* course;
}Student;
Run Code Online (Sandbox Code Playgroud)
在我的主要()我这样做了
Student* stds = (Student*) malloc(app.std_cnt * sizeof(*stds) );
getStdData(stds);
Run Code Online (Sandbox Code Playgroud)
这是getStdData函数
void getStdData(struct Student *students){
int i;
char name[50];
Student std;
printf("\n");
for(i = 0; i < app.std_cnt; i++){
printf("std [%i] name : ",i+1);
scanf("%s",&name);
strcpy(std.name,name);
students[i] = std;
}
}
Run Code Online (Sandbox Code Playgroud)
当我编译我得到
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23026 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
gpa.c
gpa.c(124): error C2440: '=': cannot convert from 'Student' to 'Student'
Run Code Online (Sandbox Code Playgroud)
谁能告诉我我做错了什么?为什么抱怨学生转学?他们不是同一类型?
我收到警告:在第2,4行上取消NULL指针'ch'
我不明白为什么.有人可以帮我吗?
char *my_alloc(size_t size) {
char *ch = (char *)malloc(size);
//FIXED: If malloc fails -> exit program
if(*ch == NULL){
exit(0);
}
return ch;
}
Run Code Online (Sandbox Code Playgroud)