Allocated memory size is not correct.

This is a discussion on Allocated memory size is not correct. within the C Programming forums, part of the General Programming Boards category; Hi everybody, I'm writing a simple program which is declaring an array of strings and allocating some space using malloc(). ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    4

    Allocated memory size is not correct.

    Hi everybody,

    I'm writing a simple program which is declaring an array of strings and allocating some space using malloc(). Then, Im gonna sort it using qsort(), so i need the size of my array, but it always displays wrong size. However, If I declare the array as I define it and initialize it, the real size is returned. I'd appreciate any opinion or comments that may help.
    (FYI, I havent programmed in c for 3-4 years and Im not a malloc expert!!!)

    if I use this, that would work fine: (Approach 1)
    Code:
        char *S1[] = {"vbhk", "dfgnhd", "dtj6f", "e56u", "qwert", "ertert"};
    But this way, its not working: (Approach 2)
    Code:
     
        char** S1;
        size_t rows =10;
        size_t cols= 6;
        
        S1 = (char**) malloc(sizeof(char*) * rows);
        for(i=0; i<rows; i++){
             S1[i] = (char*) malloc(sizeof(char) * cols);
         }
    I expect that sizeof(S1) returns 10 and sizeof(char*) returns 6. But they return 1 and 4, respectively.

    Anything?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    sizeof() on pointers tells you the size of the pointer, NOT how much memory it is pointing to.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    Thank you; So what should I do? Hoever I've seen qsort usage on such an array which is fine. so what do you think? Even when I try to qsort it using constants, I encounter Segmentation fault error :
    I need to provide those sizes for the follwong function:
    Code:
     
    qsort(S1, stringlen, sizeof(char*), cstring_cmp);

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ramtinraji View Post
    Thank you; So what should I do?
    Pay attention to what qsort wants:

    qsort( sort_this, how_many_is_this, how_big_is_one_item, my_sort_function )


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    Thank you very much guys. I solved the problem. You are right Quzah, I just put constant 100 (as in 100 strings) instead of stringlen and it worked!!

    Thanks again Salem and Quzah. GoodDay

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    Why would you sort on a random constant number like 100 things? For the dynamic array, you have 'rows' amount of char * to sort. In any case, a magic number isn't really going to be the best code, it could seg fault at any time.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    My input file consists of 100 lines of ascii text which should be sorted. I have defined my textBuffer as an array of 100 strings and everywhere, loops and etc., its 100. So its not a magic number. However, that'd be great if you have any idea how I can replace it with some variable denoting the size (i mean number of line) of my textBuffer.

    There must be someway, any idea?

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    Count the lines while you read the file.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by whiteflags View Post
    Count the lines while you read the file.
    Wow, what a completely vague and unrelated response. This thread went from "how do I use qsort" to you turning it into counting lines in a file, which has nothing to do with anything in this thread until you brought it up.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Dynamically allocated size
    By maverickbu in forum C++ Programming
    Replies: 12
    Last Post: 06-26-2007, 01:16 PM
  4. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 01:15 AM
  5. Where are pointers allocated in memory?
    By sparks in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2005, 12:52 PM

Tags for this Thread


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