realloc

Dynamic arrays: using realloc() without memory leaks

余生长醉 提交于 2019-12-18 06:17:21
问题 I use realloc to resize the memory allocated: char **get_channel_name(void) { char **result; int n; result = (char **) 0; for (elem = snd_mixer_first_elem(handle), n = 0; elem; elem = snd_mixer_elem_next(elem)) { if (!snd_mixer_selem_is_active(elem)) continue; if (snd_mixer_selem_has_playback_volume(elem) && snd_mixer_selem_has_playback_switch(elem) && snd_mixer_selem_has_capture_switch(elem)) { if (result == (char **) 0) result = (char **) malloc(sizeof(char *)); else result = (char **)

can realloc move pointer if new size smaller?

若如初见. 提交于 2019-12-18 04:02:41
问题 I am wondering whether the C or C++ standard guarantees that a pointer is not changed when realloc is called with a smaller (nonzero) size: size_t n=1000; T*ptr=(T*)malloc(n*sizeof(T)); //<--do something useful (that won't touch/reallocate ptr of course) size_t n2=100;//or any value in [1,n-1] T*ptr2=(T*)realloc(ptr,n2*sizeof(T)); //<-- are we guaranteed that ptr2==ptr ? Basically, can the OS decide on its own that since we freed a large memory block, he wants to take advantage of all

How to update other pointers when realloc moves the memory block?

偶尔善良 提交于 2019-12-17 20:04:03
问题 The realloc reference says: The function may move the memory block to a new location, in which case the new location is returned. Does it mean that if I do this: void foo() { void* ptr = malloc( 1024 ); unsigned char* cptr = ( unsigned char* )ptr+256; ptr = realloc( ptr, 4096 ); } then cptr may become invalid if realloc moves the block? If yes, then does realloc signal in any way, that it will move the block, so that I can do something to prevent cptr from becoming invalid? 回答1: Yes, cptr

How do you 'realloc' in C++?

℡╲_俬逩灬. 提交于 2019-12-17 04:27:19
问题 How can I realloc in C++? It seems to be missing from the language - there is new and delete but not resize ! I need it because as my program reads more data, I need to reallocate the buffer to hold it. I don't think delete ing the old pointer and new ing a new, bigger one, is the right option. 回答1: Use ::std::vector! Type* t = (Type*)malloc(sizeof(Type)*n) memset(t, 0, sizeof(Type)*m) becomes ::std::vector<Type> t(n, 0); Then t = (Type*)realloc(t, sizeof(Type) * n2); becomes t.resize(n2); If

Array of Structs memory realloc stderr

醉酒当歌 提交于 2019-12-13 21:08:08
问题 I am trying to realloc an array of structs when it runs out of space as I'm putting elements into it but I keep receiving a realloc stderr. The array of struct will eventually have 235,000 elements in it. When I set the initial start size to 100,000 I receive the stderr when trying to realloc. If I se the initial start size to 300,000 it does not give the error because it never reaches the realloc statement. #define _XOPEN_SOURCE #include <stdlib.h> #include <stdio.h> #include <assert.h>

resizing buffer using realloc

被刻印的时光 ゝ 提交于 2019-12-13 11:50:23
问题 If the area pointed to was moved, a free(ptr) is done. Can you please explain the above line about realloc() ? This line is from a man page for calloc, malloc, realloc and free. 回答1: I think this explains it better: If sufficient space does not exist to expand the current block in its current location, a new block of the size for size is allocated, and existing data is copied from the old block to the beginning of the new block. The old block is freed, and the function returns a pointer to

QGLViewer simpleViewer example built with cmake not running

僤鯓⒐⒋嵵緔 提交于 2019-12-13 08:44:48
问题 I'm trying to familiarize with QGLViewer (http://libqglviewer.com/) so I installed it (on Ubuntu 14.04) and I'm trying to run the simpleViewer (which is a provided example). Now, the code can be built using qmake, but I want to compile the code with cmake so I wrote the following CMakeLists.txt: cmake_minimum_required(VERSION 2.6) PROJECT(simple_viewer) SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules) FIND_PACKAGE(OpenGL REQUIRED) INCLUDE_DIRECTORIES(${OPENGL_INCLUDE}) FIND_PACKAGE

Getting (core dumped) when using realloc

随声附和 提交于 2019-12-13 06:06:41
问题 void replace(char *str) { unsigned int len = 0; unsigned int no_of_spaces = 0; while (*str) { if ((char)*str == SPACE) no_of_spaces++; str++; len++; } unsigned int new_len = len + 2 * no_of_spaces; str = (char*) realloc(str, new_len * sizeof(char)); str[new_len] = '\0'; } I use function like replace("random string"); . Here I am trying to increase the size of the string so that spaces can be replaced with another string. For this I need to count the no of spaces and also get the length of the

Realloc causing program to crash

杀马特。学长 韩版系。学妹 提交于 2019-12-13 04:55:24
问题 Currently trying to write menu based program that asks user for student records, after initial records are entered I want user to have option to add more record slots but when I try to realloc my 2d pointer array my program crashes, specifically after the 2nd function is called. #include <stdio.h> #include <string.h> #include <stdlib.h> #define LENGTH 21 void printRecords(int* size, char **fistName, char **lastName, float *grade); void addRecord(int* size, char** firstName, char** lastName,

How should I malloc/realloc with a struct that includes an array?

本秂侑毒 提交于 2019-12-12 16:01:10
问题 I'm pretty new to c, so if my steps are wrong, please let me know. Let's say that I have something like the following: struct graphNode{ int val; graphNode* parent; int succSize; int succMaxSize; graphNode* succ[1]; }; I will create a new node with: graphNode *n; n = malloc(sizeof(struct graphNode)); assert(n); n->val = 1; n->parent = NULL; n->succSize = 0; n->succMaxSize = 1; Then, if I want to add a successor to the node if (n->succSize == n->succMaxSize){ n->succ = realloc(n->succ, sizeof