当我尝试释放从"new_job-> jobs_adress"指向的字符串的已分配内存时,我出现"分段错误"错误.我已经为我的字符串分配了足够的内存(即使我分配的远远超出我的需要,我仍然有这个问题),但仍然存在这个错误.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct job_t {
pid_t pid;
time_t creation_time;
bool stop;
bool foreground;
char * jobs_adress;
} Job;
int main() {
char * jobs_adress = "string";
/* creating an object of "Job" Type */
printf("another try");
Job * new_job = (Job*)malloc(sizeof(new_job));
if(!new_job) {
return;
}
/* allocating memory for a new string, and copies the old string
into the new string*/
int length2=strlen(jobs_adress);
char * str2 = malloc(length2+1);
strcpy(str2,jobs_adress);
new_job->jobs_adress=str2;
new_job->pid = 1; …Run Code Online (Sandbox Code Playgroud) 我正在学习有关 C 编程语言中动态内存分配的知识。当我试图编写一个描述如下的程序时:
编写一个名为duplicate 的函数,它使用动态存储分配来创建一个字符串的副本。例如,调用
p = 重复(str);
将为与 str 相同长度的字符串分配空间,将 str 的内容复制到新字符串中,并返回指向它的指针。如果内存分配失败,让重复返回空指针。
这是《C程序设计语言,一种现代方法(第二版)》一书中第17章的练习2。
在我的第一次尝试中,我编写代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *duplicate(char* str){
char* news=(char*)malloc((strlen(str)+1)*sizeof(char));
if(news==NULL){
printf("Error: malloc failed\n");
return NULL;
}
strcpy(news,str);
return news;
}
int main(){
char *str,*p;
str="Hello world";
p=duplicate(str);
puts(p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运行成功。但是当我修改我的代码以调用free(str) 时,如下所示:
int main(){
char *str,*p;
str="Hello world";
p=duplicate(str);
free(str);
puts(p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它在没有任何输出的情况下失败并返回一个异常值。在我的书中,它没有提到关于这个问题的任何内容,而只给出了一些关于在没有分配任何值的指针上使用的 free() 函数的示例。我很好奇我的代码有什么问题?如果我想使用 free 函数,使用它的正确方法是什么,如何释放指向已分配值的内存的指针?
我总是被告知释放由malloc()以下分配的堆内存:
#include <stdlib.h>
#define width 5
int main(void)
{
char* ptr = malloc(sizeof(*ptr) * width);
/* some stuff with the heap object */
free(ptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是现在我已经阅读了当您在 malloc 之后不释放时真正会发生什么?我不必这样做,因为操作系统会在程序终止后自动释放占用的内存。
但是当时为什么我的老师要我这样做呢?这样做有什么好处吗?
今天我试图在 C 中使用链表实现一个堆栈,一切都很好,直到我创建了一个使用 free 的 pop 函数,一旦我调用 free 函数,我的程序就会崩溃,然后 Visual Studio 在新窗口中抛出堆损坏错误消息。除了这个之外,所有其他功能都工作正常,我只是不知道发生了什么。感谢您的所有回答。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
struct Node {
char data;
struct Node *next;
};
void push(struct Node **top, char data) {
struct Node *temp = (struct Node *)malloc(sizeof(struct Node *));
if (temp == NULL) {
printf("Stack overflow.\n");
} else {
temp->data = data;
temp->next = *top;
(*top) = temp;
}
}
void pop(struct Node **top) {
struct Node *aux;
if (*top != NULL) {
printf("Popped element: %c\n", (*top)->data); …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) 时,我都会遇到错误 - 最常见的是“尝试释放非堆对象”。
我该如何解决这个问题?蒂亚!
我使用 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) 我得到了一组源代码,其中的情况如下:
假设我有一个源文件 (source1.c),其中包含:
static float *var;
void alloc_mem ()
{
var = (float *) malloc(N * sizeof(float));
}
void some_function (float *data)
{
// do something with var and data
}
Run Code Online (Sandbox Code Playgroud)
在主函数中,我有
int main()
{
alloc_mem ();
some_function (data);
}
Run Code Online (Sandbox Code Playgroud)
现在,我无法var通过调用free(var)main来释放分配给的内存,因为它不在那里的范围内。
这种情况会导致内存泄漏吗?如果我在 source1.c 中定义一个函数为
void dealloc_mem()
{
free(var);
}
Run Code Online (Sandbox Code Playgroud)
并在 main 的末尾调用它,它会起作用吗?
该函数仅在 C 中的链表为空时才删除它。ll_length() 函数已确认有效。如果链表删除成功,则返回0。如果没有删除,则返回负1。该程序返回-1。它输入第一个 if 语句,但不输入第二个。怎么了
ll_destroy(struct linked_list *ll)
{
if (ll_length(ll) == 0){
free(ll);
if (ll == NULL){
return 0; //remove 0 if removed
}
}
return -1; // if not deleted
}
Run Code Online (Sandbox Code Playgroud) 我不明白为什么这里的 free 不起作用,而我在 printf 中仍然得到 5。我应该将 p 值指向 NULL 吗?我搜索了很多线程,但没有找到任何关于此的有用信息。
#include<stdio.h>
void main(){
int *p = malloc(sizeof(int));
*p = 5;
free(p);
printf("%d\n",*p);
}
Run Code Online (Sandbox Code Playgroud) 我有个疑问。假设我有一个使用 malloc 分配的缓冲区,如下所示:
uint16_t buffer_length = 200; // !size is dynamic!, never static
uint8_t* buffer = (uint8_t*) malloc(200*sizeof(uint8_t));
// buffer then is populated somehow from 0 to 199
Run Code Online (Sandbox Code Playgroud)
现在我有一个 std::queue ,我需要 FIFO 几个分配的缓冲区,如下所示:
std::vector<uint8_t> buffer_vector(buffer, buffer + buffer_length);
std::queue<std::vector<uint8_t>> fifo_queue;
fifo_queue.push(buffer_vector);
Run Code Online (Sandbox Code Playgroud)
考虑到我决定从 uint8_t* 转移到 std::vector 因为我可以在单个元素中存储更多缓冲区信息(长度)。现在我想从队列中取出项目:
std::vector<uint8_t> taken_item = fifo_queue.front(); // reads the FIFO item
fifo_queue.pop(); // removes from FIFO
uint8_t* taken_item_buffer_ptr = taken_item.data(); // takes the buffer ptr
uint16_t taken_item_buffer_length = taken_item.capacity(); // takes the size
Run Code Online (Sandbox Code Playgroud)
现在我可以处理从 FIFO 返回的缓冲区,问题是,我是否必须释放返回的指针?例如如下: …
free ×10
c ×9
malloc ×5
heap-memory ×3
c++ ×2
string ×2
assign ×1
char ×1
fifo ×1
if-statement ×1
linked-list ×1
memory ×1
memory-leaks ×1
queue ×1
stack ×1
static ×1