我有以下程序:
#include <stdio.h>
#include <stdlib.h>
int* testPointerReturnType();
int main(void)
{
int x = 2;
printf("Value of x is %d and its address is %p\n", x, &x);
int* y = testPointerReturnType();
printf("Value of y is %d and its address is %p\n", *y, y);
}
int* testPointerReturnType()
{
int x = 33;
int r = 99;
return &x;
}
Run Code Online (Sandbox Code Playgroud)
以下是我的整体o/p(我将在下面的问题中讨论这个问题):
// This is with ** int r = 99; ** line commented
jharvard@appliance (~/psets/pset5/pset5_test): ./PointerAddresss
Value of x is 2 and its …
Run Code Online (Sandbox Code Playgroud) PS:我几乎所有的问题都是"无效的下一个大小",但他们没有帮助我,因为我没有另外一段代码可以使用malloc或realloc,所以这是排除在外的.另外,我没有超出内存空间的限制,所以我也很好.
我有以下代码给我 - *** Error in
./server_issue':realloc():下一个大小无效:0x08249170***`.
我无法识别问题,我一次只分配64个字节,所以我没有内存地址空间,还有其他内存分配可能会损坏我的堆内存.
错误说,下一个大小无效但是因为我的上一个日志告诉Reallocating 64 bytes, Aborted (core dumped)
所以这意味着我仍然在尝试分配64个字节.那么,为什么错误呢?
对于HTML文件,任何超过256个字节的文件.
代码:(重现问题的最小代码)
#include<stdio.h>
#include<stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef char BYTE;
bool load(FILE*, BYTE**, size_t*);
int main(void)
{
FILE* file = fopen("/home/university/psets/pset6/pset6_working/public/hello.html", "r");
BYTE* content;
size_t length;
load(file, &content, &length);
}
bool load(FILE* file, BYTE** content, size_t* length)
{
int totalLength = 0;
int readBytes = 0;
*content = NULL;
BYTE* fileContentTemp[64]; // working with 222222
while ((readBytes = fread(fileContentTemp, 1, 64, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个链接列表.所以,我宣布了一个LL数据结构如下:
typedef struct linkedList
{
char* word;
struct linkedList* next;
} linkedList;
Run Code Online (Sandbox Code Playgroud)
现在,我声明了一个表示LL的头部的全局变量 linkedList* head = NULL;
现在,我很惊讶,即使我为指针分配了一个NULL值,我也可以进入下面的while
循环.
linkedList* node = head;
while(node != NULL);
{
//Why I am here when I have not yet malloc'ed head and through global variable initialization my head is NULL
}
Run Code Online (Sandbox Code Playgroud)
因此,我觉得我无法检查我的链表head
是否已初始化或分配了一些内存.我无法检查,node->next
因为它会导致分段错误错误并且node == NULL
正在通过!
请注意:
head
是已初始化还是已分配了一些内存.我想使用我的链表数据结构来做到这一点.这是因为假设我运送我的数据结构,并希望如果有人试图删除某些内容并且链接列表然后为空,那么应该打印/抛出一些含义完全错误.请问任何想法.
我有一个节点,我正在定义其全局指针变量,如下所示:
typedef struct node
{
char* word;
struct node* next;
} node;
node* HashTable = NULL;
node* HeadOfHashTable = NULL;
Run Code Online (Sandbox Code Playgroud)
现在,我分配了如下内存:
void allocateMemory(int numOfElements, bool isRealloc, const char* word)
{
if(!isRealloc)
{
printf("Allocating %d blocks\n", numOfElements);
HashTable = malloc(sizeof(node*) * numOfElements);
} else {
printf("Reallocating %d blocks for %s", numOfElements, word);
HashTable = realloc(HashTable, sizeof(node*) * numOfElements);
}
if(HashTable == NULL)
{
printf("### Out Of Memory ###\n");
exit(0);
}
HeadOfHashTable = HashTable;
}
Run Code Online (Sandbox Code Playgroud)
现在,我正在传递一个HASH值和单词以放入哈希表,在下面的方法中.我已经评论了我在哪里遇到了seg错误.
void putInHashTable(char* ch, unsigned int hashValue) …
Run Code Online (Sandbox Code Playgroud) 我从Integer Overflow Wiki读取以下行:
无符号整数溢出导致数量减去模2的幂,这意味着无符号整数在溢出时"环绕".
我有下面的代码,我试图创建一个哈希函数,并得到int溢出的情况.我试图通过使用缓解它,unsigned int
但它没有工作,我能够看到负值.
我知道我可以通过其他方式处理它并且它可以工作,如我的代码注释所示 - Comment 2:
.但这是正确的方式,为什么unsigned int
没有环绕和溢出?
int hash(char *word) {
char *temp = word;
unsigned int hash = 0; // Comment 1: I tried to handle int overflow using "unsigned" int.
while (*word != '\0') {
// Comment 2: This works but I do not want to go this way.
//while ((hash * PRIME_MULTIPLIER) < 0) {
// hash = (hash * PRIME_MULTIPLIER) + 2147483647;
//}
hash = …
Run Code Online (Sandbox Code Playgroud)