匡吉 发表于 2018-8-20 11:45:21

malloc内存方面的问题

贴上代码:

       
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

void main()
{
        char *buf = NULL;
        const char *str = "   i am student, you are teacher   ";
        buf = (char *)malloc(sizeof(str) * 2);
        memset(buf, 0, sizeof(buf));
        strcpy(buf, str);


        printf("buf:%s \n", buf);
        free(buf);
        buf = NULL;
}
       
编译器使用的是微软visual studio 2012版本.
问题:printf打印的时候总是触发断点,进入到malloc.c文件的_heap_alloc(size_t size)函数中.个人感觉buf指针作为堆上内存空间的指针,在strcpy函数之前buf指针并没有改动,问题可能出现在strcpy方面,求大神帮忙解答!!!!!

无符号整形 发表于 2018-8-20 11:47:39

sizeof(*str)试试?

匡吉 发表于 2018-8-20 11:50:48

无符号整形 发表于 2018-8-20 11:47
sizeof(*str)试试?

sizeof(*str)中的*str作为指针变量,只占有4个字节,乘上2后,只有8个字节,放不下字符串大小.

无符号整形 发表于 2018-8-20 11:51:41

匡吉 发表于 2018-8-20 11:50
sizeof(*str)中的*str作为指针变量,只占有4个字节,乘上2后,只有8个字节,放不下字符串大小.

试试?

匡吉 发表于 2018-8-20 11:53:06

无符号整形 发表于 2018-8-20 11:51
试试?

试过了,还是在printf函数那边失败

人造人 发表于 2018-8-20 12:21:16

匡吉 发表于 2018-8-20 11:53
试过了,还是在printf函数那边失败

试试 strlen

匡吉 发表于 2018-8-20 12:24:39

人造人 发表于 2018-8-20 12:21
试试 strlen

strlen只是求出字符串的长度,不包含'\0'隐含字符,sizeof计算的是C风格的字符串长度,包含'\0',如果使用strlen,打印出来后面会有乱码.
但是现在关键问题不是这个,我现在面临的是malloc分配的内存问题,怀疑buf指针被改动了,但是一直找不到原因.

claws0n 发表于 2018-8-20 12:27:29

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

int main()
{
      char *buf = NULL;
      const char str[] = "   I am student, you are teacher   ";
      buf = (char *)malloc(sizeof(str));
      memset(buf, 0, sizeof(buf));
      strcpy(buf, str);

      printf("buf: %s\n", buf);
      free(buf);
//      buf = NULL;      // 已经 free 了,怎么还用??
      return 0;
}
      

人造人 发表于 2018-8-20 12:30:10

匡吉 发表于 2018-8-20 12:24
strlen只是求出字符串的长度,不包含'\0'隐含字符,sizeof计算的是C风格的字符串长度,包含'\0',如果使用str ...

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

int main(void)
{
        const char *str = "   i am student, you are teacher   ";
        int len = strlen(str);
        char *buf = malloc(len + 1);
       
        buf = '\0';
        strcpy(buf, str);

        printf("buf:%s \n", buf);
       
        free(buf);
        buf = NULL;
        return 0;
}

人造人 发表于 2018-8-20 12:31:37

匡吉 发表于 2018-8-20 12:24
strlen只是求出字符串的长度,不包含'\0'隐含字符,sizeof计算的是C风格的字符串长度,包含'\0',如果使用str ...

"sizeof计算的是C风格的字符串长度"

你告诉我,是谁告诉你的?

人造人 发表于 2018-8-20 12:38:26

抱歉,第11行多余
修正后的代码

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

int main(void)
{
        const char *str = "   i am student, you are teacher   ";
        int len = strlen(str);
        char *buf = malloc(len + 1);
       
        strcpy(buf, str);
        printf("buf:%s \n", buf);
       
        free(buf);
        buf = NULL;
        return 0;
}

claws0n 发表于 2018-8-20 13:06:27

匡吉 发表于 2018-8-20 12:24
strlen只是求出字符串的长度,不包含'\0'隐含字符,sizeof计算的是C风格的字符串长度,包含'\0',如果使用str ...

    char str[] = "November";
    printf("Length of String is %d\n", strlen(str));
    printf("Size of String is %d\n", sizeof(str));
Length of String is 8
Size of String is 9

      char a[] = {"Geeks for"};
      char b[] = {'G','e','e','k','s',' ','f','o','r'};
      cout << "sizeof(a) = " << sizeof(a);
      cout << "\nstrlen(a) = "<< strlen(a);
      cout<<"\nsizeof(b) = " << sizeof(b);
      cout<<"\nstrlen(b) = " << strlen(b);
sizeof(a) = 10
strlen(a) = 9
sizeof(b) = 9
strlen(b) = 11

匡吉 发表于 2018-8-20 13:06:38

claws0n 发表于 2018-8-20 12:27
#include "stdio.h"
#include "stdlib.h"
#include "string.h"


终于找到原因了,我混肴了指针和内存空间以及数组名的区别.
老哥这段程序可以运行的原因,老哥用一个字符数组将全局区字符串拷贝到栈区保存,sizeof(str)此时计算的是数组中字符串大小.
而我的代码失败原因,是一直计算sizeof(str)这个str指针类型,所以错误.
至于//      buf = NULL;      // 已经 free 了,怎么还用?
这一句,是为了杜绝野指针.

匡吉 发表于 2018-8-20 13:08:14

人造人 发表于 2018-8-20 12:31
"sizeof计算的是C风格的字符串长度"

你告诉我,是谁告诉你的?

这个我依然坚持我的意见,你可以运行以下下面的代码:
void main()
{
        char buf[] = "123456";
        printf("%d\n", strlen(buf));
        printf("%d\n", sizeof(buf));

        system("pause");
}
两句打印出来的长度不一样,因为有隐含字符'\0'

TyCk 发表于 2018-8-20 13:12:13

指针变量的sizeof值与指针所指的对象没有任何关系,数组的sizeof值等于数组所占用的内存字节数。
const char *str = "   i am student, you are teacher   ";
      buf = (char *)malloc(sizeof(str) * 2);
这里分配空间,就有问题了吧,str是指针,sizeof(str)的值为4,哪怕乘以2也只有8个字节,而字符串显然不止8个字节。

匡吉 发表于 2018-8-20 13:13:40

TyCk 发表于 2018-8-20 13:12
指针变量的sizeof值与指针所指的对象没有任何关系,数组的sizeof值等于数组所占用的内存字节数。

这里分 ...

是的,已经发现问题了

TyCk 发表于 2018-8-20 13:14:27

匡吉 发表于 2018-8-20 13:13
是的,已经发现问题了

嗯嗯,解决就好啦,{:10_266:}

人造人 发表于 2018-8-20 13:17:58

匡吉 发表于 2018-8-20 13:08
这个我依然坚持我的意见,你可以运行以下下面的代码:
void main()
{


匡吉 发表于 2018-8-20 13:21:51

人造人 发表于 2018-8-20 13:17


我没有一开始就划分数组大小,只是为了区分strlen和sizeof针对计算字符串大小方面的区别.
当然,sizeof作为类型操作符,它的功能不仅仅可以用来计算字符串大小.
我想要表达的问题是,strlen计算的字符串长度不是C风格的字符串长度,而sizeof计算的字符串长度是C风格的字符串长度.

人造人 发表于 2018-8-20 13:24:50

匡吉 发表于 2018-8-20 13:21
我没有一开始就划分数组大小,只是为了区分strlen和sizeof针对计算字符串大小方面的区别.
当然,sizeof作 ...

什么是C风格字符串?
页: [1] 2 3
查看完整版本: malloc内存方面的问题