How to "free" an array cell.

This is a discussion on How to "free" an array cell. within the C Programming forums, part of the General Programming Boards category; Hi all. Suppose I have a char array of size 3 with the following contents: Code: [a][b][

Thread: How to "free" an array cell.

] where
  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    20

    How to "free" an array cell.

    Hi all. Suppose I have a char array of size 3 with the following contents:

    Code:
    [a][b][\0]
    where \0 is the null pointer. Is there a way that I can make the array become

    Code:
    [][b][\0]
    (That is, I want to "free" cell 0, empty it, return it to its state right after the array declaration.)

    I tried the following bit of code:

    Code:
    	char foo[3];
    	printf("%c\n", foo[0]);
    Windows XP returns the character 'p' and I've no idea why.

    Thanks for any help!

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Wow, where to begin.

    1. In C you cannot just free a single element like you suggested. You would either free the whole array or nothing.
    2. In your 'foo[]' example, you didn't free anything you just created an array and then printed the contents of element zero. The fact that a 'p' came out was entirely accidental; could have been garbage or any random character.
    3. If you wanted to initialize an array to a known state, look up the function memset(); this could also be used to "reset" the array as well. free() only involves de-allocating memory, not setting it to any specific value.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    If you want to print nothing for foo[0] , then assign the foo[0] with '\0' ,

    if you want to print 'b' in foo[0] , then do the shift operation using a for loop. i

    if your idea is to release the array memory then there is no way in C.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    20
    jeffcobb,

    The way it sounds, memset() is the function I am looking for.

    And, also, I wasn't trying to free anything in the code I gave. I was just looking at what arrays hold right after initialization so that I can "reset" them.

    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an object with a dynamic pointer
    By maxsthekat in forum C++ Programming
    Replies: 11
    Last Post: 09-16-2009, 01:52 PM
  2. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  3. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  4. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

Tags for this Thread