Solved

string problems

Posted on 1998-08-26
5
191 Views
Last Modified: 2010-04-01
One last problem with my "getdiskspace" function...
The RootPathName has to hold the app path to give over to the api call, I've tried declaring the variable as a string but that caused problems, I thought the below code would work but it keeps coming up garbage. Code follows ->

char RootPathName[4];

RootPathName[0] = char(_getdrive() + 'A' -1);

RetVal = GetDiskFreeSpace(RootPathName,&SectorsPerCluster,
         &BytesPerSector,&NumberOfFreeClusters,  &TotalNumberOfClusters);
0
Comment
Question by:jtm082698
  • 3
  • 2
5 Comments
 

Author Comment

by:jtm082698
ID: 1171305
Edited text of question
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1171306
Your string is not termianted.  details follows.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1171307
C strings are arrays of characters that are terminated (end with) a NUL character.  When you declare

char RootPathName[4];

You have an array of 4 random characters.  This is followed by more random bytes that are not really part of the array and may cause an error it they are accessed.   Thus this array does not necessarily represent a string of 3 or 4 characters.  The string begins with the first character and continues through randomly set characters until it happens to come across a NUL character.

When you do

RootPathName[0] = char(_getdrive() + 'A' -1);

you set the first entry in the array to a letter, but the following entries are still random, thus the array does not necessarily hold a NUL terminated string.  

If you want the string to hold only 1 character, you can set the array 2nd entry to NUL by doing

RootPathName[1] = '\0';
0
 

Author Comment

by:jtm082698
ID: 1171308
Okay, I'm a little confused...can't you set an array or string to a line of text (ex. RootName = "C:\\" or "Hi-Di-Ho Kyle" without the null character)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1171309
Yes and no.  

(I guess you want more than that.)  first of all constant literal string, that is, 0 or more characters inside double quotes, like "C:\\", is always represented by the compiler as an array of characters followed by an extra NUL character that you didn't specify.  Thus "ABC" is a 4 character array with 4th character being a NUL.  "ABC\0" is a 5 character array with the 4th and 5th chracters both beng NUL.  So

RootName = "C:\\";

says rootname points to a 4 character array that contains a "C", a  ":", a "\" and then a NUL.  So RootName = "C:\\"; does set rootname to a valid nul terminated string.  Also

char RootName[4] = "C:\\";

does as well and is more like your original code.  But

char RootName[4];

leaves the 4 characters unitialized.  So it could contain a string that goes past the 4 characters allocated.

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now