Solved

C array problems. Compiler bug? or not.

Posted on 1997-12-04
5
175 Views
Last Modified: 2010-04-02
Ok, right now im working with Watcom C Version 10.0a
and ive either found some sort of bug (hope not) or
ive missed something very obvious.

The following code is what ive writen, and i'll outline the
problem after.
-----

#include <stdio.h>

int main()
{
  char string[10];

  string[0] = 0;
  printf("String[0] = %d",string[0]);

  return 0;
}

Ok, the code above ive had no problem with at all, no errors etc etc, BUT the code below buggers up BIG time! :(

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

int main()
{
  char *string;

  string = (char *)malloc(10);

  // for the sake of peoples getting through the code
  // quickly, just note, i KNOW there is enough memory
  // avail. And string has never been NULL yet.

  string[0] = 0;
  printf("String[0] = %d",string[0]);

  return 0;
}

Ok, now this code prints '0' to the stdout, as it should.
BUT ive done other things with this and ended up needing to
go into WD (Watcom Debugger) and traced through the code,
the problem is this,

The first code segment included sets string[0] to 0, Perfect!, But setting the var string[0] to 0 in the second segment of code seems to cause problems!, WD is has the value of string[0] as ????

(Note: all other elements in the allocated array are set-able and work fine except the first one!, and some of my functions get very confused with the value of ???? whatever that is!, although printf STILL outputs a '0' to stdout for it.)

Also remember setting string[0] for the static array works fine, not the allocated one.

HELP!!! what is going on?!, I really need to find out how
to get past this, as i cant use static var's for what i want to do, its just an example! ;)

Thanks for your help.

Chris.
0
Comment
Question by:ccheek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 4

Expert Comment

by:jos010697
ID: 1256507
Are the code snippets above a verbatim copy of the code
that's giving you trouble? If so, your debugger or compiler
is broken (quite unlikely, but possible ...)

If not, could you supply a (short) snippet of code that's
actually giving you problems?

kind regards,

Jos aka jos@and.nl
0
 
LVL 1

Expert Comment

by:terrycj
ID: 1256508
it doesn't look like it's your fault, but on the other hand, something
as basic as this should not be failing. in the meantime, you can
do something like this which may allow you to keep working
until you get a real solution:

#define my_malloc(n) (malloc((n) + 1) + 1)

then just use my_malloc instead of malloc. if it's really only
the first byte that's inaccessible, this will work fine.

terry.

0
 
LVL 1

Expert Comment

by:meessen
ID: 1256509
I still don't understand what the problem is.
This ???? may just be a debugger problem.
There may be a printf problem, or there may be a malloc problem.
it is not possible to know with the given information.

We have first to understand where the problem is comming from.

First check if the stored value in string[0] is the expected value. Also check with explicit casting like string[0] = (char)5;
Then test if(string[0] == 5).
Check with different values and of course with 0.
After that you should be sure about the string buffer content.
If there is something here it may be the malloc function.
I think you have a tool to display memory content. Look at the location string points to to see what is stored there. make sure it is really what you expect.

If everything is ok there, then check the printf function.
This function is tricky because it uses vararg. And you pass it a char type value. You should try casting explicitely the string[0] to an int like this: printf( "string[0] = %d\n", (int)string[0] ); Strange and unexpected things may happen with printf.

If everything is ok there, check the debugger. Well there is not much to do about it then. And this should normally not confuse your functions.


0
 
LVL 10

Accepted Solution

by:
rbr earned 70 total points
ID: 1256510
I also think this could be a debugger bug. What does your debugger print out if you print instead of string[0] *string. If this is 0 all should be ok.
0
 

Author Comment

by:ccheek
ID: 1256511
Yeah, it could be a Debugger bug, although my program has problems with the very code itself anyway.

I use the following computer system, surly no flaw with the computer itself?

Pentium Pro 200 (TX chipset i believe.)
32meg RAM (EDO)

Oh well, thanks everyone that added comments etc.


0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question