释放c循环中的子字符串 [英] freeing substring in c - loop

查看:108
本文介绍了释放c循环中的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取结构'structs'的每个成员的子字符串,然后将该子字符串分配给temp_struct的新成员. 我遇到的问题是如何在每次迭代中释放子字符串,出于某种原因代码会运行,但是valgrind抛出了Invalid read of size 1,我以为我正在读取内存块.

如何释放子字符串?

谢谢

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct st_ex {
    char product[16];
    float price;
};
struct st_temp {
    char *prod;
};

char *temp = NULL;

// from stackoverflow
char* substr( const char* source, size_t start, size_t end )
{
    char* dest = malloc( end - start + 1) ;
    memcpy( dest, &source[start], end - start ) ;
    dest[end - start] = 0 ;
    return dest ;
}

int main()
{
    struct st_ex structs[] = {{"mp3 player", 2.0f}, {"plasma tv", 20.0f},
                              {"notebook", 10.0f},  {"smartphone", 49.9f},
                              {"dvd player", 10.0f}, {"matches", 0.2f }};
    struct st_temp **temp_struct;

    size_t j, i;
    temp_struct = malloc(sizeof *temp_struct * 6);
    for (j = 0; j < 6; j++)
        temp_struct[j] = malloc(sizeof *temp_struct[j]);

    size_t structs_len = sizeof(structs) / sizeof(struct st_ex);

    for(i=0; i<structs_len; i++){
        temp = substr(structs[i].product, 0, 4);
        temp_struct[i]->prod = temp;
        free(temp);
        temp = NULL;
    }
    for(i=0; i<6; i++ )
        printf("%s\n",temp_struct[i]->prod);

    for(i=0; i<6; i++ )
        free(temp_struct[i]);

    free(temp_struct);
    return 0;
}

解决方案

1)您正在释放子字符串

    temp = substr(structs[i].product, 0, 4); 
    temp_struct[i]->prod = temp; 
    free(temp); 

上面第三行释放了您在substr中分配的内存.

2)因为您要释放此处的内存,所以引入了一个错误.
释放它之后访问malloc的内存是无效的,因此尝试打印temp_struct[i]->prod是无效的.

解决方案?
不要free(temp),而是在释放temp_struct[i]的循环中,您首先需要释放temp_struct[i]->prod,像这样

for(i=0; i<6; i++ )     
{
    free(temp_struct[i]->prod);
    free(temp_struct[i]);    
}

I'm trying to get a sub-string for each member of the struct 'structs' and then assign that sub-string to a new member of the temp_struct. The problem I'm having is how to free the sub-string on each iteration, for some reason the code runs, however valgrind throws an Invalid read of size 1, which I assume I'm reading off the block of memory.

How could I free the sub-string?

Thanks

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct st_ex {
    char product[16];
    float price;
};
struct st_temp {
    char *prod;
};

char *temp = NULL;

// from stackoverflow
char* substr( const char* source, size_t start, size_t end )
{
    char* dest = malloc( end - start + 1) ;
    memcpy( dest, &source[start], end - start ) ;
    dest[end - start] = 0 ;
    return dest ;
}

int main()
{
    struct st_ex structs[] = {{"mp3 player", 2.0f}, {"plasma tv", 20.0f},
                              {"notebook", 10.0f},  {"smartphone", 49.9f},
                              {"dvd player", 10.0f}, {"matches", 0.2f }};
    struct st_temp **temp_struct;

    size_t j, i;
    temp_struct = malloc(sizeof *temp_struct * 6);
    for (j = 0; j < 6; j++)
        temp_struct[j] = malloc(sizeof *temp_struct[j]);

    size_t structs_len = sizeof(structs) / sizeof(struct st_ex);

    for(i=0; i<structs_len; i++){
        temp = substr(structs[i].product, 0, 4);
        temp_struct[i]->prod = temp;
        free(temp);
        temp = NULL;
    }
    for(i=0; i<6; i++ )
        printf("%s\n",temp_struct[i]->prod);

    for(i=0; i<6; i++ )
        free(temp_struct[i]);

    free(temp_struct);
    return 0;
}

解决方案

1) You are freeing the substring

    temp = substr(structs[i].product, 0, 4); 
    temp_struct[i]->prod = temp; 
    free(temp); 

The third line above frees the memory you malloc'd in substr.

2) because you're freeing the memory here, you've introduced a bug.
It's invalid to access the malloc'd memory after you free it, therefore it's invalid to try to print temp_struct[i]->prod.

The solution?
Don't free(temp), instead in your loop to free temp_struct[i], you first need to free temp_struct[i]->prod, like this

for(i=0; i<6; i++ )     
{
    free(temp_struct[i]->prod);
    free(temp_struct[i]);    
}

这篇关于释放c循环中的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆