malloc

Does memory allocated in a function still stay allocated after the function returns?

十年热恋 提交于 2021-02-06 02:07:02
问题 For the code below: (1) "main" calls a function "f1". (2) function "f1" does some number crunching; creates an array of "char" with malloc and then, returns the pointer of the array to the main (without de-allocating -freeing- the array). I have 3 questions related to the case: (1) I assume, although the function "f1" has terminated, the allocated char array still stays allocated until the main program terminates completely. That is, the allocated memory still belongs to the main and no other

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

Custom heap/memory allocation ranges

坚强是说给别人听的谎言 提交于 2021-02-05 12:21:04
问题 I am writing a 64-bit application in C (with GCC) and NASM under Linux. Is there a way to specify, where I want my heap and stack to be located. Specifically, I want all my malloc'ed data to be anywhere in range 0x00000000-0x7FFFFFFF. This can be done at either compile time, linking or runtime, via C code or otherwise. It doesn't matter. If this is not possible, please explain, why. P.S. For those interested, what the heck I am doing: The program I am working on is written in C. During

Size of dynamic array in C doesn't change

匆匆过客 提交于 2021-02-05 09:50:32
问题 I was getting realloc(): invalid next size for a program. So I just coded this to understand what's happening. #include <stdio.h> #include <stdlib.h> int main() { char *inp; printf("%lu ",sizeof(inp)); char *res = (char*)malloc(15*sizeof(char*)); printf("%lu ",sizeof(res)); res = "hello world"; printf("%lu\n",sizeof(res)); return 0; } And surprisingly it outputs 8 8 8 . Can anyone explain why is it like that? Why is it 8 by default? And how malloc() effects size of inp ? 回答1: You are using

how to define a 2d array using malloc and pass it to a function

[亡魂溺海] 提交于 2021-02-05 09:13:55
问题 I want to pass a 2d array i defined using malloc to a function. First i define the array using code from a blog post. int** Make2DIntArray(int arraySizeX, int arraySizeY) { int** theArray; theArray = (int**) malloc(arraySizeX*sizeof(int*)); for (int i = 0; i < arraySizeX; i++) theArray[i] = (int*) malloc(arraySizeY*sizeof(int)); return theArray; } int main(){ int** myArray = Make2DIntArray(nx, ny); } I can then use it as myArray[i][j]. After that,i want to pass this array to a function.I

aligned_malloc() vs alignas() for Constant Buffers

懵懂的女人 提交于 2021-02-05 08:59:06
问题 In C++, we have the keyword alignas(n) and we have the _aligned_malloc(m,n) function. alignas works on the type while aligned_malloc works on whatever you call it. Can I use alignas(16) to fullfil the 16-byte alignment requirement for Direct3D Constant Buffers? 回答1: Yes, you could use it like this: struct SceneConstantBuffer { alignas(16) DirectX::XMFLOAT4X4 ViewProjection[2]; alignas(16) DirectX::XMFLOAT4 EyePosition[2]; alignas(16) DirectX::XMFLOAT3 LightDirection{}; alignas(16) DirectX:

C Passing Pointer to Pointer to a Function and Using malloc

两盒软妹~` 提交于 2021-02-05 08:32:16
问题 I'm trying to get std input to scan in two 2d parallel arrays (arrAtk, arrDef) of x rows (x<100), and y columns (y<1,000,000). But y is a variable length in each row. The first line of input is x for the number of rows in each array. the second line is y for the number of columns in the first row. Following that is y integers to be read into the arrAtk array. Then another y integers to be read into the arrDef array. Directly following is an int y for the number of columns in the next two rows

How to malloc in address range > 4 GiB

偶尔善良 提交于 2021-02-05 07:24:08
问题 I am trying to do some tests on X86_64 Linux, and I want to know how to make malloc() allocate return addresses in the range higher than 4 GiB Do I have to override malloc with a separate/custom library or is there any other simple way to do this? Thanks. ----Edit---- What I am interested is in the address when taken as value ( uintptr_t ) and it does not matter whether its virtual or physical address, because, all that I want is the address must be a value greater than 4GiB I am using gcc (4