allocation

How is Memory Allocated to variables of different data types?

≯℡__Kan透↙ 提交于 2019-11-28 06:58:40
问题 I wrote the following Code. #include<stdio.h> int main() { int x = 1 ; int *j = &x ; int y = 2 ; int *t = &y ; printf("%p\n" , (void *)j); printf("%p" , (void *)t); } Output is 0028FF14 0028FF10 . The Point I want to make is that the difference between the addresses is `4'. Whereas in this case #include<stdio.h> int main() { char x = 't' ; char *j = &x ; char y = 'f' ; char *t = &y ; printf("%p\n" , (void *)j); printf("%p" , (void *)t); } Output is 0028FF17 0028FF16 The difference is 1 .

C++ Multi-dimensional Arrays on the Heap

江枫思渺然 提交于 2019-11-28 05:54:29
How would I go about dynamically allocating a multi-dimensional array? If you know the size of nested dimensions already, you can also literally allocate a multi dimensional array using new: typedef int dimensions[3][4]; dimensions * dim = new dimensions[10]; dim[/* from 0 to 9 */][/* from 0 to 2 */][/* from 0 to 3 */] = 42; delete [] dim; instead of 10 , a runtime determined value can be passed. Since it's not part of the type operator new returns, that's allowed. This is nice if you know the number of columns, but want to keep the number of rows variable, for example. The typedef makes it

Correct way to cap Mathematica memory use?

≡放荡痞女 提交于 2019-11-28 04:33:51
Under a 32-bit operating system, where maximum memory allocated to any one program is limited, Mathematica gracefully terminates the kernel and returns a max memory allocation error. On a 64-bit OS however, Mathematica will freely use all the memory available and grind the system to a halt. Therefore, what is the correct way to cap memory usage? One could use MemoryConstrained combined with $Pre or CellEvaluationFunction but I would rather not tie up either of those for this purpose, or have to modify existing uses to incorporate this function. Is there another way to globally restrict memory

Uninitialized memory blocks in VC++

房东的猫 提交于 2019-11-28 04:25:43
问题 As everyone knows, the Visual C++ runtime marks uninitialized or just freed memory blocks with special non-zero markers. Is there any way to disable this behavior entirely without manually setting all uninitialized memory to zeros? It's causing havoc with my valid not null checks, since 0xFEEEFEEE != 0 . Hrm, perhaps I should explain a bit better. I create and initialize a variable (via new), and that all goes just fine. When I free it (via delete), it sets the pointer to 0xFEEEFEEE instead

Freaky way of allocating two-dimensional array?

萝らか妹 提交于 2019-11-28 02:50:37
In a project, somebody pushed this line: double (*e)[n+1] = malloc((n+1) * sizeof(*e)); Which supposedly creates a two-dimensional array of (n+1)*(n+1) doubles. Supposedly , I say, because so far, nobody I asked could tell me what this does, exactly, nor where it originated from or why it should work (which allegedly, it does, but I'm not yet buying it). Perhaps I'm missing something obvious, but I'd appreciate it if somebody could explain above line to me. Because personally, I'd feel much better if we'd use something we actually understand. The variable e is a pointer to an array of n + 1

malloc(0) actually works? [duplicate]

南笙酒味 提交于 2019-11-28 02:29:15
Possible Duplicate: what’s the point in malloc(0)? Why does malloc(0) actually return a valid pointer for writing ? char *str = NULL; str = (char*)malloc(0); // allocate 0 bytes ? printf("Pointer of str: %p\n", str); strcpy(str, "A very long string ..................."); printf("Value of str: %s", str); free(str); // Causes crash if str is too long Output: Pointer of str: 0xa9d010 Aborted Value of str: A very long string ................... When str is shorter then it just works as it should. BTW: For compiling I used GCC with "-D_FORTIY_SOURCE=0 -fno-stack-protector" *** glibc detected *** ..

What is a NSPathStore2? [closed]

▼魔方 西西 提交于 2019-11-28 02:26:47
问题 All that I know is this: Its private Its created somehow cause of strings trying to do Path related things I mean, if they are private and still my app is telling me that NSPathStore2 is interfering, I need to know why . I just want to understand why a release of NSPathStore2 is making my app crash. 回答1: NSPathStore2 is a subclass of NSString , used when creating strings that contain paths. For all intents and purposes, you should treat it as a NSString . 来源: https://stackoverflow.com

Dynamic Allocation of two-dimensional array C++

给你一囗甜甜゛ 提交于 2019-11-28 02:25:32
Hi I'm pretty new to C++ and I need to dynamicacally allocate two-dimensional array. No error but in runtime when I set a order and first row then I get a runtime error: "Segmentation Fault"...Here's code: #include <iostream> using namespace std; double ** allocateDynamicArray(int &order){ double ** arr = new double *[order]; for(int i = 0;i < order; i++){ *arr = new double[order+1]; } return arr; } void deallocateDynamicArray(double **arr, int order){ for(int i=0; i<order; i++){ delete [] arr[i]; } delete [] arr; } void addAndPrintArray(double **arr, int order){ cout << "Zadejte prvky pole

a library forces global overloads of new/delete on me!

北战南征 提交于 2019-11-28 01:15:45
问题 I'm maintaining a plugin (implemented as a dll) for a big closed source application. This has been working fine for years. However, with the latest update to it's SDK the vendor overloaded global operators new and delete. This causes lots of trouble for me. What happens is that my plugin allocates a string. I pass this string into a statically linked library which modifies it (changes it's length thus reallocating it). My application crashes. The reason is of course, that the string lives on

Why isn't my new operator called

折月煮酒 提交于 2019-11-27 18:17:28
问题 I wanted to see that a dynamically loaded library (loaded with dlopen etc.) really uses its own new an delete operators and not these ones defined in the calling program. So I wrote the following library.cpp #include <exception> #include <new> #include <cstdlib> #include <cstdio> #include "base.hpp" void* operator new(size_t size) { std::printf("New of library called\n"); void *p=std::malloc(size); if (p == 0) // did malloc succeed? throw std::bad_alloc(); // ANSI/ISO compliant behavior