我正在尝试编写一个可以打印指针变量大小的ac程序,该变量指向malloc()分配的内存位置.
我正在使用的C版本是C99.
以下是我的代码:
#include<stdio.h>
int main()
{
int *temp;
temp=malloc(sizeof(int)*3);
printf("%d %d\n",sizeof(int),sizeof(temp));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
sh-4.2 $ ./f1 4 8
根据代码,printf应该为两个表达式打印相同的值.即12. sizeof(int)= 4.
因为我为temp分配了12个字节的内存,所以sizeof(temp)应该返回12.但是,它返回值8.
我想知道可能是什么原因?如果有人知道,请回答.
在分配大尺寸的动态数组时,我遇到了段错误.
作为一个具体示例,以下代码会导致段错误.
int max = 1399469912;
int *arr = (int*) malloc((max+1) * sizeof(int));
arr[0] = 1;
Run Code Online (Sandbox Code Playgroud)
但是,如果我用5之类的小东西替换max,那么我就不会遇到段错误.
为什么会这样?或者,是否有其他解决方案可以达到同样的效果?我需要一个动态分配的大小数组.
谢谢
在我的函数中使用它后,我无法释放此指针.它给了我这个错误信息.该函数应检查trie字典,以确定单词拼写是对还是错.而root是第一个trie节点.
`./peller'中的错误:free():无效指针:0x00007fe53a80d848
这是功能:
bool check(const char *word)
{
int pos=0;
int path;
char wordChar=*(word+pos);
wordTriePtr cursor=(wordTriePtr) malloc(sizeof(wordTrie));
cursor=root;
while(wordChar!='\0')
{
path=determinePath(wordChar);
if(cursor->children[path]==NULL)
{
free(cursor);
return false;
}
else
cursor = cursor->children[path];
wordChar=*(word+(++pos));
}
if(cursor->isThisWord==true)
{
free(cursor);
return true;
}
else
{
free(cursor);
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我对char指针的概念有点困惑,所以我创建了一个简单的代码,只打印用户(我)提供的名字.我也想练习malloc,所以我引用了指向RAM中某个内存的指针,但我真的不知道在"sizeof(char)*"后要放什么,因为这是用户输入,尚未确定.此外,在执行此操作后,我释放了内存,但我在命令行上收到一条错误消息:
*** Error in `./char': double free or corruption (fasttop): 0x00000000017fe030 ***
Aborted
Run Code Online (Sandbox Code Playgroud)
好像我释放了相同的内存两次或者什么,但我不知道要删除或添加什么.请帮忙!
#include <stdio.h>
#include <cs50.h>
int main (void)
{
char *strings = malloc(sizeof(char) * 10);
printf("What is your name?\n");
//wait for use to type his/her name
strings = get_string();
printf("Hello %s\n", strings);
free (strings);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我想创建一个具有确切UINT_MAX索引数的数组.由于某些原因,这非常困难.我尝试过以下方法:
char arr[UINT_MAX]; // Compiler claims array size cannot be negativechar* const arr = calloc(UINT_MAX, sizeof(char)); // Runs but seg fault when accessedchar* const arr = malloc(sizeof(char) * UINT_MAX); // arr is NULL我不明白发生了什么.这是我的HEAP尺寸太低了吗?如果是这样,我该如何增加它?无法malloc/calloc处理那种性质的块?我发现没有什么有用malloc或callocAPI页面.
这个问题适用于C和C++.
Windows 7 64位,16GB RAM,CMake 3.6.1,GCC 7.11.1,并在CLion 64位上编译.
我试图在C中编写一个简单的列表,它可以存储数字.
使用数字SIZE,将计算索引,并且数字必须存储在数组索引处的一种线性列表中.
但是,有时我会得到一个"分段错误",就像10次尝试中的2次一样,输出是正确的.
我进行了长时间的搜索,但我找不到问题.
请记住,我的"解决方案"没有完全实现,所以目前它只在计算的索引没有存储指针时才有效.(由于错误,无法继续编码.)
这是我的代码:
#define SIZE 3
#define NULL 0
typedef struct node_s node_t;
struct node_s {
node_t* next;
int number;
};
static node_t nodeArray[SIZE];
int addNumber(int numb){
int index = number % SIZE;
if(nodeArray[index].next == NULL){
node_t* node = (node_t*) malloc(sizeof(node_t));
node->number = number;
nodeArray[hash].next = node;
}
}
void print(){
for(int i = 0; i < SIZE; i++){
node_t* ptr = nodeArray[i].next;
while(ptr != NULL){
printf("%d -> ", ptr->number);
ptr = ptr->next;
}
printf("\n"); …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个用于井字游戏的蒙特卡罗树搜索,但是在运行它时会出现真正的内存问题(例如我的计算机内存不足).
所以我决定调查情况valgrind.
下面是一个代码块,上面valgrind写着"绝对泄漏".
void player_init (Player **p, player_t symbol)
{
*p = (Player *) malloc (sizeof (Player));
(**p).symbol = symbol;
(**p).score = 0;
}
void player_init_all (Player ***p)
{
*p = (Player **) malloc (sizeof (Player *) * 2);
for (int i = 0; i < 2; i++)
{
(*p)[i] = (Player *) malloc (sizeof (Player));
}
player_init (&(*p)[0], PLAYER1);
player_init (&(*p)[1], PLAYER2);
}
void player_destroy (Player *p)
{
free (p);
}
Run Code Online (Sandbox Code Playgroud)
在哪里Player和player_t …
所以我有这个功能:
source_t * source_init(char * argsource)
{
source_t * temp = (source_t *) malloc(sizeof(source_t *));
temp->path = realpath(argsource, NULL);
temp->dir = dirname(temp->path);
temp->name = basename(temp->path);
temp->ext = strrchr(temp->name, '.');
temp->content = read_file(temp->path); // here
temp->error = error_free();
return temp;
}
Run Code Online (Sandbox Code Playgroud)
它的调用功能read_file():
char * read_file(char * sourcepath)
{
char * buffer = NULL;
long string_size, read_size;
FILE * file = fopen(sourcepath, "r");
fseek(file, 0, SEEK_END);
string_size = ftell(file);
rewind(file);
buffer = (char *) malloc(sizeof(char) * (string_size + 1) ); …Run Code Online (Sandbox Code Playgroud) 我尝试用c来理解malloc和动态分配,但是当我编译程序时一切正常,但是如果我运行它终端告诉我Segmentation fault(core dumped)并退出
#include <stdio.h>
#include <stdlib.h>
int main(){
int **matrice;
int righe, colonne;
int r, c;
printf("Quante RIGHE deve avere la matrice? ");
scanf("%d", &righe);
printf("Quante COLONNE deve avere la matrice? ");
scanf("%d", &colonne);
matrice = (int**) malloc(righe*colonne*sizeof(int));
for(r=0; r<righe; r++){
matrice[r] = (int*) malloc(colonne*sizeof(int));
for(r=0; r<righe; r++){
for(c=0; c<colonne; c++){
printf("Elemento[%d][%d]: ",r, c);
scanf("%d", &matrice[r][c]);
}
// print out
for(r=0; r<righe; r++){
for(c=0; c<colonne; c++){
printf ("%d\n", matrice[r][c]);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我定义了以下结构
typedef struct {
uint16_t type;
uint16_t name_offset;
uint32_t data_offset;
uint32_t size;
}node;
Run Code Online (Sandbox Code Playgroud)
sizeof(node) 按预期返回12个字节.
node *nodes = (node*)malloc(sizeof(node)*nodecount);
Run Code Online (Sandbox Code Playgroud)
在我当前的测试中,nodecount是96,并且为节点分配的内存是预期的1152.(通过_msize测试)
我想从我在偏移量为20的缓冲区memcpy进入这个新节点数组(这是我崩溃的地方).我已经确认0x20(含)-0x4A0(不包括)是这个数组的正确结构.
memcpy(nodes,buffer[0x20],sizeof(node)*nodecount)
Run Code Online (Sandbox Code Playgroud)
缓冲区看起来像这样
00000020: 01 00 00 00 00 00 00 00 00 00 00 60
...
00000490: 00 00 00 88 00 00 06 89 02 15 DE 40
Run Code Online (Sandbox Code Playgroud)