小编Mic*_*chi的帖子

为什么指向字符串文字后无法指针free()

关于我的问题有一些相关的讨论,但没有关于我的问题的明确解释.

我认为free()无论我malloc()在哪里,无论何时何地,但我必须这样做,以防止内存泄漏.所以我有以下程序:

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

int main(void)
{
    int *ptr;

    ptr = malloc(256);

    if (ptr == NULL)    {
        printf("Out of memory\n");
        return 1;
    }

    *ptr = 10;

    printf("The value of PTR is:\t%d\n",*ptr);
    free(ptr);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我有一个指针,我动态分配了一些内存(256),然后我检查了它的NULL字母free().

直到这里一切正常:指针被动态分配一些内存然后我free().

现在我将使用一个字符指针,在我将动态分配一些内存(256)后,我将指向字符串文字的指针,让我们说MICHI:

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

int main(void)
{
    char *ptr;

    ptr = malloc(256);

    if (ptr == NULL)    {
        printf("Out of memory\n");
        return 1;
    }

    ptr = "michi";
    printf("%s",ptr);

    free(ptr);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在这里我做错了,因为如果我尝试free()它,那么它将无法工作,因为它发生了我将要释放一个非堆对象. …

c

2
推荐指数
1
解决办法
425
查看次数

从同一个类打印3个对象

我开始阅读一本书,在某些时候,我来到class我创建第一个类并在main一个对象中创建后,我决定在同一个类上再创建两个Object.该程序工作正常,但输出似乎与我的理解不同.

这是代码:

#include <iostream>

using namespace std;

class Data{
    public:
        Data( string z ){
            cout << z << endl;
        }
        void setName ( string x ){
            name = x;
        }

        string getName( void ){
            return name;
        }
    private:
        string name;
};

int main ( void ){
    Data obj1( "Hello" );
    Data obj2( "World" );
    Data obj3( "!" );

    cout << obj3.getName() << endl;
    cout << obj2.getName() << endl;
    cout << obj1.getName() << endl;
}
Run Code Online (Sandbox Code Playgroud)

这是输出:

Hello …
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
40
查看次数

函数声明不是原型

以下代码编译正常:

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

extern int errno ;

int main ( void )
{
    FILE *fp;
    int errnum;
    fp = fopen ("testFile.txt", "rb");

    if ( fp == NULL )
    {
        errnum = errno;
        fprintf( stderr, "Value of errno: %d\n", errno );
        perror( "Error printed by perror" );
        fprintf( stderr, "Error opening file: %s\n", strerror( errnum ) );
        exit( 1 );
    }

    fclose ( fp );
}
Run Code Online (Sandbox Code Playgroud)

但我不能编译它:

gcc-8 -Wall -Wextra -Werror -Wstrict-prototypes
Run Code Online (Sandbox Code Playgroud)

我得到以下内容:

 program.c:6:1: error: …
Run Code Online (Sandbox Code Playgroud)

c errno gcc8

2
推荐指数
1
解决办法
245
查看次数

检查字母字符的功能

我创建了一个函数来检查用户是否键入了一个不包括所有其他非字母字符的真实姓名.好吧,在我看来,作为C语言的初学者,它的工作正常.无论如何我只有一个小问题,用字符串名称,如果该字符串中有空格我错了名字,但如果只有一个名字(michi)一切都好.

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

        /* Here is the Function which check if the string contains only: */
        /* abcdefghijklmnopqrstuvwxyz and ABCDEFGHIJKLMNOPQRSTUVWXYZ */
    int checkName(char *s){
        int i,length;
        length = (strlen(s));

        for (i=0;i<length;i++){
            if(s[i] == '0' || s[i] <= '9'){
                return 1;
            }
        }
        return 0;
    }

    int main(){
        char name[]= "Michi";
        int check;

        if((check = checkName(name)) == 0){
            printf("\n\n\t\t\tYour name is:\t%s\n\n",name);
        }else{
            printf("\n\n\t\t\tWrong name:\t%s\n\n",name);
        }

        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是:1)我找到了一种正确的方法来检查字符串是否只包含非字母字符.2)如何扩展我的函数以跳过空格

c

1
推荐指数
1
解决办法
9141
查看次数

如何删除警告“从'int'转换为'char'可能会改变其值”警告

我虽然说,如果我cast有一个像这样的数字(unsigned char)32,就足以解决编译器的警告,但这并不是我的计划。

在这里,我有程序的以下部分,实际上解释了这个问题:

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

int main(void){
    char *ptr = malloc(6);
    const char *name = "MICHI";
    unsigned int i = 0;

    if(ptr){
        strcpy(ptr, name);
        ptr[strlen(ptr)] = '\0';
    }else{
        return 1;
    }

    while(ptr[i] != '\0'){
        if((ptr[i] >= 'A') && (ptr[i] <= 'Z')){
            ptr[i] += (unsigned char)32;
        }
        i++;
    }

    printf("Name = %s\n",ptr);
    if(ptr){
        free(ptr);
        ptr = NULL;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试在打开编译器警告的情况下对其进行编译时,得到以下信息:

error: conversion to ‘char’ from ‘int’ may alter its value [-Werror=conversion]|
Run Code Online (Sandbox Code Playgroud)

这意味着以下内容ptr[i] …

c casting

1
推荐指数
1
解决办法
5395
查看次数

"指向int问题的指针"

今天我尝试从这里解决一个测验,当我到达问题3时,有以下代码:

#include <stdlib.h>

int main(void){
    int *pInt;
    int **ppInt1;
    int **ppInt2;

    pInt = (int*)malloc(sizeof(int));
    ppInt1 = (int**)malloc(10*sizeof(int*));
    ppInt2 = (int**)malloc(10*sizeof(int*));

    free( pInt );
    free( ppInt1 );
    free( *ppInt2 );
}
Run Code Online (Sandbox Code Playgroud)

问题是:

在C程序上面选择正确的语句:

A - malloc() for ppInt1 and ppInt2 isn’t correct. It’ll give compile time error.
B - free(*ppInt2) is not correct. It’ll give compile time error.
C - free(*ppInt2) is not correct. It’ll give run time error.
D - No issue with any of the malloc() and …
Run Code Online (Sandbox Code Playgroud)

c malloc free valgrind gcc8

1
推荐指数
1
解决办法
139
查看次数

排序错误输出+在Array中找到bigges

今天我创建了一个具有3个功能的程序:

sortArray(数组,长度);
removeDuplicateInArray(array,length);
max = findMax(array,length);

该程序工作正常但是,如果我运行它多次,比方说三次,输出只有一个OK,其他两个是不同的,我认为在某种程度上与findMax函数中的数组长度有关,因为我删除重复和数组不具有相同的大小.我不确定是否有问题.

该计划是这样的:

#include<stdio.h>

void sortArray(int *array, int length){
    int i, j, k, temp;

    for(i=0;i<length-1;i++){
        for(k=0;k<length-i-1;k++){
            if(array[k]<array[k+1]){
                temp=array[k];
                array[k]=array[k+1];
                array[k+1]=temp;
            }
        }
    }

    for(j=0;j<length;j++){
        printf("%d ",array[j]);
    }
    printf("\n\n");
}

void removeDuplicateInArray(int *array, int length){
    int i,j,k;

    for (i = 0; i < length; i++) {
      for (j = i + 1; j < length;j++) {
         if (array[j] == array[i]) {
            for (k = j; k < length; k++) {
               array[k] = array[k + 1];
            } …
Run Code Online (Sandbox Code Playgroud)

c arrays

0
推荐指数
1
解决办法
43
查看次数

strlen 并不总是一个好主意

我不确定我是否选择了正确的标题,但今天我发现(作为 C 的初学者)对我来说 strlen 并不总是在我需要时做出的正确决定。

所以我尝试了以下方法:

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

 int foo(char *s){
    int len = strlen(s);
    /* code here */
    return len;
 }

int main(void){
    char *name = "Michi";
    int len = foo(name);
    int a = 20, b = 10, c = a - b;

    if(c < len){
        printf("True:  C(%d) < Len(%d)\n",c,len);
    }else{
        printf("False:  C(%d) > Len(%d)\n",c,len);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

错误:C(10) > Len(5)

但是当我用“ -Wconversion ”编译时,我得到:

program.c:5:19: warning: conversion to ‘int’ from ‘size_t’ may alter its value [-Wconversion]
         int …
Run Code Online (Sandbox Code Playgroud)

c

-6
推荐指数
1
解决办法
4747
查看次数

标签 统计

c ×7

gcc8 ×2

arrays ×1

c++ ×1

casting ×1

errno ×1

free ×1

malloc ×1

valgrind ×1