dynamic-memory-allocation

Using realloc inside a function [duplicate]

a 夏天 提交于 2019-12-29 01:27:09
问题 This question already has answers here : C Programming: malloc() inside another function (8 answers) Closed 5 years ago . My apologies, I know many related questions have already been asked, so I will keep it very simple. Despite some years of programming I cannot find the correct syntax for resizing and modifying an array (or several) inside a function. For example, say I want a function to fill an array with a set of "n" numbers, where "n" is defined within the array: int main(int argc,

Allocating struct with variable length array member

删除回忆录丶 提交于 2019-12-28 03:06:07
问题 I know I can do new char[n] to create an array of n chars. This works even when n is not a compile time constant. But lets say I wanted a size variable followed by n chars: My first attempt at this is the following: struct Test { std::size_t size; char a[]; }; However it seems new Test[n] doesn't do what I expect, and instead allocates n size s. I've also found that sizeof(std::string) is 4 at ideone, so it seems it can allocate both the size and the char array in one block. Is there a way I

c++ dynamic array of pointers [duplicate]

爷,独闯天下 提交于 2019-12-27 07:02:31
问题 This question already has answers here : How do I declare a 2d array in C++ using new? (23 answers) Closed 2 years ago . I'm trying to understand how to create a dynamic array of pointers in C++. I understand that new returns a pointer to the allocated block of memory and int*[10] is an array of pointers to int . But why to you assign it to a int** ? I'm struggling to understand that. int **arr = new int*[10]; 回答1: According to the C++ Standard (4.2 Array-to-pointer conversion) 1 An lvalue or

c++ dynamic array of pointers [duplicate]

青春壹個敷衍的年華 提交于 2019-12-27 07:02:25
问题 This question already has answers here : How do I declare a 2d array in C++ using new? (23 answers) Closed 2 years ago . I'm trying to understand how to create a dynamic array of pointers in C++. I understand that new returns a pointer to the allocated block of memory and int*[10] is an array of pointers to int . But why to you assign it to a int** ? I'm struggling to understand that. int **arr = new int*[10]; 回答1: According to the C++ Standard (4.2 Array-to-pointer conversion) 1 An lvalue or

c++ dynamic array of pointers [duplicate]

旧城冷巷雨未停 提交于 2019-12-27 07:02:10
问题 This question already has answers here : How do I declare a 2d array in C++ using new? (23 answers) Closed 2 years ago . I'm trying to understand how to create a dynamic array of pointers in C++. I understand that new returns a pointer to the allocated block of memory and int*[10] is an array of pointers to int . But why to you assign it to a int** ? I'm struggling to understand that. int **arr = new int*[10]; 回答1: According to the C++ Standard (4.2 Array-to-pointer conversion) 1 An lvalue or

Why can freed dynamically allocated memory still be accessed after a delete operation in C++?

偶尔善良 提交于 2019-12-26 06:49:13
问题 Suppose I have allocated some memory for storing an int value like this: int *p=new int; Here I created the required memory using new operator and assigned the address of that memory block so that I can access that memory block. Now it's my control to what I store in that memory block. But when I write a statement like this: delete p; we say that I have deleted the dynamically allocated memory. But if I really delete 'd or freed up that memory, should I not be able to access that memory

Why can freed dynamically allocated memory still be accessed after a delete operation in C++?

こ雲淡風輕ζ 提交于 2019-12-26 06:49:07
问题 Suppose I have allocated some memory for storing an int value like this: int *p=new int; Here I created the required memory using new operator and assigned the address of that memory block so that I can access that memory block. Now it's my control to what I store in that memory block. But when I write a statement like this: delete p; we say that I have deleted the dynamically allocated memory. But if I really delete 'd or freed up that memory, should I not be able to access that memory

Exception thrown at 0x0F640E09 (ucrtbased.dll) in ConsoleApplication5.exe: 0xC0000005: Access violation writing location 0x014C3000?

我怕爱的太早我们不能终老 提交于 2019-12-25 18:53:16
问题 I just compiled this code, and it showed me this error: Exception thrown at 0x0F640E09 (ucrtbased.dll) in ConsoleApplication5.exe: 0xC0000005: Access violation writing location 0x014C3000. I literally have no idea what this error means as I've just been using C++ for a couple months, and I've also tried looking on any other websites to look for help, but I didn't find any. For this code, I'm only allowed to use the c-string functions and the <cstring> library. I cannot use the string object

Constructors for C++ objects

◇◆丶佛笑我妖孽 提交于 2019-12-25 03:29:07
问题 I have class Person as following : class Person { char* name; int age; }; Now I need to add two contructors. One taking no arguments, that inserts field values to dynamically allocated resources. Second taking (char*, int) arguments initialized by initialization list. Last part is to define a destructor showing information about destroying objects and deallocating dynamically allocated resources. How to perform this task ? That's what I already have : class Person { char* name; int age;

How to assign values dynamically to a struct

和自甴很熟 提交于 2019-12-25 03:28:20
问题 I am stumped as to how to access and change the values of a struct. The program takes in some external files and tokenized each string and categorizes them in the following fields of climate info. The external files look something like this: TDV format: TN 1424325600000 dn20t1kz0xrz 67.0 0.0 0.0 0.0 101872.0 262.5665 TN 1422770400000 dn2dcstxsf5b 23.0 0.0 100.0 0.0 100576.0 277.8087 TN 1422792000000 dn2sdp6pbb5b 96.0 0.0 100.0 0.0 100117.0 278.49207 TN 1422748800000 dn2fjteh8e80 6.0 0.0 100.0