==17209== Conditional jump or move depends on uninitialised value(s)
==17209== at 0x402E7C5: __GI___rawmemchr (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==17209== by 0x40CE921: _IO_str_init_static_internal (strops.c:45)
==17209== by 0x40B0B76: __isoc99_vsscanf (isoc99_vsscanf.c:42)
==17209== by 0x8048647: main (lala.c:23)
==17209== Uninitialised value was created by a stack allocation
==17209== at 0x8048659: gatherInfoSalt (lala.c:28)
==17209==
Run Code Online (Sandbox Code Playgroud)
。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
struct sysinfo_s {
char *salt_id;
};
void gatherInfoSalt(char ** );
int main (int argc, char *argv[]) {
struct sysinfo_s si;
si.salt_id = …Run Code Online (Sandbox Code Playgroud) 我开始学习 C 中的 struct 。今天我发现了一个我无法解决的问题。我有这个代码:
typedef struct fraze
{
char *mostSearch = NULL; // for string from user
double freq;
} s_FRAZE;
int readFraze( )
{
int i = 2, k;
size_t len = 0;
char c;
s_FRAZE *s;
s = (s_FRAZE *)malloc( i * sizeof( int ));
k = 0;
while( (c = getchar()) != '\n')
{
ungetc( c, stdin );
if( scanf( "%lf%c", &s[k].freq, &c) != 2 || c != ':' )
{
return 1;
}
if( k …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个程序,该程序首先为 100 个 int 元素动态初始化一个队列数组。每当队列已满并且另一个元素应该排队时,原始数组应该将其大小加倍,以便可以插入新元素。如果元素出队,并且队列包含的元素数量低于其实际大小的一半,则应该将队列大小减半。但是,它的大小永远不应低于 10。
我正在尝试使用 realloc 扩展和收缩数组,但是我在理解其机制时遇到了一些问题,尤其是在返回新指针时。下面是我的程序(有一些冗余printf的debugging原因):
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <iso646.h>
void enqueue(int arr[], int* lastElementIdx, int *length, int element);
int dequeue (int arr[], int* lastElementIdx, int *length);
void printQueue(const int arr[], int lastElementIdx);
int expandArray(int *arr, int length);
int shrinkArray(int *arr, int length, bool min);
void test1(int *arr, int* lastElementIdx, int *length)
{
int* temp = arr;
printf("\nprintQueue #1:\n"); //print queue, should be empty
printQueue(temp, *lastElementIdx);
for(int i = …Run Code Online (Sandbox Code Playgroud) 我想知道 realloc() 到底要花多少性能:我经常这样做是为了通过一个元素(=特定结构)扩展可用内存区域。多亏了 MMU,这样的 realloc() 只是保留内存区域的扩展,还是在某些条件下可以想象到所有数据的完整复制?
据我所知,当 std::vector 的大小增加并且预定义的内存量太小时,它通常必须复制内存区域......
我们可以通过使用and will 处理乘法来安全地分配 C 中x大小的元素。ycalloc(x, y)calloc()x*y
然而,realloc()例如仅将新大小作为参数,我想知道如何x*y使用realloc().
如果x*y不适合怎么办size_t?calloc()对此如何处理?
我的目标是在 C 中为二维 int 数组动态重新分配内存。我知道已经有几个关于该主题的问题,但不幸的是我的代码无法正常运行,我不知道出了什么问题。
首先我分配内存:
int n = 10;
int m = 4;
int** twoDimArray;
twoDimArray = (int**)malloc(n * sizeof(int*));
for(int i = 0; i < n; i++) {
twoDimArray[i] = (int*)malloc(m * sizeof(int));
}
Run Code Online (Sandbox Code Playgroud)
并用整数初始化数组:
for(int i = 0; i < n; i++) {
for(j = 0; j < 4; j++) {
twoDimArray[i][j] = i * j;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我用来realloc()动态重新分配内存:
int plus = 10;
int newArraySize = n + plus;
twoDimArray = (int**)realloc(twoDimArray, newArraySize * sizeof(int)); …Run Code Online (Sandbox Code Playgroud) 我决定学习 C 和 C++,但我在完成一项简单的任务时遇到困难,无法集中注意力。我非常感谢您的帮助。
在我的一个函数中,我创建了一个指针 *linesLenght ,然后调用另一个函数来分配内存并填充数组。在其中一个函数中,会发生 1-3 次重新分配。第一次一切都很顺利。但是当第二次尝试重新分配时,我得到了损坏的大小与 prev_size 错误。
代码:
第一。功能
char * wordWrap (int width, const char * src ){
ulong len = strlen(src);
ulong lines = getLines(width,len);
int * linesLenght = getLinesLen(&lines, src, len, width);
... }
Run Code Online (Sandbox Code Playgroud)
第二功能
int * getLinesLen(ulong * lines, const char * src, ulong srcLen, int maxLine){
int * linesLen = malloc((*lines) * sizeof(int));
ulong counter = 0;
for(int i = 0; i < srcLen; i++)
{
///other hidden logic....
if(counter == …Run Code Online (Sandbox Code Playgroud) 我用calloc函数分配了一个字符串:
//string1 and string2 previously declared
char *stringClone = calloc(strlen(string1) + 1, sizeof(char));
Run Code Online (Sandbox Code Playgroud)
现在我想用不同的字符串在stringClone上做同样的事情。正在做:
stringClone = calloc(strlen(string2) + 1, sizeof(char));
Run Code Online (Sandbox Code Playgroud)
我会有一些内存泄漏,对吧?在这种情况下我应该如何使用realloc?
我一直在阅读“如何重新分配使用 calloc 分配的一些内存? ”。现在我想知道如果块更大,arealloc后跟 a是否calloc会将新字节归零。
愚蠢的例子:
#include <stdlib.h>
#include <string.h>
int test() {
char *mem;
mem = calloc(100, 1);
mem = realloc(mem, 120);
memset(mem + 100, 0, 20); // is this even necessary?
}
Run Code Online (Sandbox Code Playgroud)
我已经对其进行了测试,似乎已将其归零 - 但我不确定是否总是如此?
通常,realloc() 用于重新分配先前分配的指针:
int *DynamicArray = malloc(sizeof(int)*SomeArbitraryValue);
// Some rando code where DynamicArray is used
DynamicArray = realloc(DynamicArray, sizeof(int)*SomeOtherArbitraryValue)
Run Code Online (Sandbox Code Playgroud)
但是realloc()可以用来直接分配内存吗?如在
int *DynamicArray = realloc(/*...*/);
Run Code Online (Sandbox Code Playgroud)
realloc() 可以处理非预分配指针吗?
c ×10
realloc ×10
calloc ×3
memory ×3
malloc ×2
allocation ×1
arrays ×1
c++ ×1
heap-memory ×1
memory-leaks ×1
performance ×1
pointers ×1
reference ×1
stack ×1
struct ×1
valgrind ×1