Thread: Pointers in a for loop

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    31

    Pointers in a for loop

    Hi

    if i have a for loop and inside the for loop i have a pointer. the declaration, allocating of memory and freeing the pointer is done within the for loop. Will there be any problem? Am I treating the pointer as a local pointer?

  2. #2
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    Should not be a problem. Can you please share the code so as to get more clarity on the situation?

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    31
    Code:
    for(h=0;h<10;h++)
    {
    	testStruct_t* ptyTestStruct;				
    	if(test_FillStruct(&ptyTestStruct)!=test_SUCCESS)
    	{
    		free(ptyTestStruct);
    	}
    }
    Basically its like this. the function test_FillStruct will allocate memory inside the function, hence passing in address of the pointer.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You wont have a problem, but you also wont have access to the pointer or what it points to outside that for loop. You are correctly passing the address of ptyTestStruct assuming that test_FillStruct actually allocates memory, something like
    Code:
    *ptyTestStruct = malloc(sizeof(**ptyTestStruct));

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    31
    @anduril462 THanks! yup that was my intention of not having access to the pointer outside that for loop. My worry was that I am not sure whether the pointer declared within each loop will affect the pointers declared for subsequent loops

  6. #6
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    Ya, as of this code, there should not be any problem.
    Can you post the function 'test_FillStruct()' ??

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The curly braces for the for loop create a new scope, similar to that of a function. The variable ptyTestStruct is only valid in that scope. Be careful, since you could still have a memory leak if you allocate in each loop and don't always free. Once the for loop is over, the ptyTestStruct variable ceases to exist, so you can't pass it to free.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. running through a loop using pointers
    By boy in forum C Programming
    Replies: 43
    Last Post: 08-19-2011, 04:43 PM
  2. Initialising pointers using a loop.
    By will of fortune in forum C Programming
    Replies: 4
    Last Post: 03-13-2010, 05:17 AM
  3. Loop Help with Pointers.
    By 1BadRbt in forum C Programming
    Replies: 3
    Last Post: 12-20-2006, 01:36 AM
  4. pointers, arrays, for loop, class
    By Lucid003 in forum C++ Programming
    Replies: 7
    Last Post: 10-18-2005, 04:36 PM
  5. pointers in arrays... Declaring objects in a loop.
    By Guest852 in forum C++ Programming
    Replies: 10
    Last Post: 04-05-2005, 06:57 AM