今天我到了The C Programming Language(第二版Brian W. Kernighan和Dennis M. Ritchie)的第167页,发现作者说我必须演员.这是本书的一部分:malloc
7.8.5存储管理
函数malloc和calloc动态获取内存块.
Run Code Online (Sandbox Code Playgroud)void *malloc(size_t n)返回指向未初始化存储的n个字节的指针,如果无法满足请求,则返回NULL.
Run Code Online (Sandbox Code Playgroud)void *calloc(size_t n, size_t size)为具有指定大小的n个对象的数组返回指向足够可用空间的指针,如果无法满足请求,则返回NULL.存储初始化为零.malloc或calloc返回的指针对于有问题的对象具有正确的对齐方式,但必须将其强制转换为适当的类型,如
Run Code Online (Sandbox Code Playgroud)int *ip; ip = (int *) calloc(n, sizeof(int));
我已经知道malloc(和它的族)返回类型void*,并且有很好的解释为什么不进行转换malloc.
但我的问题是:为什么这本书说我应该施展它?
基于这个问题(c中的奇怪输出问题)有一个答案(由@Lundin提供)关于这一行:
int *ptr = (int*)(&a+1);
Run Code Online (Sandbox Code Playgroud)
在哪里他说:
the cast (int*) was hiding this bug.
所以我带来了以下内容:
#include <stdio.h>
int main( void ){
int a[5] = {1,2,3,4,5};
int *ptr = *( ( &a ) + 1 );
printf("%d", *(ptr-1) );
}
Run Code Online (Sandbox Code Playgroud)
我想知道这是否:
int *ptr = *( ( &a ) + 1 );
Run Code Online (Sandbox Code Playgroud)
标准明确定义了吗?
编辑:
在某些时候@chux指出§6.3.2.3.7哪个是:
Run Code Online (Sandbox Code Playgroud)A pointer to an object type may be converted to a pointer to a different object type. If the resulting …
fgets函数的声明如下所示:
char *fgets(char *str, int n, FILE *stream);
Run Code Online (Sandbox Code Playgroud)
这意味着第二个参数应该是一个int.
在以下程序中哪种方法可以避免这种转换?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
const char *buffer = "Michi";
size_t len = strlen(buffer) + 1;
char arr[len];
printf("Type your Input:> ");
if (fgets(arr, (int)len, stdin) == NULL) {
printf("Error, fgets\n");
exit(1);
} else {
printf("Arr = %s\n", arr);
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我使用(int)len哪个看起来很好,但如果buffer存储一个很长的字符串会发生什么?
让我们说:
const char *buffer = "Very long long ..."; /* where length is beyond the range of …Run Code Online (Sandbox Code Playgroud) 就像标题说我正在开发一个应用程序,我需要设置所有4个孩子的背景,但我不知道如何做到这一点.
我需要改变这样的背景:
这是代码:
#include <gtk/gtk.h>
int main(int argc, char *argv[]){
//---------- CSS -------------
GtkCssProvider *provider;
GdkDisplay *display;
GdkScreen *screen;
//---------------------------
GtkWidget *window;
GtkWidget *child, *child2, *child3, *child4;
GtkWidget *grid;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Equalizer");
gtk_window_set_default_size(GTK_WINDOW(window), 400, 250);
gtk_container_set_border_width(GTK_CONTAINER(window), 5);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
// ---------------------------------------------------- CSS -----------------------------------------------------------
provider = gtk_css_provider_new ();
display = gdk_display_get_default ();
screen = gdk_display_get_default_screen (display);
gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
const …Run Code Online (Sandbox Code Playgroud) ISO C标准指出:
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
Run Code Online (Sandbox Code Playgroud)
我在BIT Linux mint(19.1)上使用GCC-8,大小long int为8。
我正在使用使用GCC 7的应用程序,并且编译器是64位的。的大小long int为4。编译器或操作系统是否定义a的大小long int?
就像标题所说,我需要code::blocks与之合作C11,我无法弄清楚如何去做.
我去了settings=> compiler settings=> Other options我添加-std=c11并尝试了-std=gnu11,两者似乎都没有用.
我编译gcc-5.2然后我更改了默认编译器(gcc-4.9)仍然没有结果.
当我尝试编译以下程序时:
#include<stdio.h>
int main(void){
int arr[] = {0,1,2,3,4};
for(int i=0;i<5;i++){
printf("%d ",arr[i]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到以下内容:
|6|error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode|
Run Code Online (Sandbox Code Playgroud)
但如果我在终端(ubuntu 15.04,64BIT,gcc-5.2)中这样做:
./install/gcc-5.2.0/bin/gcc5.2 program.c -o program
Run Code Online (Sandbox Code Playgroud)
似乎工作正常.
我的问题是,如何code::blocks与之合作c11?
我目前正在Linux Mint我的机器上运行GCC-5.3因为C11包含默认值.
我开始C为自己学习,只是为了好玩,当时我的GCC版本是4.8正确的.
如果在以下程序中使用GCC-4.8带有-pedantic标志的任何方式:
#include <stdio.h>
#include <string.h>
int main(void){
char *arr = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
size_t length = strlen(arr);
printf("Length of Arr = %zu\n",length);
}
Run Code Online (Sandbox Code Playgroud)
在编译时,会收到以下警告:
program.c: In function ‘main’:
program.c:5:5: warning: string length ‘510’ is greater than the length ‘509’ ISO C90 compilers are required to support [-Woverlength-strings]
char *arr = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
^
program.c:7:5: warning: ISO C90 does not support the ‘z’ gnu_printf length …Run Code Online (Sandbox Code Playgroud) 我今天尝试检查答案,我意识到,如果我使用代码块(使用gcc),我必须使用gcc处理与命令行(Ubuntu Linux)不同的错误.
该计划是这样的:
#include<stdio.h>
#include<math.h>
int main(void){
double len,x,y =0;
int n=123456;
len=floor(log10(abs(n))) + 1;
x = n / pow(10, len / 2);
y = n - x * pow(10, len / 2);
printf("First Half = %f",x);
printf("\nSecond Half = %f",y);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试编译它我得到:
错误:函数'abs'的隐式声明[-Werror = implicit-function-declaration] |
所以这是有趣的事情.我将-lm添加到Compiler => global compiler => settings => Other settings,但结果是一样的.
它只在我包含stdlib.h时才有效.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(void){
double len,x,y =0;
int n=123456;
len=floor(log10(abs(n))) + 1;
x = n / pow(10, len …Run Code Online (Sandbox Code Playgroud) 我设法成功使用可变长度数组,C现在我有以下内容:
#include <stdio.h>
#include <stdlib.h>
int (*foo(size_t row, size_t col))[3];
int main(void){
size_t row, col;
printf("Give the ROW: ");
if ( scanf("%zu",&row) != 1){
printf("Error, scanf ROW\n");
exit(1);
}
printf("Give the COL: ");
if ( scanf("%zu",&col) != 1){
printf("Error, scanf COL\n");
exit(2);
}
int (*arr)[col] = foo(row, col);
for ( size_t i = 0; i < row; i++){
for( size_t j = 0; j < col; j++){
printf("%d ",*(*(arr+i)+j));
}
}
free(arr);
}
int (*foo(size_t row, size_t col))[3]{ …Run Code Online (Sandbox Code Playgroud) 关于我的问题有一些相关的讨论,但没有关于我的问题的明确解释.
我认为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()它,那么它将无法工作,因为它发生了我将要释放一个非堆对象. …