strange behaviour

K

Keith Thompson

CBFalconer said:
Chris Torek wrote: [...]
Others have pointed out several earlier problems. Another one
occurs here: you pass the integer constant 1 to allocation_memory(),
and then test for the character-constant '1', which is probably 49
or 241 (depending on whether your machine uses ASCII or EBCDIC), but
in any case is definitely not 1.

I think it is on MIX :)

I think you're right -- but '1' cannot equal 1 on any conforming C
implementation. (The digits are contiguous, and '\0' is reserved.)
Which means that a conforming C implementation for MIX can't use the
MIX-native character encoding.
 
A

Andrew Poelstra

CBFalconer said:
Chris Torek wrote: [...]
Others have pointed out several earlier problems. Another one
occurs here: you pass the integer constant 1 to allocation_memory(),
and then test for the character-constant '1', which is probably 49
or 241 (depending on whether your machine uses ASCII or EBCDIC), but
in any case is definitely not 1.

I think it is on MIX :)

I think you're right -- but '1' cannot equal 1 on any conforming C
implementation. (The digits are contiguous, and '\0' is reserved.)
Which means that a conforming C implementation for MIX can't use the
MIX-native character encoding.

MIX also fails in that it has no lowercase letters. IIRC, it also lacks
some of the spacing and symbol characters that are required.
 
R

Richard Bos

Andrew Poelstra said:
CBFalconer said:
Chris Torek wrote: [...]
Others have pointed out several earlier problems. Another one
occurs here: you pass the integer constant 1 to allocation_memory(),
and then test for the character-constant '1', which is probably 49
or 241 (depending on whether your machine uses ASCII or EBCDIC), but
in any case is definitely not 1.

I think it is on MIX :)

I think you're right -- but '1' cannot equal 1 on any conforming C
implementation. (The digits are contiguous, and '\0' is reserved.)
Which means that a conforming C implementation for MIX can't use the
MIX-native character encoding.

MIX also fails in that it has no lowercase letters. IIRC, it also lacks
some of the spacing and symbol characters that are required.

Isn't it a pity that MMIX uses ASCII <g>?

Richard
 
B

Barry Schwarz

this one is complete and compile, sorry about that i'm new on this forum ;-)

to test it just put text file in the commande line.
So it works well if i don't put the line
if (line == '\\'){ printf("hello");}



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char *line;
void allocation_memory(int);

int main(int argc, char *argv[]){

line = (char *)malloc(2*sizeof(char));


line can hold up to 2 char.
char c;
int i = 0;
int v = 0;
int l = 0;
int w = 0;

FILE *fp;
fp = fopen(argv[1], "r");
while ((c = fgetc(fp)) != EOF) {
line[l] = c;
allocation_memory(1);

Now line can hold up the 3 char, never any more. Is your data file
that small?

Once l reaches 3, you are attempting to overwrite memory you don't
own. This is undefined behavior.
}


fclose(fp);

i=0;
w = strlen(line);

Is there any reason to believe that you ever stored a '\0' in the
memory line points to
..
while (i < w){

A for loop is the more common approach here.
if (line == '\\'){
printf("hello");
}
printf("%c", line);
i++;
}
free(line);


main should return an int.
}

void allocation_memory(int grade)
{

int sizeOfLine = 3;

Because this is not static, it will always be 3 when this function
starts.
int sizeOfbodytext = 2;
int sizeOfdocwidth = 2;
switch( grade )
{
case '1' : if ((line = realloc(line, sizeOfLine))) {

sizeOfLine is always 3 at this point.
/* la réallocation s'est bien passée , l'affectation est
sûre: */
sizeOfLine = sizeOfLine++;

This invokes undefined behavior. Use either
sizeOfLine++;
or
sizeOfLine += 1;
} else {

printf ("*** Mémoire insuffisante\n");
exit (1);
free (line);

This line can never execute. exit() never returns after it is called.

Even if it did, line now contains NULL and you will not free any
memory.
}
// return NULL;
break;

}
}


Remove del for email
 

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

Similar Threads

Working with files 1
URGENT 1
strange behaviour of the program. 8
write error 13
Command Line Arguments 0
strtod strange behaviour 9
Opening a file of user's choice 11
comparison error 12

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top