Thread: Either I'm Stupid or my compiler is stupid.....my vote is for me.

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    8

    Either I'm Stupid or my compiler is stupid.....my vote is for me.

    Now to the best of my knowlege the following code should work. but it casues an "Access violation" every time i try to run it.

    Code:
    int main(int argc, char* argv[])
    {
    	char *message;
    	int help = 1;
    	sprintf(message,"char: %d",help);
    	cout<<message;
    	return 0;
    }
    Any time i try doing anything else to a "char *" i get a runtime error that says memory location could not be "read". including this:

    Code:
    char *blank;
    int Size = 80;
    blank = new char[Size];
    memset((char*)blank,'0',Size);
    I'm also having some other errors that make absolutly no sense to me for example i have a DirectX9 program that draws a textured quad to th screen. Its and windowed program. I changed it to fullscreen recompiled and bang! it refused to load the texture from the disk. I cant find any reason why. ive been trying to figure this out for 2 days now. please help! thanks.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    8
    ok how would i do that then if i dont know what the size of the "message" is going to be?

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    8
    thanks. Didnt even thing of using a string class.

    another question. I was messing around with malloc and it wasnt allocationg what i wanted. explain what im doing wrong here please.


    Code:
    char *buffer;
    int Size = 6;
    
    buffer = malloc(Size);
    
    cout<<strlen(buffer);   //<<---prints out 25 for the value

    it prints out that value regardless of the size i mall0c to it. and when ever i read a sting into the buffer there is a giant empty space at the end of the string.

    any idea on that one short of using a string class?

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    i believe malloc will limit how small an allocation can be to prevent memory fragmentation when small pieces are released.

  5. #5
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Originally posted by Perspective
    i believe malloc will limit how small an allocation can be to prevent memory fragmentation when small pieces are released.
    am i on crack or is this actually correct? i remember my asembly prof mentioning something like this....

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    It is true, but it's not why strlen() returns 25 (in that particular instance).

    strlen() returns the number of bytes before the first '\0' character, starting at the pointer you provide. It has no way of knowing what memory you allocated, or where. It can run right on past the end of the buffer, until it hits a memory location with that particular value. It could stop halfway through the buffer if the buffer happened to contain a '\0'. Given the code, you cannot predict the return of strlen() in that case; it could be <6, ==6, or >6.
    Last edited by Cat; 08-03-2003 at 08:01 PM.

  7. #7
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >> It is true, but it's not why strlen() returns 25 (in that particular instance)

    cool. yeah, i wasnt trying to suggest Salem was wrong or anything, hitting the first null makes sense. i just wanted to verify if the malloc thing was correct or not. thnx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Problem: Stupid Compiler Error
    By iSlak in forum C++ Programming
    Replies: 4
    Last Post: 08-22-2005, 05:34 AM
  2. Stupid compiler
    By chris1985 in forum C Programming
    Replies: 5
    Last Post: 07-01-2005, 07:22 AM
  3. stupid compiler
    By polonyman in forum C++ Programming
    Replies: 32
    Last Post: 09-11-2004, 11:04 AM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  5. Am I stupid, or is it my compiler?
    By CodeMonkey in forum Windows Programming
    Replies: 3
    Last Post: 11-24-2001, 05:30 PM