10-19-2009
broli,
C does not hide memory pointer information.
"asdf" is a "string literal."
when initializing a char*, "asdf" is seen by the program as a pointer to that location in memory.
The same is not true for other basic types.
int *Bogus = { 0, 1, 2, 3, 4}; - does not work
int Correct[5] = { 0, 1, 2, 3, 4}; does work
malloc() allocates memory on a global heap, and requires a type cast.
using [] allocates memory locally, and the size of this chunk is limited, so don't use brackets for really big arrays.
Two operators will come in handy all the time... "*" and "&"
10 More Discussions You Might Find Interesting
1. Programming
void main()
{
int a={1,2,3,4,5,6,7,8,9,10};
int *p=a;
int *q=&a;
cout<<q-p+1<<endl;
}
The output is 10, how?
if we give cout<<q it will print the address, value won't print....
if we give cout<<p it will print the address, value won't print....
p has the base addr; q... (1 Reply)
Discussion started by: sarwan
1 Replies
2. Programming
what is far pointer in C (1 Reply)
Discussion started by: useless79
1 Replies
3. What is on Your Mind?
All Sys Administrators,
With due respect I would like to know what should be BEST Things to
do when LEAVING one job , and what Precaution MUST be taken
while taking over new JOB?? Please Discuss in detail the STEP to be
taken for both the TIME ?? (3 Replies)
Discussion started by: vakharia Mahesh
3 Replies
4. Programming
If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it:
(1) one is to pass a pointer-to-pointer parameter, like:
int my_malloc(int size, char **pmem)
{
*pmem=(char *)malloc(size);
if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies
5. Homework & Coursework Questions
1. List commands
to create the directory hierarchy $HOME/a/b/c
in vi to replace all occurences of TMP with tmp in lines 1 through 10
in vi to replace first occurence of CPU_file with DISK_file at line 15
2. Explain with a very simple example, usage of "ls -a"
3. What do the... (2 Replies)
Discussion started by: jessesaini
2 Replies
6. Shell Programming and Scripting
Q-1
Write a shell script in Unix that lists files from your current working directory
· By modification time when called with lm
· By access time when called with la.
By default, the script should show the listing of all the files in the current directory.
Q-2
Write a... (1 Reply)
Discussion started by: Vishank Parikh
1 Replies
7. Homework & Coursework Questions
Q-1
Write a shell script in Unix that lists files from your current working directory
· By modification time when called with lm
· By access time when called with la.
By default, the script should show the listing of all the files in the current directory.
Q-2
Write a shell script which... (1 Reply)
Discussion started by: Vishank Parikh
1 Replies
8. Programming
When I compile the below code, I am getting error as
template<typename T> T AddFun(T i, T j) {
return i + j;
}
template<> T* AddFun<T*>(T* i, T* j) {
return new T(*i + *j);
}
int main() {
int n = AddFun<int>(10, 20);
int i = 10, j = 20;
int* p = AddFun<int*>(&i,... (1 Reply)
Discussion started by: royalibrahim
1 Replies
9. Homework & Coursework Questions
Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
1. The problem statement, all variables and given/known data:
In the command line I will be given files or words which will be USER ID's or a file of USER ID's. I am to let... (4 Replies)
Discussion started by: ByronicX
4 Replies
10. UNIX for Dummies Questions & Answers
Hello all im currently working on this assignment and a little stump on how to check for an argument heres the instructions:
Step 4:
Modify your script so that if there is an argument called TestError you display the following error message with your usage statement.
TestError found
Example:... (1 Reply)
Discussion started by: bsn3971
1 Replies
LEARN ABOUT REDHAT
memalign
POSIX_MEMALIGN(3) Linux Programmer's Manual POSIX_MEMALIGN(3)
NAME
posix_memalign, memalign, valloc - Allocate aligned memory
SYNOPSIS
#include <stdlib.h>
int posix_memalign(void **memptr, size_t alignment, size_t size);
void *memalign(size_t boundary, size_t size);
void *valloc(size_t size);
DESCRIPTION
The function posix_memalign() allocates size bytes and places the address of the allocated memory in *memptr. The address of the allocated
memory will be a multiple of alignment, which must be a power of two and a multiple of sizeof(void *).
The obsolete function memalign() allocates size bytes and returns a pointer to the allocated memory. The memory address will be a multiple
of boundary, which must be a power of two.
The obsolete function valloc() allocates size bytes and returns a pointer to the allocated memory. The memory address will be a multiple
of the page size. It is equivalent to memalign(sysconf(_SC_PAGESIZE),size).
For all three routines, the memory is not zeroed.
RETURN VALUE
memalign() and valloc() return the pointer to the allocated memory, or NULL if the request fails.
posix_memalign() returns zero on success, or one of the error values listed in the next section on failure. Note that errno is not set.
ERRORS
EINVAL The alignment parameter was not a power of two, or was not a multiple of sizeof(void *).
ENOMEM There was insufficient memory to fulfill the allocation request.
NOTES
posix_memalign() verifies that alignment matches the requirements detailed above. memalign() may not check that the boundary parameter is
correct.
POSIX requires that memory obtained from posix_memalign() can be freed using free(). Some systems provide no way to reclaim memory allo-
cated with memalign() or valloc() (because one can only pass to free() a pointer gotten from malloc(), while e.g. memalign() would call
malloc() and then align the obtained value). GNU libc allows memory obtained from any of these three routines to be reclaimed with free().
GNU libc malloc() always returns 8-byte aligned memory addresses, so these routines are only needed if you require larger alignment values.
AVAILABILITY
The functions memalign() and valloc() have been available in all Linux libc libraries. The function posix_memalign() is available since
glibc 2.1.91.
CONFORMING TO
The function valloc() appeared in 3.0 BSD. It is documented as being obsolete in BSD 4.3, and as legacy in SUSv2. It no longer occurs in
SUSv3. The function memalign() appears in SunOS 4.1.3 but not in BSD 4.4. The function posix_memalign() comes from POSIX 1003.1d.
SEE ALSO
malloc(3), free(3), getpagesize(2), brk(2)
GNU
2001-10-11 POSIX_MEMALIGN(3)