struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* test = malloc(sizeof(struct ListNode*));
test->val = 6;
struct ListNode* lex = malloc(sizeof(struct ListNode*));
test->next = lex;
return test;
Run Code Online (Sandbox Code Playgroud)
此时我应该收到一个填充的结构。相反,我得到了这个:
Line 14: Char 18: runtime
error: store to address
0x602000000118 with
insufficient space for an
object of type 'struct ListNode
*' (solution.c)
0x602000000118: note: pointer
points here
be be be be 00 00 00 00 00 00
00 00 02 00 00 00 ff ff ff 02
08 00 00 …Run Code Online (Sandbox Code Playgroud) 我使用以下 malloc/calloc 代码获得空内存。有时它无法为“name1”分配内存,然后 strcpy 失败。请指导。
struct testMalloc
{
char name1[90];
char name2[90];
struct testMalloc* ptr;
};
int main(int argc, char* argv[])
{
struct testMalloc* test = 0 ;
int size = 0;
size = sizeof(struct testMalloc);
printf("Size of struct is %d", size);
test = (struct testMalloc*) calloc(sizeof(struct testMalloc));
strcpy((test->name1), "hdshdssdsdfsfffffffffffffffffffffffffffffh");
return 0;
}
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <stdlib.h>
#include <time.h>
int** intptrArray;
char word[50];
Run Code Online (Sandbox Code Playgroud)
在此之后的函数中使用 malloc 分配数组和单词后,我尝试在数组和单词上使用 free()。free(intptrArray) 似乎工作得很好,但是每次我尝试 free(word)、free(*word) 或 free(&word) 时,我都会遇到错误 - 最常见的是“尝试释放非堆对象”。
我该如何解决这个问题?蒂亚!
好的,所以我在 C++ 中尝试使用指针,因为我在将近一年半的休息(学校的东西)后又开始编码了。所以如果这对你来说很幼稚,请原谅我,我只是生锈了。
无论如何,我在 VS 2019 中纠结于指针,我注意到一些事情开始困扰我。
这是我写的代码:
#include <iostream>
int main() {
int* i = new int[4];
*i = 323;
*(i + 1) = 453;
std::cout << *i << std::endl;
std::cout << *(i + 1) << std::endl;
delete i;
}
Run Code Online (Sandbox Code Playgroud)
有些东西看起来很奇怪吧?别担心,删除是故意的,并且是这个问题的重点。现在我预计它会发生一些内存事故,因为这不是我们在堆上删除数组的方式,但令我惊讶的是,它没有(我在 Debug 和 Release 中都这样做了并且有相同的观察)
现在我期待在删除时发生某种事故,因为我没有按照应该删除的方式删除它。但
删除不正确
现在在另一次运行中,我实际上正确地使用了删除运算符,它做了同样的事情:
现在,当我尝试使用 malloc 分配并使用 delete 释放它时,我得到了相同的结果。
所以我的问题是 - 为什么我的代码没有搞砸?我的意思是,如果这是它的工作方式,我可以使用 delete(指针)来擦除整个数组。
我的问题的要点是“新操作员在幕后做什么?”
malloc 运行时,会生成内存块,不设置任何值,包含垃圾值。当 calloc 运行时,会像 malloc 函数一样发生一些事件,但有一个区别。当 calloc 生成新块时,它在块中设置 0(零)。
#include <stdio.h>
#include <stdlib.h>
int main(void){
int i,j;
int* array1 = (int*)malloc(sizeof(int)*5);
int* array2 = (int*)calloc(sizeof(int),5);
for(i = 0; i < 5; i++){
printf("%p: %d\n",&array1[i],array1[i]);
}
printf("===================\n");
for(j = 0; j < 5; j++){
printf("%p: %d\n",&array2[j],array2[j]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
根据此信息,前五个值必须包含垃圾值(实际上它应该看起来像随机数据),后五个值必须为零。但是当这段代码在 Windows 中运行时,没有问题,但是当这段代码在 Linux 中运行时,这种情况不会发生。我以为是ASLR还是DEP保护,我把ASLR关掉了,用旧的Linux系统做DEP保护,结果还是一样。最后,我认为它可能取决于 C Standard,并且我在编译 Code 时更改了 C Standard,但结果并没有什么不同。我问这是什么原因。
我正在读取一个浮点数文件,然后对它们进行排序。当我对 100 万个数字使用以下排序和交换函数时,我能够成功地对数字进行排序。但是,当我尝试对 1 亿个数字进行排序时,出现分段错误。我不知道为什么,因为我正在动态分配内存。我如何能够处理超过 100 万个数字?
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
void swap(float *a, float *b, size_t n) {
size_t numbytes;
size_t sz = sizeof(float);
void *temp = NULL;
numbytes = n * sz;
if (numbytes == 0){
exit(EXIT_FAILURE);
}
temp = malloc(numbytes);
memcpy(temp, a, numbytes);
memcpy(a,b,numbytes);
memcpy(b,temp,numbytes);
free(temp);
}
void radixSort(float array[], size_t count) {
int numOfZero = 0;
float a[count];
float *b = a;
for (uint32_t radix=1; radix; radix<<=1) { //unsigned int 32 bit
uint32_t *arrayToInt …Run Code Online (Sandbox Code Playgroud) 我有以下结构:
struct scp_header {
char state_name[NAME_MAXSIZE];
enum scp_packet_type type;
size_t payload_size;
size_t script_size;
};
Run Code Online (Sandbox Code Playgroud)
我想在几个函数中使用这个结构,但我需要做的第一件事是填充字段state_name,为了做到这一点,我的函数的一部分执行以下操作:
struct scp_header *header;
if ((header = malloc(sizeof(struct scp_header)) == NULL))
return -1;
if (header->state_name == NULL)
printf("This field is NULL\n");
header->type = INIT;
header->payload_size = 0;
header->script_size = total_code_size;
Run Code Online (Sandbox Code Playgroud)
我做了上面的验证,因为当我尝试使用时,strncpy(header->state_name, "sometext", NAME_MAXSIZE)我遇到了分段错误。所以我决定检查该state_name字段是否不是 NULL 并且我真的进入了 if 语句,该语句表明该字段是NULL.
我的问题是:为什么这个字段是NULL如果我分配了整个结构,它应该malloc为那个字段分配必要的内存,不是吗?
我使用 malloc 分配指针类型使用的内存。然后我强行修改指针类型。使用 free func 释放内存块。我很想知道是否会发生内存泄漏?
我认为 memroy 是免费的指针类型。"int *b" 比 "char *a" 具有更广泛的内存块。我编译以下代码并运行它。没有期望发生。有人能告诉我发生了什么,为什么?
#include <stdlib.h>
int
main(int argc, char **argv) {
char *a = (char*)malloc(sizeof(char));
int *b = (int*)a;
free(b);
}
Run Code Online (Sandbox Code Playgroud) 我知道这是一个非常基本的问题,可能有重复,但我找不到这个特定问题的严格答案,该问题涉及标准。(我看到有人说是UB,有人说不是)
如果我分配一块内存而不向其中填充数据,
int* ptr = malloc(10 * sizeof(int));
Run Code Online (Sandbox Code Playgroud)
然后尝试读取它,那里的值将是垃圾。
但这是否被归类为未定义行为?或者它只是糟糕但至少不是UB?
所以,想象一下我有这个代码:
typedef struct Point {
float x;
float y;
} Point;
class Foo {
private:
Point * p;
public:
Foo () {
this->p = (Point *) malloc(sizeof(Point));
if (this->p == NULL) {
// throw exception_malloc_fail;
}
}
};
Run Code Online (Sandbox Code Playgroud)
一旦 malloc 在构造函数内分配内存失败,我应该抛出哪种异常?
在这种情况下,我不能简单地 returnfalse或NULL。所以throw声明应该是要走的路。
但是,我找不到要抛出的正确类型的异常。我应该抛出一个默认异常吗?或者有没有适合这种情况的?