Thread: Manipulating data in memory

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    8

    Manipulating data in memory

    So, it seems I have a bug in my code:

    Code:
    char *mem = malloc(9), *bottom = malloc(3), *in = "abc";
    	int position = 2, i;
    
    
    	mem = "onetwo";
    
    
    	printf("OLD mem = %s\n", mem);
    
    
    	/* save the second half */
    	for(i = position; i < (9 - position); i++)
    	{
    		bottom[i] = mem[i + position];
    		position++;
    	}
    
    
    	/* insert the middle */
    	for(i = position; i < (position + 3); i++)
    	{
    		mem[i] = in[i];
    	}
    
    
    	/* put it back together */
    	for(i = (position + 3); i < 9; i++)
    	{
    		mem[i + position] = bottom[i];
    	}
    
    
    	printf("NEW mem = %s\n", mem);
    What I'm trying to do is insert "abc" in the 3rd position of "onetwo". Results would be "oneabctwo", but all I get is crashing.

    There's got to be a better way to do this, I'm not sure.

    Thank you for your suggestions,

    Congi

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > mem = "onetwo";
    You didn't copy the memory, you just made it point somewhere else.

    This new place has the distinction of being read-only, so as soon as you try to write to it, the program crashes.

    > bottom[i] = mem[i + position];
    It seems unlikely that bottom has enough memory to contain whatever you're trying to write into it.
    It also seems likely that mem isn't long enough either.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    8
    I don't quite understand it.. is there a better way to do this or an example?

    Goal is to insert (not overwrite) data ("abc") in a position of one piece ("onetwo") of memory from another one.

    Currently I'm trying to do this by saving the data after the position in another buffer, overwriting with inserted data, and then putting back after the inserted data to make a new memory.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    strncpy(&s2[to],&s1[from],n);
    Copies n chars from a position in s1, to a position in s2.

    You only need this, and something to track where the \0 goes when you're done.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2015
    Posts
    8
    I see..

    Just curious, would this also work if the data is not a string (which is ended with a \0)?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You should try it, and find out.

    I know I can do it, but there are still plenty of ways to make a mess of it.

    Post your attempt, then we can see whether you've understood everything.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jan 2015
    Posts
    8
    Ok!

    Code:
    char *mem = malloc(9), *bottom = malloc(3), *in = "abc";
    	int position = 2, i;
    
    
    	mem = "onetwo";
    
    
    	printf("OLD mem = %s\n", mem);
    
    
    	strncpy(&mem[position], in, 3);
    
    
    	printf("NEW mem = %s\n", mem);
    Getting a crash when running... Just prints "OLD mem" and then stops.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This overwrites mem, which contained the return value of the malloc call:
    Code:
    mem = "onetwo";
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > mem = "onetwo";

    You missed this part of the information previously given
    > This new place has the distinction of being read-only, so as soon as you try to write to it, the program crashes.

    I suggest you forget about micro-managing malloc down to the last byte (you seem to be almost always off by 1 anyway), and start with

    Code:
    char mem[100] = "onetwo";
    char in[100] = "abc";
    int position = 2;
    strncpy(&mem[position], in, 3);
    At least until you understand exactly what is going on in memory, and the actual number of bytes needed to store a string.

    Then make a go of the malloc version.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Jan 2015
    Posts
    8
    I think I understand it now... thanks for your help!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating data in a FILE
    By Joemar P. Gava in forum C Programming
    Replies: 5
    Last Post: 09-28-2014, 02:44 AM
  2. Replies: 3
    Last Post: 12-17-2013, 02:19 PM
  3. I/O files: reading, manipulating and outputting data
    By Mwepak in forum C++ Programming
    Replies: 17
    Last Post: 12-12-2012, 07:50 AM
  4. Manipulating data in an array...
    By MikeyIckey in forum C Programming
    Replies: 11
    Last Post: 01-03-2009, 03:23 PM
  5. NOOB: Need a little help opening file and manipulating data
    By liquidcourage1 in forum C++ Programming
    Replies: 7
    Last Post: 02-26-2006, 10:27 PM