unexpected array modification

J

j_hop_97

I'm hoping someone can explain the behavior of the following program.
When I run it on my linux machine the value of "bit[0]" for the last
"for" loop is "0".


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 4

int main(void)
{

int idx;
unsigned char bit[SIZE];

char tmpstr[1];

/* initialize the array*/
for (idx = 0;idx < SIZE; idx++) {
bit[idx] = (unsigned char)idx + 1;
}

printf("\n\n");
for (idx = 0;idx < SIZE; idx++) {
printf("%d. bit[%d] = '%d'\n", idx, idx, bit[idx]);
}

printf("\n\n");
for (idx = 0;idx < SIZE; idx++) {
printf("%d. bit[%d] = '%d'\n", idx, idx, bit[idx]);
}

strcpy(tmpstr, "0"); // why does this have any affect?

printf("\n\n");
for (idx = 0;idx < SIZE; idx++) {
printf("%d. bit[%d] = '%d'\n", idx, idx, bit[idx]);
}

return 0;
}



Makefile:

CC = gcc
CFLAGS = -Wall

all: test

test: test.c
$(CC) $(CFLAGS) -o $@ $<

clean:
rm -f *.o test

..PHONY: clean
 
B

Bill Pursell

j_hop_97 said:
I'm hoping someone can explain the behavior of the following program.
When I run it on my linux machine the value of "bit[0]" for the last
"for" loop is "0".


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 4

int main(void)
{

int idx;
unsigned char bit[SIZE];

char tmpstr[1];

/* initialize the array*/
for (idx = 0;idx < SIZE; idx++) {
bit[idx] = (unsigned char)idx + 1;
}

printf("\n\n");
for (idx = 0;idx < SIZE; idx++) {
printf("%d. bit[%d] = '%d'\n", idx, idx, bit[idx]);
}

printf("\n\n");
for (idx = 0;idx < SIZE; idx++) {
printf("%d. bit[%d] = '%d'\n", idx, idx, bit[idx]);
}

strcpy(tmpstr, "0"); // why does this have any affect?

Because tmpstr only has a size of one, but you are writing two
characters to here, (a '0' and a '\0'). It sounds like
the '\0' is being written to bit[0]. Try making tmpstr have
size 2, and the problem will probably go away.
 
G

Guest

j_hop_97 said:
I'm hoping someone can explain the behavior of the following program.
When I run it on my linux machine the value of "bit[0]" for the last
"for" loop is "0". [...]
char tmpstr[1]; [...]
strcpy(tmpstr, "0"); // why does this have any affect?

This will try to store two characters in tmpstr: '0', and '\0'. That
final '\0' is the only way C standard library functions can know how
long a string is. You only have enough room for a single character, so
the '\0' gets stored in whatever happens to follow tmpstr in memory.
(You may even get the program to exit, or worse, especially if nothing
directly follows tmpstr in memory.)
 
J

j_hop_97

Harald said:
j_hop_97 said:
I'm hoping someone can explain the behavior of the following program.
When I run it on my linux machine the value of "bit[0]" for the last
"for" loop is "0". [...]
char tmpstr[1]; [...]
strcpy(tmpstr, "0"); // why does this have any affect?

This will try to store two characters in tmpstr: '0', and '\0'. That
final '\0' is the only way C standard library functions can know how
long a string is. You only have enough room for a single character, so
the '\0' gets stored in whatever happens to follow tmpstr in memory.
(You may even get the program to exit, or worse, especially if nothing
directly follows tmpstr in memory.)

Yep, that was it. Defining tmpstr as "char tmpstr[2]" solved the
problem. Thanks.
 
C

Coos Haak

Op 9 Sep 2006 09:10:07 -0700 schreef j_hop_97:

/* initialize the array*/
for (idx = 0;idx < SIZE; idx++) {
bit[idx] = (unsigned char)idx + 1;
Lose the cast, it's unnecessary here, as most casts are.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top