小编mch*_*mch的帖子

C-如何释放以下malloced内存

有人可以帮我解决如何在这里释放二维数组.我尝试使用循环释放但我得到一个错误说:

 *** glibc detected *** ./assignment4: free(): invalid pointer: 0x0932b196 ***
Run Code Online (Sandbox Code Playgroud)

问题是当我单独释放第0个元素时,我没有遇到任何问题.但是当我释放元素[1]到[7]时,我得到了错误.

char ** stringOne; 
stringOne = malloc(sizeof(char*)*8);

stringOne[0] = malloc(sizeof(char)*6);
stringOne[0] = strtok(listofdetails[0], " ");

for(i=1;i<8;i++)
{
    stringOne[i] = malloc(sizeof(char)*6);
    stringOne[i] = strtok(NULL, " ");
}

for(i=0;i<8;i++) // FREEING memory
{
    free(stringOne[i]);
}
Run Code Online (Sandbox Code Playgroud)

c malloc free

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

如何快速填充结构值?

我使用Visual Studio 2013与标准C.

我的程序需要四行才能 {"Analysis 1", "AN 1", 0, 0.0667}输入Fach[0].

我不想浪费这么多行来填充这个数组.

有没有更好的方法将这四行合二为一?

#define _CRT_SECURE_NO_WARNINGS
#include "header.h"
#include <stdio.h>
#include <string.h>

struct sFach
{
    char Mod[40];       //Modulbezeichnung
    char Ab[5];         //Abkürtzung
    int Note;           //Note
    double Gew;         //Gewichtungsfaktor
};

int main()
{
    struct sFach Fach[31];

    strcpy(Fach[0].Mod, "Analysis 1");
    strcpy(Fach[0].Ab, "AN 1");
    Fach[0].Note = 0;
    Fach[0].Gew = 0.0667;

    strcpy(Fach[1].Mod, "Algebra");
    strcpy(Fach[1].Ab, "AL");
    Fach[1].Note = 0;
    Fach[1].Gew = 0.0889;

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

c arrays structure

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

如何处理子函数中的const传播

我需要修改现有函数,有一些const输入参数:

int f(const owntype *r1, const owntype *r2)
Run Code Online (Sandbox Code Playgroud)

为了做到这一点,我想调用一个使用相同类型但没有const关键字的子函数:

void subfunction (owntype *src, owntype *dst)
Run Code Online (Sandbox Code Playgroud)

我已经尝试了这个(以及其他一些变体),但它不起作用:

int f(const owntype *r1, const owntype *r2) {
  ...
  subfunction((const owntyp*) r1);
  ...
}
Run Code Online (Sandbox Code Playgroud)

如何在不需要更改两个函数的parmeter描述的情况下编译它?

c const

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

嵌套的printf语句工作

我无法理解以下代码如何给出不同的输出

#include <stdio.h>
int main()
{
        int i=43;
        printf("%d\n",printf("%d",printf("%d",i)));
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出: 4321

printf("%d\n",printf("%d",printf("%d ",i)));
Run Code Online (Sandbox Code Playgroud)

输出: 43 31

printf("%d\n",printf("%d ",printf("%d ",i)));
Run Code Online (Sandbox Code Playgroud)

输出: 43 3 2

printf("%d\n",printf("%d ",printf(" %d ",i)));
Run Code Online (Sandbox Code Playgroud)

产量 43 4 2

printf("%d\n",printf(" %d ",printf(" %d ",i)));
Run Code Online (Sandbox Code Playgroud)

输出: 43 4 3

和其他变化也给出了其他产出.

如何只是一个空格改变一个数字.

提前致谢.

c printf

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

如果我们在增加指针后删除数组会发生什么?

int main(){

int *p=new int[5];

//case 1: delete p;
//case 2: p++;delete[] p;


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

如果我分别使用案例1和案例2会发生什么?

c++ arrays dynamic

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

Makefile:make运行命令

这是我的 makefile。我知道有一种更短的方法可以写出来,但我有一个关于如何运行它的问题。我的硬件说我必须使用命令: make run --- 该命令应该导致可执行文件使用文件重定向来运行以读取输入文件数据。

我该如何设置呢?

我也知道 gcc 应该是选项卡式的。

test:  main.o sum.o stack.o bSearch.o database.o db.o set.o parse.o bubble.o
    gcc -o object main.o sum.o stack.o bSearch.o db.o set.o parse.o bubble.o

main.o: main.c sum.h
gcc -c main.c

sum.o: sum.c sum.h
    gcc -c sum.c

stack.o: stack.c stack.h
    gcc -c stack.c

bSearch.o: bSearch.c defs.h sortAndsearch.h
    gcc -c bSearch.c

database.o: database.c defs.h parse.h
    gcc -c database.c

db.o: db.c defs.h
    gcc -c db.c

set.o: set.c set.h db.h
    gcc -c set.c

parse.o: parse.c parse.h
    gcc …
Run Code Online (Sandbox Code Playgroud)

c makefile

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

C语言中的以下代码有什么问题?

我观察到int *x = malloc(sizeof(int));这个代码正在尝试将void*转换为int*而不使用正确的类型转换.所以,根据我的答案应该是选择一个.但是在官方的GATE-2017考试答案中,答案是给予D.所以我错了吗?怎么样 ?

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

int *assignval(int *x, int val){
  *x = val;
  return x;
}

void main(){
    clrscr();
    int *x = malloc(sizeof(int));
    if(NULL==x) return;
    x = assignval(x,0);
    if(x){
        x = (int *)malloc(sizeof(int));
        if(NULL==x) return;
        x = assignval(x,10);
    }
    printf("%d\n",*x);
    free(x);
    getch();
}
Run Code Online (Sandbox Code Playgroud)
  • (A)编译器错误,因为malloc的返回没有适当地进行类型转换.
  • (B)编译器错误,因为比较应该是x == NULL而不是如图所示.
  • (C)编译成功但执行可能导致悬空指针.
  • (D)编译成功但执行可能导致内存泄漏.

在我看来,选项D只在int *x = (int *)malloc(sizeof(int));使用时才是正确的.

c pointers

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

类型定义不完整(结构体)

我尝试了很多次来修复错误” Incomplete Definition of Type of struct *,请您能帮我找到问题并解决吗?

有什么建议吗?

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
struct mypoint;
typedef struct mypoint {
    int x;
    int y;
};

void setRandomPoint(struct mypoint *a, int min, int max)
{
    do {
        a->x = rand();

    } while (a->x > max || a->x < min);
    do
    {
        a->y = rand();
    } while (a->y > max || a->y < min);


}
int main()
{
    struct myPoint point;
    int min = 0, max = 0;
    int i;
    srand(time(NULL));
    while …
Run Code Online (Sandbox Code Playgroud)

c struct visual-studio-2015

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

为什么UDP套接字无法从nc -u主机端口接收udp流量?

 int main(int argc, char *argv[])
 {
     struct sockaddr_in src = { .sin_family=AF_INET, .sin_addr.s_addr=INADDR_ANY, .sin_port=htons(90) };

     int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);

     bind(fd, (struct sockaddr *)&src, sizeof(src));

     char buf[1024];
     ssize_t res = recvfrom(fd, buf, sizeof(buf), 0, NULL, 0);
     printf("res=%zi\n", res);

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

编译并执行此程序之后。在另一个终端上执行

nc -u localhost 90
Run Code Online (Sandbox Code Playgroud)

测试自从我使用INADDR_ANY以来,我是否确实从“任何接口”接收到一些udp流量。但是程序只是挂起。我想念什么?

c sockets udp netcat

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

我如何在 C++ 中使用 Cin 输入 1 行?

就像 if ab是变量 then

cin>>a>>b;
Run Code Online (Sandbox Code Playgroud)

像这样,我可以用 1 取多少个变量cin

c++

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