Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, your code is neither valid C nor valid C++.</p> <p>Perhaps you mean:</p> <pre><code> char str* = new char[512]; delete[] str; </code></pre> <p>which is valid C++ (not C, because <code>new</code> and <code>delete</code> are C++ keywords, not C ones) and works on Linux as it does on Windows or any standard C++ implementation.</p> <p>Then, the <code>str</code> is allocated in the <a href="http://en.wikipedia.org/wiki/Memory_management" rel="nofollow">heap</a> (and the same program is allocating in the heap both on Windows and on Linux).</p> <p>There is no need for <a href="http://man7.org/linux/man-pages/man3/posix_memalign.3.html" rel="nofollow">posix_memalign(3)</a> (please look at the man page I just linked) in your case.</p> <p>You could want to use <code>posix_memalign</code> if for some particular reason you wanted the pointer to be aligned, e.g. be a multiple of 1024. </p> <p><sup>This example is silly on purpose, because it asks an alignment of 1024 bytes for a memory zone of 512 bytes.</sup></p> <p>Then please have</p> <pre><code> #define _GNU_SOURCE #include &lt;new&gt; #include &lt;stdlib.h&gt; </code></pre> <p>and later in the same C++ source file:</p> <pre><code> char* str = NULL; void* ad = NULL; if (posix_memalign(&amp;ad, 1024, 512)) { perror("posix_memalign failed"); exit (EXIT_FAILURE); } str = new(ad) char[512]; </code></pre> <p>but you should have some particular reason to want an aligned pointer (here, a multiple of 1Kbyte). Usually you don't want pointer to be aligned more than the default. (You may require a large alignment, e.g. if doing arithmetic on <code>(intptr_t)</code> casts of your pointers; but this is very unusual). Notice the <a href="http://en.wikipedia.org/wiki/Placement_new" rel="nofollow">placement new</a> (provided by the <code>&lt;new&gt;</code> standard header).</p> <p>I recommend reading <a href="http://advancedlinuxprogramming.com/" rel="nofollow">Advanced Linux Programming</a> (which is more focused on C than on C++). And always compile on Linux with all warnings requested by the compiler and debugging info (e.g. <code>g++ -Wall -g</code>). Learn to use the <code>gdb</code> debugger and the <code>valgrind</code> memory leak detector. Once your program has no bugs, consider asking the compiler to optimize its produced object code with <code>g++ -Wall -O2</code> instead of <code>g++ -Wall -g</code>.</p> <p>actually, the real <a href="http://en.wikipedia.org/wiki/Syscall" rel="nofollow">syscalls</a> done by your application to the <a href="http://en.wikipedia.org/wiki/Linux_kernel" rel="nofollow">Linux kernel</a> (whose list is on <a href="http://man7.org/linux/man-pages/man2/syscalls.2.html" rel="nofollow">syscalls(2)</a>) for memory management are <a href="http://man7.org/linux/man-pages/man2/mmap.2.html" rel="nofollow">mmap(2)</a> and <code>munmap(2)</code> (and perhaps <code>sbrk(2)</code> which is nearly obsolete). You should use the <code>strace</code> command to find out the many syscalls done by a process.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload