How can I know where the segment of memory is all Zero?


 
Thread Tools Search this Thread
Top Forums Programming How can I know where the segment of memory is all Zero?
# 1  
Old 12-09-2009
Java How can I know where the segment of memory is all Zero?

I mean, I malloc a segment of memory, maybe 1k maybe 20bytes.. assume the pointer is pMem How can I know the content pMem refered is all Zero or \0 . I know memcmp but the second parameter should another memory address... thanx
# 2  
Old 12-09-2009
Simple. memset(3) it to zero.
# 3  
Old 12-09-2009
Code:
size_t is_all_zero( void *location, size_t len)
{
     unsigned char *p=(unsigned char *)location;
     while( len && ! *p)
      {
          p++;
          len--;
      }
      return len; 
}

returns zero when len bytes at location are all zero.
# 4  
Old 12-09-2009
If you use calloc it will initialize all memory locations to zero before returning.
# 5  
Old 12-09-2009
Quote:
Originally Posted by macroideal
I mean, I malloc a segment of memory, maybe 1k maybe 20bytes.. assume the pointer is pMem How can I know the content pMem refered is all Zero or \0 . I know memcmp but the second parameter should another memory address... thanx
Never guess or hope. If you want it zeroed, zero it yourself(which is just as fast as checking it). Even if seems to always be zero right now, that may change when you start doing repeated malloc() and free() to the point you get bits of memory you've used before.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

why segment fault,

I always get segment fault, why? can sb help me and modify it, I have spend on much time on #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <string.h> #define MAX 10 pthread_t thread; void *thread1() { int *a; int i, n; ... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

2. Programming

Data segment or Text segment

Hi, Whether the following piece of code is placed in the read-only memory of code (text) segment or data segment? char *a = "Hello"; I am getting two different answers while searching in google :( that's why the confusion is (7 Replies)
Discussion started by: royalibrahim
7 Replies

3. Programming

Segment Violation

Hi to all. I'm reciving a "Segment violation" error from this code and I don't know why. void insertAtEnd(NodeType *pList) { char element; printf("Introduce a element: \n"); setbuf(stdin, NULL); scanf("%c", &element); //Find the end of the list; while... (4 Replies)
Discussion started by: daniel.gbaena
4 Replies

4. UNIX for Dummies Questions & Answers

code segment

how do i close a do code segment? od? (1 Reply)
Discussion started by: trob
1 Replies

5. Shell Programming and Scripting

Cutting segment of a string

Hi, I am using bash. My question concerns cutting out segments of a string. Given the following filename: S2002254132542.L1A_MLAC.x.hdf I have been able to successfully separate the string at the periods (.): $ L1A_FILE=S2002254132542.L1A_MLAC.x.hdf $ BASE=$(echo $L1A_FILE | awk -F.... (5 Replies)
Discussion started by: msb65
5 Replies

6. HP-UX

WAS 6.1 ND/Lawson ERP/HP-UX 11.23 PA-RISC - unable to attach shared memory segment

I'm not an HP-UX Admin professional, but rather a Lawson ERP installer. I'm looking for clues on how to troubleshoot this issue. I have WAS 6.1 ND running on HP-UX 11.23 PA-RISC with the Lawson ERP application . When I start the Lawson application, no errors arise. When I start WAS app server, an... (4 Replies)
Discussion started by: mrvitas
4 Replies

7. Shell Programming and Scripting

extract segment

Hey all, could someone please direct me on how to extract a segment from a file between two tags? Thanks! (1 Reply)
Discussion started by: mpang_
1 Replies

8. IP Networking

Network Access on Different Segment

I do have 2 different segment network which different platform on each segment. E.g. 20 segment, Windows OS can talk to Unix OS and 21 segment practice same rule. Further more, Windows OS from 20 segment can talk to 21 segment other OS but only the UNIX OS in 20 segment fail to talk to 21 segment.... (3 Replies)
Discussion started by: HASM
3 Replies

9. Programming

Segment Fault

When run it, segment fault. What is wrong? #include <stdio.h> #include <stdlib.h> const int max =20; //**************************************************** // Input Matrix //**************************************************** void inMatrixAA(int *AA, int row, int col)... (9 Replies)
Discussion started by: zhshqzyc
9 Replies
Login or Register to Ask a Question