dynamic-allocation

Is it well-defined to use a pointer pointing to one-past-malloc?

我是研究僧i 提交于 2021-02-05 20:14:34
问题 In C, it is perfectly well to make a pointer that points to one past the last element of an array and use it in pointer arithmetics, as long as you don't dereference it: int a[5], *p = a+5, diff = p-a; // Well-defined However, these are UBs: p = a+6; int b = *(a+5), diff = p-a; // Dereferencing and pointer arithmetic Now I have a question: Does this apply to dynamically allocated memory? Assume I'm only using a pointer pointing to one-past-the-last in pointer arithmetics, without

Is it well-defined to use a pointer pointing to one-past-malloc?

≯℡__Kan透↙ 提交于 2021-02-05 20:13:19
问题 In C, it is perfectly well to make a pointer that points to one past the last element of an array and use it in pointer arithmetics, as long as you don't dereference it: int a[5], *p = a+5, diff = p-a; // Well-defined However, these are UBs: p = a+6; int b = *(a+5), diff = p-a; // Dereferencing and pointer arithmetic Now I have a question: Does this apply to dynamically allocated memory? Assume I'm only using a pointer pointing to one-past-the-last in pointer arithmetics, without

Is it well-defined to use a pointer pointing to one-past-malloc?

℡╲_俬逩灬. 提交于 2021-02-05 20:13:04
问题 In C, it is perfectly well to make a pointer that points to one past the last element of an array and use it in pointer arithmetics, as long as you don't dereference it: int a[5], *p = a+5, diff = p-a; // Well-defined However, these are UBs: p = a+6; int b = *(a+5), diff = p-a; // Dereferencing and pointer arithmetic Now I have a question: Does this apply to dynamically allocated memory? Assume I'm only using a pointer pointing to one-past-the-last in pointer arithmetics, without

Allocating initialized, aligned memory

笑着哭i 提交于 2021-02-04 17:23:27
问题 I'm writing a program (in C++) in which I need to allocate arrays whose starting addresses should be aligned with the cache line size. When I allocate these arrays I also want the memory initialized to zero. Right now I have it working using the posix_memalign function. This works well for getting memory aligned arrays but the arrays are uninitilized. Is there a better function I can use to zero out the arrays when I initialize them or do I just have to settle for writing a separate loop to

Increase array size at runtime in c++ without vector, pointer [closed]

不问归期 提交于 2020-01-17 08:16:44
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 years ago . I have declared a array of int in c++ with some size. say, int a[6] at runtime if my array size exceeds 6, then i need to increase it. i am not going to use pointer, vector and the size will not be given by the user. 回答1: You can not change the size of your array at run time. An alternative is to

String Arrays in Ada

百般思念 提交于 2020-01-14 19:07:29
问题 I have a program in Ada95, in which I have to create an array of strings. This array can contain strings of variable length. Example: I have declared the array in which all the indexes can store strings of size 50. When I assign a smaller string to the above array, I get "Constraint Error". Code: procedure anyname is input_array : array(1..5) of String(1..50); begin input_array(1):="12345"; end anyname; I have tried to create the array of Unbounded_Strings. But that doesn't work either. Can

String Arrays in Ada

人盡茶涼 提交于 2020-01-14 19:07:10
问题 I have a program in Ada95, in which I have to create an array of strings. This array can contain strings of variable length. Example: I have declared the array in which all the indexes can store strings of size 50. When I assign a smaller string to the above array, I get "Constraint Error". Code: procedure anyname is input_array : array(1..5) of String(1..50); begin input_array(1):="12345"; end anyname; I have tried to create the array of Unbounded_Strings. But that doesn't work either. Can

memory allocation in Stack and Heap

℡╲_俬逩灬. 提交于 2020-01-10 07:58:14
问题 This may seem like a very basic question, but its been in my head so: When we allocate a local variable, it goes into stack. Similarly dynamic allocation cause the variable to go on heap. Now, my question is, is this variable actually lie on stack or heap or we will just a reference in the stack and Heap. For example, Suppose I declare a variable int i . Now this i is allocated on the stack. So, when I print the address of i , this will be one of the location on stack? Same question for heap