malloc issues
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: malloc issues

  1. #1
    Join Date
    Mar 2004
    Posts
    43

    malloc issues

    I have read and read, but I cant seem to find the answer...
    I have written a program that calls this function:

    Code:
    char *ConvertToBinaryString(Ciphertext *cipher)
    {
    	int i;
    	char *output;
    
    	output = (char *)malloc(sizeof(char));
    	
    	strcpy(output, "");
    
    	for(i=0; i<64; i++)
    	{
    		if(cipher->cipher[i].bit == 1)
    			strcat(output, "1");
    		else
    			strcat(output, "0");
    
    	}
    
    	return output;
    }//End of ConvertToBinaryString
    In my main I call the function that calls the above function twice, sequentially. The first time, the function that calls the above function executes, everthing is fine and I get the desired output. The second time I call the function that calls this function, the malloc statement at the start of this function returns a NULL. I have tried time and time again to get around this. Does anyone have any ideas?

    I have tried using preset array size (the output will always be a 64 byte string), but to no avail. This function comes at the end of the function that calls it, and I have stepped through it a thousand times. Malloc always returns NULL the second time I call the function. Thanks in advance.

  2. #2
    Join Date
    Mar 2004
    Posts
    43

    additional info...

    Not sure if it makes a difference, but the structure I send the function has 64 individual bits and the purpose of the function itself is to look at the bits and create a character string of 1's and 0's.

  3. #3
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    output = (char *)malloc(sizeof(char));
    you have allocated a memory for a single char!
    you cant use strcat on that pointer unless you allocate
    enough memory to occupy that strcat.

    according to your loop, it should be:
    output = (char *)malloc(65*sizeof(char));

    where 65 = (64 characters + 1 character for null terminator)
    **** **** **** **** **/**

  4. #4
    Join Date
    Mar 2004
    Posts
    137
    Hi,

    ***************************************************
    output = (char *)malloc(sizeof(char));
    ...
    strcat(output, "1");
    ***************************************************

    How u can concat to a memory which is only one byte, u have to allocate some more memory. Looking at ur code (loop), it should be atleast 65 bytes.

    ashish

  5. #5
    Join Date
    Mar 2004
    Posts
    43
    I agree with the previous 2 posts, but it works once without initially allocating 65 bytes. I will try it though... thanks.

  6. #6
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    actually it didn't work, you just had luck (or unluck)
    it didn't crashed before. you had accessed a memory
    that is not belongs to you therefore one of the next scenarios
    could happen:
    1. your valus will be overriden
    2. you override data that is in used not by you
    each of these cannot be concidered as "worked"...
    **** **** **** **** **/**

  7. #7
    Join Date
    Mar 2004
    Posts
    43
    I got it working! Thanks for everyone's input.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center