allocation

How is Memory Allocated to variables of different data types?

六眼飞鱼酱① 提交于 2019-11-29 13:06:10
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 . Difference In First Case is 4 . Whereas in the second case it is 1 . Why is it so? And What will I get if I

Dynamic allocation of 2D array within function (using pointers to return adress of allocated object)

岁酱吖の 提交于 2019-11-29 12:58:57
I'd /ike to know, how to pass pointers to dynamically allocated arrays using function arguments. This function is supposed to allocate array 10x10 (checks skipped for simplicity sake). Is this possible? What am i doing wrong? Thanks in advance. int array_allocate2DArray ( int **array, unsigned int size_x, unsigned int size_y) { array = malloc (size_x * sizeof(int *)); for (int i = 0; i < size_x; i++) array[i] = malloc(size_y * sizeof(int)); return 0; } int main() { int **array; array_allocate2DArray (*&array, 10, 10); } Try something like this: int array_allocate2DArray (int ***p, unsigned int

Uninitialized memory blocks in VC++

我怕爱的太早我们不能终老 提交于 2019-11-29 10:57:48
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 of NULL . When I insert a proper check for NULL , as all good programs that manage their own memory

fortran, passing allocatable arrays to a subroutine with right bounds

妖精的绣舞 提交于 2019-11-29 10:09:27
问题 I need in a program to pass some allocatable arrays to subroutines, and i need to know if the way I do it are in the standard or not. If you know where I can search for the standard of fortran, Tell me please. Here is a little code that will better explain than words program test use modt99 implicit none real(pr), dimension(:), allocatable :: vx allocate(vx(-1:6)) vx=(/666,214,558,332,-521,-999,120,55/) call test3(vx,vx,vx) deallocate(vx) end program test with the module modt99 module modt99

Why is there a stack and a heap?

喜夏-厌秋 提交于 2019-11-29 07:42:57
问题 Why do assembly languages use both a stack and a heap? They seem redundant. 回答1: They're not redundant. Each of them has strengths and weaknesses: A stack is faster if used right, because memory allocation is trivial (push / pop). The downside is that you can only add and remove items at the top (hence the name, stack). Also, total stack space is limited, and when you run out, you have a... well, stack overflow. The heap, by contrast, allows random allocation and deallocation, and you can

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

微笑、不失礼 提交于 2019-11-29 07:41:32
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 the vendor allocated custom heap. The statically linked library knows nothing about this heap and tries

Why isn't my new operator called

你。 提交于 2019-11-29 04:19:49
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 return p; } void operator delete(void* p) { std::printf("Delete of library called\n"); std::free(p); } class

How to implement a memory heap

十年热恋 提交于 2019-11-29 00:16:23
问题 Wasn't exactly sure how to phrase the title, but the question is: I've heard of programmers allocating a large section of contiguous memory at the start of a program and then dealing it out as necessary. This is in contrast to simply going to the OS every time memory is needed. I've heard that this would be faster because it would avoid the cost of asking the OS for contiguous blocks of memory constantly. I believe the JVM does just this, maintaining its own section of memory and then

In what cases should I use memcpy over standard operators in C++?

☆樱花仙子☆ 提交于 2019-11-28 18:14:49
When can I get better performance using memcpy or how do I benefit from using it? For example: float a[3]; float b[3]; is code: memcpy(a, b, 3*sizeof(float)); faster than this one? a[0] = b[0]; a[1] = b[1]; a[2] = b[2]; Efficiency should not be your concern. Write clean maintainable code. It bothers me that so many answers indicate that the memcpy() is inefficient. It is designed to be the most efficient way of copy blocks of memory (for C programs). So I wrote the following as a test: #include <algorithm> extern float a[3]; extern float b[3]; extern void base(); int main() { base(); #if

Sourced allocation of a non-contiguous array section

余生颓废 提交于 2019-11-28 11:08:33
问题 In relation to a recent post regarding how to declare the array shape concisely, I tried the following three patterns, i.e., (A) automatic re-allocation, (B) sourced allocation, and (C) allocation with assumed shape. Then gfortran seems to give incorrect results for b(:,:) in the case of sourced allocation. Here, am I doing something wrong, or is this simply because it is not yet fully supported by gfortran? Although the latter seems likely, I am not very sure if my installation or usage of