i'm just lost.. in memory allocation

This is a discussion on i'm just lost.. in memory allocation within the C Programming forums, part of the General Programming Boards category; hi all gurus, well.. i'm lost. i'm sure anyone of you can pick me up, because this seems to be ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    7

    i'm just lost.. in memory allocation

    hi all gurus, well.. i'm lost. i'm sure anyone of you can pick me up, because this seems to be really basic knowledge i'm missing here.

    Code:
    struct ListElement
    {
    	struct ListElement *next;
    	char* URL;
    };
    
    //////////////
    
    char *data = "blaaaaaaaalb\0";
    
    ListElement *listptr = (struct ListElement*)malloc(sizeof(ListElement));
    listptr->next = NULL;
    listptr->URL = (char*)malloc(strlen(data)+1);
    listptr->URL = data;
    
    listptr->URL = (char*)realloc(listptr->URL, strlen(data)+1);
    at the line with realloc() it gives me a "user breakpoint called from code .." both in debug and release mode. what am i doing wrong here? i just want to assign a different string to URL and therefore need to reallocate. i also tried with free()'ing listptr->URL followed by a malloc(), but the same happens at free(listptr->URL).

    thanks for ur time.

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    eh wrong forum there, someone move this to C where it
    belongs!!?
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    No that's kinda C++. He doesn't typedef the struct the whole time, malloc/realloc is legal and he casts it. Problem:

    Code:
    listptr->URL = (char*)malloc(strlen(data)+1);
    listptr->URL = data;
    
    listptr->URL = (char*)realloc(listptr->URL, strlen(data)+1);
    You allocate memory, then reassign the pointer (memory leak), and then try to realloc something that wasn't originally allocated with realloc/malloc/calloc or is NULL, so it prolly crashes.

    Code:
    listptr->URL = (char*) malloc(strlen(data)+1);
    strcpy(listptr->URL, data);
    
    listptr->URL = (char*)realloc(listptr->URL, strlen(data)+1); // Block is the same size still...

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    7
    cheers! i love C

    i don't need the realloc() then too (for assigning other data possibly longer), i can just make a _strdup(new_data_that_might_be_longer) and thats it.

    thanks for the quick help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory allocation ptr to array? how?
    By kokopo2 in forum C Programming
    Replies: 8
    Last Post: 09-01-2005, 06:06 PM
  2. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 07:45 PM
  3. memory allocation problem....help..
    By CyC|OpS in forum C Programming
    Replies: 8
    Last Post: 10-18-2002, 10:26 AM
  4. Memory Allocation :: C/C++
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 08-15-2002, 11:38 AM
  5. dynamic memory allocation and returning pointers
    By sballew in forum C Programming
    Replies: 7
    Last Post: 11-03-2001, 03:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21