diff --git a/apps/app_adsiprog.c b/apps/app_adsiprog.c
index 264bab0e78d303db226da8ffeb5cb8d62e98251c..63be6bf06a2cc9899c8a49d7c82d500d0c7c02e8 100644
--- a/apps/app_adsiprog.c
+++ b/apps/app_adsiprog.c
@@ -515,7 +515,7 @@ static int geteventbyname(char *name)
 {
 	int x;
 
-	for (x = 0; x < sizeof(events) / sizeof(events[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(events); x++) {
 		if (!strcasecmp(events[x].name, name))
 			return events[x].id;
 	}
@@ -527,7 +527,7 @@ static int getjustifybyname(char *name)
 {
 	int x;
 
-	for (x = 0; x <sizeof(justify) / sizeof(justify[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(justify); x++) {
 		if (!strcasecmp(justify[x].name, name))
 			return justify[x].id;
 	}
@@ -935,7 +935,7 @@ static int process_returncode(struct adsi_soft_key *key, char *code, char *args,
 	int x, res;
 	char *unused;
 
-	for (x = 0; x < sizeof(kcmds) / sizeof(kcmds[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(kcmds); x++) {
 		if ((kcmds[x].id > -1) && !strcasecmp(kcmds[x].name, code)) {
 			if (kcmds[x].add_args) {
 				res = kcmds[x].add_args(key->retstr + key->retstrlen,
@@ -964,7 +964,7 @@ static int process_opcode(struct adsi_subscript *sub, char *code, char *args, st
 	int x, res, max = sub->id ? MAX_SUB_LEN : MAX_MAIN_LEN;
 	char *unused;
 
-	for (x = 0; x < sizeof(opcmds) / sizeof(opcmds[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(opcmds); x++) {
 		if ((opcmds[x].id > -1) && !strcasecmp(opcmds[x].name, code)) {
 			if (opcmds[x].add_args) {
 				res = opcmds[x].add_args(sub->data + sub->datalen,
diff --git a/apps/app_meetme.c b/apps/app_meetme.c
index 25cc0727ed408914902ea963fe09c4fc04222bd9..ee158b1fe85b856d622b7b89053dc5b3fb8ed332 100644
--- a/apps/app_meetme.c
+++ b/apps/app_meetme.c
@@ -3101,7 +3101,7 @@ static int conf_exec(struct ast_channel *chan, void *data)
 			/* Select first conference number not in use */
 			if (ast_strlen_zero(confno) && dynamic) {
 				AST_LIST_LOCK(&confs);
-				for (i = 0; i < sizeof(conf_map) / sizeof(conf_map[0]); i++) {
+				for (i = 0; i < ARRAY_LEN(conf_map); i++) {
 					if (!conf_map[i]) {
 						snprintf(confno, sizeof(confno), "%d", i);
 						conf_map[i] = 1;
diff --git a/apps/app_parkandannounce.c b/apps/app_parkandannounce.c
index ccd3b36e84f7ffe6b04c1d3f3e831f6c82192f50..b0e95dafa329ce4fa4832297afa365a621f29133 100644
--- a/apps/app_parkandannounce.c
+++ b/apps/app_parkandannounce.c
@@ -146,7 +146,7 @@ static int parkandannounce_exec(struct ast_channel *chan, void *data)
 
 	ast_verb(4, "Announce Template:%s\n", args.template);
 
-	for (looptemp = 0; looptemp < sizeof(tmp) / sizeof(tmp[0]); looptemp++) {
+	for (looptemp = 0; looptemp < ARRAY_LEN(tmp); looptemp++) {
 		if ((tmp[looptemp] = strsep(&args.template, ":")) != NULL)
 			continue;
 		else
diff --git a/channels/chan_dahdi.c b/channels/chan_dahdi.c
index 3d8c05e7bbe3b8552a8f5b4539038458dad233cf..34162ea5f13347f355e26b117d6266d680414c8e 100644
--- a/channels/chan_dahdi.c
+++ b/channels/chan_dahdi.c
@@ -1344,7 +1344,7 @@ static struct {
 static char *alarm2str(int alarm)
 {
 	int x;
-	for (x = 0; x < sizeof(alarms) / sizeof(alarms[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(alarms); x++) {
 		if (alarms[x].alarm & alarm)
 			return alarms[x].name;
 	}
@@ -1697,7 +1697,7 @@ static void fill_txgain(struct dahdi_gains *g, float gain, int law)
 
 	switch (law) {
 	case DAHDI_LAW_ALAW:
-		for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
+		for (j = 0; j < ARRAY_LEN(g->txgain); j++) {
 			if (gain) {
 				k = (int) (((float) AST_ALAW(j)) * linear_gain);
 				if (k > 32767) k = 32767;
@@ -1709,7 +1709,7 @@ static void fill_txgain(struct dahdi_gains *g, float gain, int law)
 		}
 		break;
 	case DAHDI_LAW_MULAW:
-		for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
+		for (j = 0; j < ARRAY_LEN(g->txgain); j++) {
 			if (gain) {
 				k = (int) (((float) AST_MULAW(j)) * linear_gain);
 				if (k > 32767) k = 32767;
@@ -1731,7 +1731,7 @@ static void fill_rxgain(struct dahdi_gains *g, float gain, int law)
 
 	switch (law) {
 	case DAHDI_LAW_ALAW:
-		for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
+		for (j = 0; j < ARRAY_LEN(g->rxgain); j++) {
 			if (gain) {
 				k = (int) (((float) AST_ALAW(j)) * linear_gain);
 				if (k > 32767) k = 32767;
@@ -1743,7 +1743,7 @@ static void fill_rxgain(struct dahdi_gains *g, float gain, int law)
 		}
 		break;
 	case DAHDI_LAW_MULAW:
-		for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
+		for (j = 0; j < ARRAY_LEN(g->rxgain); j++) {
 			if (gain) {
 				k = (int) (((float) AST_MULAW(j)) * linear_gain);
 				if (k > 32767) k = 32767;
@@ -6891,7 +6891,7 @@ static void *ss_thread(void *data)
 						len = 0;
 						distMatches = 0;
 						/* Clear the current ring data array so we dont have old data in it. */
-						for (receivedRingT = 0; receivedRingT < (sizeof(curRingData) / sizeof(curRingData[0])); receivedRingT++)
+						for (receivedRingT = 0; receivedRingT < ARRAY_LEN(curRingData); receivedRingT++)
 							curRingData[receivedRingT] = 0;
 						receivedRingT = 0;
 						counter = 0;
@@ -7040,7 +7040,7 @@ static void *ss_thread(void *data)
 				len = 0;
 				distMatches = 0;
 				/* Clear the current ring data array so we dont have old data in it. */
-				for (receivedRingT = 0; receivedRingT < (sizeof(curRingData) / sizeof(curRingData[0])); receivedRingT++)
+				for (receivedRingT = 0; receivedRingT < ARRAY_LEN(curRingData); receivedRingT++)
 					curRingData[receivedRingT] = 0;
 				receivedRingT = 0;
 				counter = 0;
diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c
index 1a749b644b08bc55ebeb3d4283cbc9aab4d9f2c5..e733ddce96732b9268efb7f80ac5054912bd2ec8 100644
--- a/channels/chan_iax2.c
+++ b/channels/chan_iax2.c
@@ -2788,23 +2788,27 @@ static char *handle_cli_iax2_show_cache(struct ast_cli_entry *e, int cmd, struct
 		if (dp->flags & CACHE_FLAG_UNKNOWN)
 			strncat(tmp, "UNKNOWN|", sizeof(tmp) - strlen(tmp) - 1);
 		/* Trim trailing pipe */
-		if (!ast_strlen_zero(tmp))
+		if (!ast_strlen_zero(tmp)) {
 			tmp[strlen(tmp) - 1] = '\0';
-		else
+		} else {
 			ast_copy_string(tmp, "(none)", sizeof(tmp));
+		}
 		y = 0;
 		pc = strchr(dp->peercontext, '@');
-		if (!pc)
+		if (!pc) {
 			pc = dp->peercontext;
-		else
+		} else {
 			pc++;
-		for (x = 0; x < sizeof(dp->waiters) / sizeof(dp->waiters[0]); x++)
+		}
+		for (x = 0; x < ARRAY_LEN(dp->waiters); x++) {
 			if (dp->waiters[x] > -1)
 				y++;
-		if (s > 0)
+		}
+		if (s > 0) {
 			ast_cli(a->fd, "%-20.20s %-12.12s %-9d %-8d %s\n", pc, dp->exten, s, y, tmp);
-		else
+		} else {
 			ast_cli(a->fd, "%-20.20s %-12.12s %-9.9s %-8d %s\n", pc, dp->exten, "(expired)", y, tmp);
+		}
 	}
 
 	AST_LIST_LOCK(&dpcache);
@@ -6527,9 +6531,10 @@ static int complete_dpreply(struct chan_iax2_pvt *pvt, struct iax_ies *ies)
 			dp->flags |= matchmore;
 		}
 		/* Wake up waiters */
-		for (x=0;x<sizeof(dp->waiters) / sizeof(dp->waiters[0]); x++)
+		for (x = 0; x < ARRAY_LEN(dp->waiters); x++) {
 			if (dp->waiters[x] > -1)
 				write(dp->waiters[x], "asdf", 4);
+		}
 	}
 	AST_LIST_TRAVERSE_SAFE_END;
 	AST_LIST_UNLOCK(&dpcache);
@@ -11440,7 +11445,7 @@ static struct iax2_dpcache *find_cache(struct ast_channel *chan, const char *dat
 		/* Expires in 30 mins by default */
 		dp->expiry.tv_sec += iaxdefaultdpcache;
 		dp->flags = CACHE_FLAG_PENDING;
-		for (x=0;x<sizeof(dp->waiters) / sizeof(dp->waiters[0]); x++)
+		for (x = 0; x < ARRAY_LEN(dp->waiters); x++)
 			dp->waiters[x] = -1;
 		/* Insert into the lists */
 		AST_LIST_INSERT_TAIL(&dpcache, dp, cache_list);
@@ -11455,12 +11460,12 @@ static struct iax2_dpcache *find_cache(struct ast_channel *chan, const char *dat
 	if (dp->flags & CACHE_FLAG_PENDING) {
 		/* Okay, here it starts to get nasty.  We need a pipe now to wait
 		   for a reply to come back so long as it's pending */
-		for (x=0;x<sizeof(dp->waiters) / sizeof(dp->waiters[0]); x++) {
+		for (x = 0; x < ARRAY_LEN(dp->waiters); x++) {
 			/* Find an empty slot */
 			if (dp->waiters[x] < 0)
 				break;
 		}
-		if (x >= sizeof(dp->waiters) / sizeof(dp->waiters[0])) {
+		if (x >= ARRAY_LEN(dp->waiters)) {
 			ast_log(LOG_WARNING, "No more waiter positions available\n");
 			return NULL;
 		}
@@ -11513,9 +11518,10 @@ static struct iax2_dpcache *find_cache(struct ast_channel *chan, const char *dat
 				/* Expire after only 60 seconds now.  This is designed to help reduce backlog in heavily loaded
 				   systems without leaving it unavailable once the server comes back online */
 				dp->expiry.tv_sec = dp->orig.tv_sec + 60;
-				for (x=0;x<sizeof(dp->waiters) / sizeof(dp->waiters[0]); x++)
+				for (x = 0; x < ARRAY_LEN(dp->waiters); x++) {
 					if (dp->waiters[x] > -1)
 						write(dp->waiters[x], "asdf", 4);
+				}
 			}
 		}
 		/* Our caller will obtain the rest */
diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index 4f71133d22c56a0b1557661f152f891bd72909db..bdaefc0f2fc808173983c43e4b9d5b5e016a83c9 100644
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -2630,7 +2630,7 @@ static unsigned int parse_sip_options(struct sip_pvt *pvt, const char *supported
 		next = ast_skip_blanks(next);
 		if (sipdebug)
 			ast_debug(3, "Found SIP option: -%s-\n", next);
-		for (i=0; i < (sizeof(sip_options) / sizeof(sip_options[0])); i++) {
+		for (i = 0; i < ARRAY_LEN(sip_options); i++) {
 			if (!strcasecmp(next, sip_options[i].text)) {
 				profile |= sip_options[i].id;
 				found = TRUE;
@@ -5705,9 +5705,10 @@ static const char *find_alias(const char *name, const char *_default)
 	};
 	int x;
 
-	for (x=0; x<sizeof(aliases) / sizeof(aliases[0]); x++) 
+	for (x = 0; x < ARRAY_LEN(aliases); x++) {
 		if (!strcasecmp(aliases[x].fullname, name))
 			return aliases[x].shortname;
+	}
 
 	return _default;
 }
@@ -13104,7 +13105,7 @@ static char *_sip_show_peer(int type, int fd, struct mansession *s, const struct
 		ast_cli(fd, "  SIP Options  : ");
 		if (peer->sipoptions) {
 			int lastoption = -1;
-			for (x=0 ; (x < (sizeof(sip_options) / sizeof(sip_options[0]))); x++) {
+			for (x = 0 ; x < ARRAY_LEN(sip_options); x++) {
 				if (sip_options[x].id != lastoption) {
 					if (peer->sipoptions & sip_options[x].id)
 						ast_cli(fd, "%s ", sip_options[x].text);
@@ -13965,7 +13966,7 @@ static char *sip_show_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_a
 			ast_cli(a->fd, "  SIP Options:            ");
 			if (cur->sipoptions) {
 				int x;
-				for (x=0 ; (x < (sizeof(sip_options) / sizeof(sip_options[0]))); x++) {
+				for (x = 0 ; x < ARRAY_LEN(sip_options); x++) {
 					if (cur->sipoptions & sip_options[x].id)
 						ast_cli(a->fd, "%s ", sip_options[x].text);
 				}
diff --git a/channels/h323/ast_h323.cxx b/channels/h323/ast_h323.cxx
index 48b876aab3f827e51baa5e5f3bc2e8a52eb3212c..a55a8957422bb3bdc11f152cbd09376733061ce2 100644
--- a/channels/h323/ast_h323.cxx
+++ b/channels/h323/ast_h323.cxx
@@ -970,7 +970,7 @@ static BOOL EmbedCiscoTunneledInfo(H323SignalPDU &pdu)
 	Q931 tmpQ931;
 	Q931 &q931 = pdu.GetQ931();
 
-	for(unsigned i = 0; i < (sizeof(codes) / sizeof(codes[0])); ++i) {
+	for(unsigned i = 0; i < ARRAY_LEN(codes); ++i) {
 		if (q931.HasIE(codes[i].ie)) {
 			tmpQ931.SetIE(codes[i].ie, q931.GetIE(codes[i].ie));
 			if (!codes[i].dontDelete)
@@ -1104,7 +1104,7 @@ static BOOL EmbedQSIGTunneledInfo(H323SignalPDU &pdu)
 	q931.Encode(message);
 
 	/* Remove non-standard IEs */
-	for(unsigned i = 0; i < (sizeof(codes) / sizeof(codes[0])); ++i) {
+	for(unsigned i = 0; i < ARRAY_LEN(codes); ++i) {
 		if (q931.HasIE(codes[i])) {
 			q931.RemoveIE(codes[i]);
 		}
diff --git a/channels/iax2-provision.c b/channels/iax2-provision.c
index 4a5281c05e10868c22c6b2e2444fc729d5adbcd9..8bdfdbfb72b23f4a321c64ae47fd3908e8898345 100644
--- a/channels/iax2-provision.c
+++ b/channels/iax2-provision.c
@@ -87,7 +87,7 @@ char *iax_provflags2str(char *buf, int buflen, unsigned int flags)
 	
 	buf[0] = '\0';
 
-	for (x = 0; x < sizeof(iax_flags) / sizeof(iax_flags[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(iax_flags); x++) {
 		if (flags & iax_flags[x].value){
 			strncat(buf, iax_flags[x].name, buflen - strlen(buf) - 1);
 			strncat(buf, ",", buflen - strlen(buf) - 1);
@@ -116,7 +116,7 @@ static unsigned int iax_str2flags(const char *buf)
 		else
 			len = 0;
 		found = 0;
-		for (x=0;x<sizeof(iax_flags) / sizeof(iax_flags[0]); x++) {
+		for (x = 0; x < ARRAY_LEN(iax_flags); x++) {
 			if ((len && !strncasecmp(iax_flags[x].name, buf, len)) ||
 			    (!len && !strcasecmp(iax_flags[x].name, buf))) {
 				flags |= iax_flags[x].value;
diff --git a/formats/format_pcm.c b/formats/format_pcm.c
index 3ecc1c16ef1c978e4799526794dc9965b00e9e63..fdf0c32b8a76ac25e25a91f0f0899787bd8a91da 100644
--- a/formats/format_pcm.c
+++ b/formats/format_pcm.c
@@ -472,9 +472,9 @@ static int load_module(void)
 	int index;
 
 	/* XXX better init ? */
-	for (index = 0; index < (sizeof(ulaw_silence) / sizeof(ulaw_silence[0])); index++)
+	for (index = 0; index < ARRAY_LEN(ulaw_silence); index++)
 		ulaw_silence[index] = AST_LIN2MU(0);
-	for (index = 0; index < (sizeof(alaw_silence) / sizeof(alaw_silence[0])); index++)
+	for (index = 0; index < ARRAY_LEN(alaw_silence); index++)
 		alaw_silence[index] = AST_LIN2A(0);
 
 	if ( ast_format_register(&pcm_f)
diff --git a/main/acl.c b/main/acl.c
index d903851ee36665c299ce95ecd701c049642d9493..54747de5f496030c7712712ca6eb6879ec477a5d 100644
--- a/main/acl.c
+++ b/main/acl.c
@@ -434,7 +434,7 @@ int ast_str2tos(const char *value, unsigned int *tos)
 		return 0;
 	}
 
-	for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) {
 		if (!strcasecmp(value, dscp_pool1[x].name)) {
 			*tos = dscp_pool1[x].space << 2;
 			return 0;
@@ -448,7 +448,7 @@ const char *ast_tos2str(unsigned int tos)
 {
 	unsigned int x;
 
-	for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) {
 		if (dscp_pool1[x].space == (tos >> 2))
 			return dscp_pool1[x].name;
 	}
diff --git a/main/callerid.c b/main/callerid.c
index 1e9bcad4256a9b7c2a3557afd3fe61aa1d333131..c8c3afcb6387559012300fba89a662c158228c51 100644
--- a/main/callerid.c
+++ b/main/callerid.c
@@ -1099,7 +1099,7 @@ int ast_parse_caller_presentation(const char *data)
 {
 	int i;
 
-	for (i = 0; i < ((sizeof(pres_types) / sizeof(pres_types[0]))); i++) {
+	for (i = 0; i < ARRAY_LEN(pres_types); i++) {
 		if (!strcasecmp(pres_types[i].name, data))
 			return pres_types[i].val;
 	}
@@ -1115,7 +1115,7 @@ const char *ast_describe_caller_presentation(int data)
 {
 	int i;
 
-	for (i = 0; i < ((sizeof(pres_types) / sizeof(pres_types[0]))); i++) {
+	for (i = 0; i < ARRAY_LEN(pres_types); i++) {
 		if (pres_types[i].val == data)
 			return pres_types[i].description;
 	}
@@ -1131,7 +1131,7 @@ const char *ast_named_caller_presentation(int data)
 {
 	int i;
 
-	for (i = 0; i < ((sizeof(pres_types) / sizeof(pres_types[0]))); i++) {
+	for (i = 0; i < ARRAY_LEN(pres_types); i++) {
 		if (pres_types[i].val == data)
 			return pres_types[i].name;
 	}
diff --git a/main/channel.c b/main/channel.c
index cbfe8ffcae877f4a02fb4fb50f9e55a0a9beeabe..44371554953b54e5633e4dd42467919089929e69 100644
--- a/main/channel.c
+++ b/main/channel.c
@@ -752,7 +752,7 @@ int ast_best_codec(int fmts)
 	fmts &= AST_FORMAT_AUDIO_MASK;
 	
 	/* Find the first preferred codec in the format given */
-	for (x = 0; x < (sizeof(prefs) / sizeof(prefs[0]) ); x++) {
+	for (x = 0; x < ARRAY_LEN(prefs); x++) {
 		if (fmts & prefs[x])
 			return prefs[x];
 	}
diff --git a/main/frame.c b/main/frame.c
index f92628e28abf523a1b62ac67bf70de0f08919e19..29bf66a9df81c3ca6346451ddb75aba4415bea3e 100644
--- a/main/frame.c
+++ b/main/frame.c
@@ -503,7 +503,7 @@ struct ast_format_list *ast_get_format_list_index(int index)
 
 struct ast_format_list *ast_get_format_list(size_t *size) 
 {
-	*size = (sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]));
+	*size = ARRAY_LEN(AST_FORMAT_LIST);
 	return AST_FORMAT_LIST;
 }
 
@@ -511,7 +511,7 @@ char* ast_getformatname(int format)
 {
 	int x;
 	char *ret = "unknown";
-	for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
 		if (AST_FORMAT_LIST[x].bits == format) {
 			ret = AST_FORMAT_LIST[x].name;
 			break;
@@ -533,7 +533,7 @@ char *ast_getformatname_multiple(char *buf, size_t size, int format)
 	end += len;
 	size -= len;
 	start = end;
-	for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
 		if (AST_FORMAT_LIST[x].bits & format) {
 			snprintf(end, size,"%s|",AST_FORMAT_LIST[x].name);
 			len = strlen(end);
@@ -561,7 +561,7 @@ static const char *ast_expand_codec_alias(const char *in)
 {
 	int x;
 
-	for (x = 0; x < sizeof(ast_codec_alias_table) / sizeof(ast_codec_alias_table[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(ast_codec_alias_table); x++) {
 		if (!strcmp(in,ast_codec_alias_table[x].alias))
 			return ast_codec_alias_table[x].realname;
 	}
@@ -573,7 +573,7 @@ int ast_getformatbyname(const char *name)
 	int x, all, format = 0;
 
 	all = strcasecmp(name, "all") ? 0 : 1;
-	for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
 		if (all || 
 			  !strcasecmp(AST_FORMAT_LIST[x].name,name) ||
 			  !strcasecmp(AST_FORMAT_LIST[x].name, ast_expand_codec_alias(name))) {
@@ -590,7 +590,7 @@ char *ast_codec2str(int codec)
 {
 	int x;
 	char *ret = "unknown";
-	for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
 		if (AST_FORMAT_LIST[x].bits == codec) {
 			ret = AST_FORMAT_LIST[x].desc;
 			break;
@@ -1023,7 +1023,7 @@ void ast_codec_pref_remove(struct ast_codec_pref *pref, int format)
 	memcpy(&oldorder, pref, sizeof(oldorder));
 	memset(pref, 0, sizeof(*pref));
 
-	for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
 		slot = oldorder.order[x];
 		size = oldorder.framing[x];
 		if (! slot)
@@ -1043,7 +1043,7 @@ int ast_codec_pref_append(struct ast_codec_pref *pref, int format)
 
 	ast_codec_pref_remove(pref, format);
 
-	for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
 		if (AST_FORMAT_LIST[x].bits == format) {
 			newindex = x + 1;
 			break;
@@ -1051,7 +1051,7 @@ int ast_codec_pref_append(struct ast_codec_pref *pref, int format)
 	}
 
 	if (newindex) {
-		for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
+		for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
 			if (!pref->order[x]) {
 				pref->order[x] = newindex;
 				break;
@@ -1104,7 +1104,7 @@ int ast_codec_pref_setsize(struct ast_codec_pref *pref, int format, int framems)
 {
 	int x, index = -1;
 
-	for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
 		if (AST_FORMAT_LIST[x].bits == format) {
 			index = x;
 			break;
@@ -1128,7 +1128,7 @@ int ast_codec_pref_setsize(struct ast_codec_pref *pref, int format, int framems)
 		framems = AST_FORMAT_LIST[index].max_ms;
 
 
-	for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
 		if (pref->order[x] == (index + 1)) {
 			pref->framing[x] = framems;
 			break;
@@ -1144,7 +1144,7 @@ struct ast_format_list ast_codec_pref_getsize(struct ast_codec_pref *pref, int f
 	int x, index = -1, framems = 0;
 	struct ast_format_list fmt = { 0, };
 
-	for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
 		if (AST_FORMAT_LIST[x].bits == format) {
 			fmt = AST_FORMAT_LIST[x];
 			index = x;
@@ -1152,7 +1152,7 @@ struct ast_format_list ast_codec_pref_getsize(struct ast_codec_pref *pref, int f
 		}
 	}
 
-	for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
 		if (pref->order[x] == (index + 1)) {
 			framems = pref->framing[x];
 			break;
@@ -1182,7 +1182,7 @@ int ast_codec_choose(struct ast_codec_pref *pref, int formats, int find_best)
 {
 	int x, ret = 0, slot;
 
-	for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
 		slot = pref->order[x];
 
 		if (!slot)
diff --git a/main/logger.c b/main/logger.c
index 47c7772f0aaee014948dd83c0c1eee2576119864..18a864c0d6cdeec9d670ab1b04689d01fdfe0254 100644
--- a/main/logger.c
+++ b/main/logger.c
@@ -530,7 +530,7 @@ static int rotate_file(const char *filename)
 		/* Find the next empty slot, including a possible suffix */
 		for (x = 0; ; x++) {
 			found = 0;
-			for (which = 0; which < sizeof(suffixes) / sizeof(suffixes[0]); which++) {
+			for (which = 0; which < ARRAY_LEN(suffixes); which++) {
 				snprintf(new, sizeof(new), "%s.%d%s", filename, x, suffixes[which]);
 				fd = open(new, O_RDONLY);
 				if (fd > -1)
@@ -546,7 +546,7 @@ static int rotate_file(const char *filename)
 
 		/* Found an empty slot */
 		for (y = x; y > -1; y--) {
-			for (which = 0; which < sizeof(suffixes) / sizeof(suffixes[0]); which++) {
+			for (which = 0; which < ARRAY_LEN(suffixes); which++) {
 				snprintf(old, sizeof(old), "%s.%d%s", filename, y - 1, suffixes[which]);
 				fd = open(old, O_RDONLY);
 				if (fd > -1) {
diff --git a/main/manager.c b/main/manager.c
index 679a8899e775a8f7860f69259b5eff6075c6d0e8..486953fb50b6eee980501988d5f1091048308549 100644
--- a/main/manager.c
+++ b/main/manager.c
@@ -345,7 +345,7 @@ static char *authority_to_str(int authority, struct ast_str **res)
 	char *sep = "";
 
 	(*res)->used = 0;
-	for (i = 0; i < (sizeof(perms) / sizeof(perms[0])) - 1; i++) {
+	for (i = 0; i < ARRAY_LEN(perms) - 1; i++) {
 		if (authority & perms[i].num) {
 			ast_str_append(res, 0, "%s%s", sep, perms[i].label);
 			sep = ",";
@@ -387,7 +387,7 @@ static int get_perm(const char *instr)
 	if (!instr)
 		return 0;
 
-	for (x = 0; x < (sizeof(perms) / sizeof(perms[0])); x++) {
+	for (x = 0; x < ARRAY_LEN(perms); x++) {
 		if (ast_instring(instr, perms[x].label, ','))
 			ret |= perms[x].num;
 	}
@@ -415,7 +415,7 @@ static int strings_to_mask(const char *string)
 		return 0;
 	if (ast_true(string)) {	/* all permissions */
 		int x, ret = 0;
-		for (x = 0; x<sizeof(perms) / sizeof(perms[0]); x++)
+		for (x = 0; x < ARRAY_LEN(perms); x++)
 			ret |= perms[x].num;
 		return ret;
 	}
diff --git a/main/pbx.c b/main/pbx.c
index d0ae17e9b78ef1001269d8b9bfbe7de7ec829ae6..544c703f2e57b8aa93b3ab7cf6266e112257c38a 100644
--- a/main/pbx.c
+++ b/main/pbx.c
@@ -2320,7 +2320,7 @@ void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, c
 		}
 	}
 	/* if not found, look into chanvars or global vars */
-	for (i = 0; s == &not_found && i < (sizeof(places) / sizeof(places[0])); i++) {
+	for (i = 0; s == &not_found && i < ARRAY_LEN(places); i++) {
 		struct ast_var_t *variables;
 		if (!places[i])
 			continue;
@@ -3098,7 +3098,7 @@ const char *ast_extension_state2str(int extension_state)
 {
 	int i;
 
-	for (i = 0; (i < (sizeof(extension_states) / sizeof(extension_states[0]))); i++) {
+	for (i = 0; (i < ARRAY_LEN(extension_states)); i++) {
 		if (extension_states[i].extension_state == extension_state)
 			return extension_states[i].text;
 	}
diff --git a/main/rtp.c b/main/rtp.c
index c0c2bf17fbb83449096d4927c52bc0dbafd1fe20..1370e12d28ea94dda0c0fe3bbdc54885ab205772 100644
--- a/main/rtp.c
+++ b/main/rtp.c
@@ -2205,7 +2205,7 @@ int ast_rtp_set_rtpmap_type(struct ast_rtp *rtp, int pt,
 	
 	rtp_bridge_lock(rtp);
 
-	for (i = 0; i < sizeof(mimeTypes)/sizeof(mimeTypes[0]); ++i) {
+	for (i = 0; i < ARRAY_LEN(mimeTypes); ++i) {
 		if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
 		    strcasecmp(mimeType, mimeTypes[i].type) == 0) {
 			found = 1;
@@ -2312,7 +2312,7 @@ const char *ast_rtp_lookup_mime_subtype(const int isAstFormat, const int code,
 {
 	unsigned int i;
 
-	for (i = 0; i < sizeof(mimeTypes)/sizeof(mimeTypes[0]); ++i) {
+	for (i = 0; i < ARRAY_LEN(mimeTypes); ++i) {
 		if ((mimeTypes[i].payloadType.code == code) && (mimeTypes[i].payloadType.isAstFormat == isAstFormat)) {
 			if (isAstFormat &&
 			    (code == AST_FORMAT_G726_AAL2) &&
diff --git a/pbx/dundi-parser.c b/pbx/dundi-parser.c
index aca902a3af0b7494ddfd355d80f6eb4728c059d5..b37c6905f80af65378665adda02747a5cfee7579 100644
--- a/pbx/dundi-parser.c
+++ b/pbx/dundi-parser.c
@@ -71,7 +71,7 @@ int dundi_str_short_to_eid(dundi_eid *eid, const char *s)
 	if (sscanf(s, "%2x%2x%2x%2x%2x%2x", &eid_int[0], &eid_int[1], &eid_int[2],
 		 &eid_int[3], &eid_int[4], &eid_int[5]) != 6)
 		 	return -1;
-	for (x=0;x<6;x++)
+	for (x = 0; x < 6; x++)
 		eid->eid[x] = eid_int[x];
 	return 0;
 }
@@ -79,7 +79,7 @@ int dundi_str_short_to_eid(dundi_eid *eid, const char *s)
 int dundi_eid_zero(dundi_eid *eid)
 {
 	int x;
-	for (x=0;x<sizeof(eid->eid) / sizeof(eid->eid[0]);x++)
+	for (x = 0; x < ARRAY_LEN(eid->eid); x++)
 		if (eid->eid[x]) return 0;
 	return 1;
 }
diff --git a/res/res_limit.c b/res/res_limit.c
index 28a5e13235204e7074aab3bec8b543512e29b055..b1cf025b0c77e80dce4b095e2b8cad76a851859e 100644
--- a/res/res_limit.c
+++ b/res/res_limit.c
@@ -66,7 +66,7 @@ static struct limits {
 static int str2limit(const char *string)
 {
 	size_t i;
-	for (i = 0; i < sizeof(limits) / sizeof(limits[0]); i++) {
+	for (i = 0; i < ARRAY_LEN(limits); i++) {
 		if (!strcasecmp(string, limits[i].limit))
 			return limits[i].resource;
 	}
@@ -76,7 +76,7 @@ static int str2limit(const char *string)
 static const char *str2desc(const char *string)
 {
 	size_t i;
-	for (i = 0; i < sizeof(limits) / sizeof(limits[0]); i++) {
+	for (i = 0; i < ARRAY_LEN(limits); i++) {
 		if (!strcmp(string, limits[i].limit))
 			return limits[i].desc;
 	}
@@ -90,7 +90,7 @@ static char *complete_ulimit(struct ast_cli_args *a)
 
 	if (a->pos > 1)
 		return NULL;
-	for (i = 0; i < sizeof(limits) / sizeof(limits[0]); i++) {
+	for (i = 0; i < ARRAY_LEN(limits); i++) {
 		if (!strncasecmp(limits[i].limit, a->word, wordlen)) {
 			if (++which > a->n)
 				return ast_strdup(limits[i].limit);
@@ -154,7 +154,7 @@ static char *handle_cli_ulimit(struct ast_cli_entry *e, int cmd, struct ast_cli_
 	if (a->argc == 1) {
 		char arg2[3];
 		char *newargv[2] = { "ulimit", arg2 };
-		for (resource = 0; resource < sizeof(limits) / sizeof(limits[0]); resource++) {
+		for (resource = 0; resource < ARRAY_LEN(limits); resource++) {
 			struct ast_cli_args newArgs = { .argv = newargv, .argc = 2 };
 			ast_copy_string(arg2, limits[resource].limit, sizeof(arg2));
 			handle_cli_ulimit(e, CLI_HANDLER, &newArgs);