dynamic-allocation

Alternatives to dynamic allocations in safety critical projects (C)

依然范特西╮ 提交于 2019-12-10 15:19:52
问题 Safety critical projects do not recommend any dynamic allocations or freeing allocated memory. Only during elaboration/initialization phase of the program execution, it is allowed. I know most of you will argue to implement SW in terms where it should do all static allocations only or do some justification in the code that dynamic allocations will not harm the overall program,etc but still, Is there any alternative to this problem? Is there any way or any example to kind of allocate some

Creating a user-inputted sized Array using new operator

大憨熊 提交于 2019-12-10 12:58:38
问题 I have a few array-related questions. I've studied that array-size must be constant on declaration/compiler must know its value. But using the GNU GCC compiler (C++11 standard filter) and I am able to perfectly compile and run a program using a variable as array size, when declaring said array dynamically (using new ) int num; cout << "How big an array? "; cin >> num; int *arr = new int [num]; Ques1) Is this considered standard? My profs are contradictory. Ques2) If it is standard, in that

Efficiently generating byte buffer without breaking strict aliasing

人盡茶涼 提交于 2019-12-06 09:32:01
问题 This is such a simple pattern, there has to be a "nice" way of sorting it out. I have a function that needs to generate a dynamically sized byte array containing arithmetic data. // Given that I have a function that kinda looks like this: void dispatch(std::vector<char> data); //Will take possesion of data. // The behavior I want, but this breaks strict aliasing void bad_foo(int c) { std::vector<char> real_data(c * sizeof(float)); float* raw_data = reinterpret_cast<float*>(real_data.data());

Fortran array automatically growing when adding a value

两盒软妹~` 提交于 2019-12-06 09:22:46
Is there any existing way to emulate growing array in Fortran? Like vector in C++. I was very surprised when I haven't found anything on this subject on the Internet. As a motivation example, suppose I compute some recurrence relation and I want to store all the intermediate numbers I get. My stopping criterion is the difference between adjacent results so I cannot know beforehand how much memory I should allocate for this. I am sure it has been shown somewhere on this site before, but I cannot find it. First, in Fortran 2003, you can add one element by simple a = [a, item] as commented by

Difference between char[] and new char[] when using constant lengths

无人久伴 提交于 2019-12-05 02:15:28
问题 So this may seem like a widely-answered question, but I'm interested more in the internals of what exactly happens differently between the two. Other than the fact that the second example creates not only the memory, but a pointer to the memory , what happens in memory when the following happens: char a[5]; char b* = new char[5]; And more directly related to why I asked this question, how come I can do const int len = 5; char* c = new char[len]; but not const int len = 5; char d[len]; //

Efficiently generating byte buffer without breaking strict aliasing

久未见 提交于 2019-12-04 17:14:31
This is such a simple pattern, there has to be a "nice" way of sorting it out. I have a function that needs to generate a dynamically sized byte array containing arithmetic data. // Given that I have a function that kinda looks like this: void dispatch(std::vector<char> data); //Will take possesion of data. // The behavior I want, but this breaks strict aliasing void bad_foo(int c) { std::vector<char> real_data(c * sizeof(float)); float* raw_data = reinterpret_cast<float*>(real_data.data()); //Fill raw_data with usefull stuff... dispatch(std::move(real_data)); } void correct_but_slow_foo(int c

warning: function returns address of local variable [enabled by default] [duplicate]

淺唱寂寞╮ 提交于 2019-12-04 12:31:58
问题 This question already has answers here : error: function returns address of local variable (7 answers) Closed 6 years ago . #include <string.h> #include<stdio.h> #include<stdlib.h> char *chktype(char *Buffer, int Size) { char *strng = "Content-Type: "; int sz; char *found = strstr (Buffer, strng); char *found1 = strstr(found, "\r\n"); sz=strlen(found)-strlen(found1); char type[sz]; strncpy(type, found1, sz-1); return(type); } void main(){ char *buffer = "HTTP/1.1 200 OK\r\nDate: Tue, 25 Jun

Difference between char[] and new char[] when using constant lengths

五迷三道 提交于 2019-12-03 17:07:42
So this may seem like a widely-answered question, but I'm interested more in the internals of what exactly happens differently between the two. Other than the fact that the second example creates not only the memory, but a pointer to the memory , what happens in memory when the following happens: char a[5]; char b* = new char[5]; And more directly related to why I asked this question, how come I can do const int len = 5; char* c = new char[len]; but not const int len = 5; char d[len]; // Compiler error EDIT Should have mentioned I'm getting this compiler error on VC++ (go figure...) 1>.\input

warning: function returns address of local variable [enabled by default] [duplicate]

这一生的挚爱 提交于 2019-12-03 08:31:24
This question already has an answer here: error: function returns address of local variable 7 answers #include <string.h> #include<stdio.h> #include<stdlib.h> char *chktype(char *Buffer, int Size) { char *strng = "Content-Type: "; int sz; char *found = strstr (Buffer, strng); char *found1 = strstr(found, "\r\n"); sz=strlen(found)-strlen(found1); char type[sz]; strncpy(type, found1, sz-1); return(type); } void main(){ char *buffer = "HTTP/1.1 200 OK\r\nDate: Tue, 25 Jun 2013 16:27:16 GMT\r\nExpires: -1\r\nCache-Control: private, max-age=0\r\nContent-Type: text/html; charset=UTF-8\r\nContent

c++ EOF running one too many times?

好久不见. 提交于 2019-12-02 14:01:37
问题 This is my first time using EOF and/or files, and I am having an issue where my code hangs, which I believe is because my EOF is looping one too many times. I am imputing from a file, and dynamically creating objects that way, and it hangs once the file is run through. while( !studentFile.eof() ) { cout << "38\n"; Student * temp = new Student(); (*temp).input( studentFile ); (*sdb).insert( (*temp) ); } This chunk of code is the code in question. The cout >> "38\n"; is the line number and the